python - pyqtgraph,绘制时间序列

标签 python datetime plot pyqtgraph

我正在尝试使用 pyqtgraph 绘制时间序列。我读过 this , thisthis .但我不确定如何正确使用它。

我的情节是一个情节小部件,我这样使用它:

graph.plot(aerosol_data, pen=pg.mkPen(color=colors[count], width=1, style=QtCore.Qt.SolidLine), axisItems={'bottom': TimeAxisItem(orientation='bottom')})

TimeAxisItem 的定义如下:

class TimeAxisItem(pg.AxisItem):
    def __init__(self, *args, **kwargs):
       super().__init__(*args, **kwargs)

    def tickStrings(self, values, scale, spacing):
        # PySide's QTime() initialiser fails miserably and dismisses args/kwargs
        return [useful_values_dict['useful_data']['data']['ISO_dates']]

其中 ISO_dates 是 ISO 格式的日期和时间列表

我也试过这个:

graph.plotItem.plot(aerosol_data, pen=pg.mkPen(color=colors[count], width=1, style=QtCore.Qt.SolidLine), axisItems={'bottom': TimeAxisItem(orientation='bottom')})

但没有效果(轴字符串仍然是数字)。

然后我尝试以这种方式使用 DateTimeAxis.py:

date_axis = pg.DateAxisItem('bottom', pen=None, linkView=None, parent=None, maxTickLength=-1, showValues=True)
date_axis.tickStrings(useful_values_dict['useful_data']['data']['timestamp_dates'],1, 1)

但是我得到一个错误:

File "C:\Python34\lib\site-packages\pyqtgraph\graphicsItems\DateAxisItem.py", line 161, in tickStrings
format_strings.append(x.strftime(tick_spec.format))
AttributeError: 'NoneType' object has no attribute 'format'

最佳答案

我终于解决了我的问题,这很容易。

我只需要以这种方式初始化我的绘图小部件:

    date_axis = pg.graphicsItems.DateAxisItem.DateAxisItem(orientation = 'bottom')
    self.graph = pg.PlotWidget(axisItems = {'bottom': date_axis})

并以这种方式绘制我的数据:

    graph.plot(x = useful_values_dict['useful_data']['data']['timestamp_dates'],
               y = useful_values_dict['useful_data']['data'][raw_header],
               pen=pg.mkPen(color=colors[count],width=1,style=QtCore.Qt.SolidLine))

以 x 数据作为时间戳数组。

谢谢!

关于python - pyqtgraph,绘制时间序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28296049/

相关文章:

python - Keras 中的自定义损失函数仅运行一次

python - 在 powershell 中运行 Anaconda 的 python 版本

javascript - 无法在 Angular 2/4 中获取本月的第一天

javascript - 使用时间作为 css 背景色

r - 将 fiddle 图与闪避的箱形图对齐

python - 如何使用 python 处理读取其中有注释的 .json 文件?

python - 是否有可能在 python 中杀死正在监听特定端口的进程,例如 8080?

sql-server - 日期添加副作用吗?

r - 将有图例的图与没有图例的图结合起来

python - 在matplotlib中使两个y轴对齐