pyqtgraph:如何绘制时间序列(x 轴上的日期和时间)? [英] pyqtgraph : how to plot time series (date and time on the x axis)?

查看:68
本文介绍了pyqtgraph:如何绘制时间序列(x 轴上的日期和时间)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 pyqtgraph 绘制时间序列,并在 x 轴刻度上显示日期和/或时间,但我找不到方法.

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.

似乎我应该继承 AxisItem 并重新实现 tickStrings().我会研究这个.

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

这是我对 AxisItem 进行子类化的方式.文档展示了如何使用该类.

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

推荐答案

看看这个要点.您可以修改 tickStrings() 以根据比例更改格式字符串

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

这篇关于pyqtgraph:如何绘制时间序列(x 轴上的日期和时间)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆