pyqtgraph : how to plot time series (date and time on the x axis)?

五迷三道 提交于 2019-12-29 08:11:49

问题


I want to plot time series with pyqtgraph, and display the date and/or time on the x axis scale, but I couldn't find how to do it.

Edit 1 : It seems like I should subclass AxisItem and reimplement tickStrings(). I'll look into this.

Edit 2 : Here is how I subclassed AxisItem. The documentation shows how to use the class.

from __future__ import print_function
from PySide.QtCore import *
from PySide.QtUiTools import *
from PySide.QtGui import *
import pyqtgraph as pg
import time

## Reimplements \c pyqtgraph.AxisItem to display time series.
# \code
# from caxistime import CAxisTime
# \# class definition here...
# self.__axisTime=CAxisTime(orientation='bottom')
# self.__plot=self.__glyPlot.addPlot(axisItems={'bottom': self.__axisTime}) # __plot : PlotItem
# \endcode
class CAxisTime(pg.AxisItem):
    ## Formats axis label to human readable time.
    # @param[in] values List of \c time_t.
    # @param[in] scale Not used.
    # @param[in] spacing Not used.
    def tickStrings(self, values, scale, spacing):
        strns = []
        for x in values:
            try:
                strns.append(time.strftime("%H:%M:%S", time.gmtime(x)))    # time_t --> time.struct_time
            except ValueError:  # Windows can't handle dates before 1970
                strns.append('')
        return strns

回答1:


Take a look at this gist. You could modify tickStrings() to change format string according to scale



来源:https://stackoverflow.com/questions/23151612/pyqtgraph-how-to-plot-time-series-date-and-time-on-the-x-axis

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!