gpt4 book ai didi

python - Pyqtgraph - 如何添加轴日期时间?

转载 作者:行者123 更新时间:2023-12-04 02:30:46 31 4
gpt4 key购买 nike

该示例使用文件 '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>

最佳答案

您必须使用 setAxisItems() 方法:

# ...
axis = pg.DateAxisItem(orientation='bottom')
self.line = pg.PlotCurveItem(clear=True, pen="g")
self.widget.addItem(self.line)
<b>self.widget.setAxisItems({"bottom": axis})</b>
# ...

关于python - Pyqtgraph - 如何添加轴日期时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64323137/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com