Pyqtgraph - 如何添加轴日期时间?
Posted
技术标签:
【中文标题】Pyqtgraph - 如何添加轴日期时间?【英文标题】:Pyqtgraph - how add axis Datetime? 【发布时间】:2021-01-27 01:53:27 【问题描述】:该示例使用文件 'QtChart.ui'(这很重要),其中嵌入了 Pyqtgraph 的小部件(这也很重要)。
问题: 图表已显示,但显示的是纪元开始以来的秒数,而不是日期时间轴。
我设置了变量axis = pg.DateAxisItem(orientation='bottom')
按照例子,应该加到pw = pg.PlotWidget(viewBox=vb, axisItems='bottom': axis)
这样一行,但是我的代码中没有这样一行。
如何设置日期时间轴而不是从纪元开始的秒数?
代码:
import random
import sys
from PyQt5.QtCore import *
from PyQt5 import QtWidgets, uic
from PyQt5.QtWidgets import QMainWindow
import time
import pyqtgraph as pg
x = []
y = []
class draw_interface(QMainWindow):
def __init__(self):
super(draw_interface, self).__init__()
uic.loadUi('QtChart.ui', self)
self.timer_update_events = QTimer()
self.timer_update_events.timeout.connect(self.update_chart)
self.timer_update_events.start(1000)
axis = pg.DateAxisItem(orientation='bottom')
self.line = pg.PlotCurveItem(clear=True, pen="g")
self.widget.addItem(self.line)
self.show()
def update_chart(self):
global x, y
x.append(time.time())
y.append(random.uniform(0, 1))
self.line.setData(x, y)
my_app = QtWidgets.QApplication([])
my_main_window = draw_interface()
sys.exit(my_app.exec_())
和代码'QtChart.ui':
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>1319</width>
<height>1091</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<layout class="QGridLayout" name="gridLayout">
<property name="leftMargin">
<number>10</number>
</property>
<property name="topMargin">
<number>10</number>
</property>
<property name="rightMargin">
<number>10</number>
</property>
<property name="bottomMargin">
<number>10</number>
</property>
<item row="0" column="0">
<widget class="QFrame" name="frame_2">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="maximumSize">
<size>
<width>200</width>
<height>16777215</height>
</size>
</property>
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="PlotWidget" name="widget" native="true"/>
</item>
</layout>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>1319</width>
<height>21</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<customwidgets>
<customwidget>
<class>PlotWidget</class>
<extends>QWidget</extends>
<header location="global">pyqtgraph</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>
【问题讨论】:
【参考方案1】:你必须使用setAxisItems()
方法:
# ...
axis = pg.DateAxisItem(orientation='bottom')
self.line = pg.PlotCurveItem(clear=True, pen="g")
self.widget.addItem(self.line)
self.widget.setAxisItems("bottom": axis)
# ...
【讨论】:
以上是关于Pyqtgraph - 如何添加轴日期时间?的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 pyqtgraph 的 GLViewWidget 将轴特征(标签、刻度、值)添加到 3D 图中?
如何在pyqtgraph PlotWidget中添加字符串轴