Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

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

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...