我的图表有点问题。我可以同时绘制两条曲线,但第二条曲线似乎也与左轴有关,是否有人知道我是否可以在第二条曲线上使用右轴刻度? 这是一个简短的例子:
from PyQt4 import QtCore, QtGui
import pyqtgraph as pg
import random
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.central_widget = QtGui.QStackedWidget()
self.setCentralWidget(self.central_widget)
self.login_widget = LoginWidget(self)
self.login_widget.button.clicked.connect(self.plotter)
self.central_widget.addWidget(self.login_widget)
def plotter(self):
self.data =[0]
self.data2 = [0]
self.curve = self.login_widget.plot.getPlotItem().plot()
self.curve2 =self.login_widget.plot.getPlotItem().plot()
self.timer = QtCore.QTimer()
self.timer.timeout.connect(self.updater)
self.timer.start(0)
def updater(self):
self.data.append(self.data[-1]+0.2*(0.5-random.random()) )
self.curve.setData(self.data, pen=pg.mkPen('b', width=1))
self.data2.append(self.data2[-1]+0.2*(0.5-random.random()) )
self.curve2.setData(self.data2, pen=pg.mkPen('r', width=1))
class LoginWidget(QtGui.QWidget):
def __init__(self, parent=None):
super(LoginWidget, self).__init__(parent)
layout = QtGui.QVBoxLayout()
pg.setConfigOption('background', 'w')
pg.setConfigOption('foreground', 'k')
self.button = QtGui.QPushButton('Start Plotting')
layout.addWidget(self.button)
self.plot = pg.PlotWidget(title='Force and Extension vs. Time')
#self.plot.showGrid(x=True,y=True)
self.plot.showAxis('right')
p2 = pg.ViewBox()
self.plot.scene().addItem(p2)
p2.setXLink(self.plot)
ax2 = self.plot.getAxis('right').linkToView(p2)
ax = self.plot.getAxis('bottom')
ax3 = self.plot.getAxis('right')
ax3.setLabel('Extension', units='in.')
ax.setLabel('Time', units='s')
ay = self.plot.getAxis('left')
ay.setLabel('Force', units='lbf')
layout.addWidget(self.plot)
self.setLayout(layout)
def pg_clear(self):
self.plot.clear()
if __name__ == '__main__':
app = QtGui.QApplication([])
window = MainWindow()
window.show()
app.exec_()
答案 0 :(得分:1)
基于此link中显示的示例,我已经实现了以下解决方案,我已经为每个轴的标签添加了必要的颜色以轻松识别数据,我还实现了updateViews()函数,当我改变屏幕的大小时,显示正确。
注意:我修改了生成数据的函数,以便观察更改。
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.central_widget = QtGui.QStackedWidget()
self.setCentralWidget(self.central_widget)
self.login_widget = LoginWidget(self)
self.login_widget.button.clicked.connect(self.plotter)
self.central_widget.addWidget(self.login_widget)
self.curve = self.login_widget.it1
self.curve2 =self.login_widget.it2
def plotter(self):
self.data =[0]
self.data2 = [0]
self.timer = QtCore.QTimer()
self.timer.timeout.connect(self.updater)
self.timer.start(0)
def updater(self):
self.data.append(self.data[-1]+10*(0.5-random.random()))
self.curve.setData(self.data, pen=pg.mkPen('b', width=1))
self.data2.append(self.data2[-1]+0.1*(0.5-random.random()))
self.curve2.setData(self.data2, pen=pg.mkPen('r', width=1))
class LoginWidget(QtGui.QWidget):
def __init__(self, parent=None):
super(LoginWidget, self).__init__(parent)
layout = QtGui.QVBoxLayout()
pg.setConfigOption('background', 'w')
pg.setConfigOption('foreground', 'k')
self.button = QtGui.QPushButton('Start Plotting')
layout.addWidget(self.button)
self.plot = pg.PlotWidget(title='Force and Extension vs. Time')
layout.addWidget(self.plot)
self.setLayout(layout)
p1 = self.plot.plotItem
p2 = pg.ViewBox()
p1.showAxis('right')
p1.scene().addItem(p2)
p1.getAxis('right').linkToView(p2)
p2.setXLink(p1)
self.plot.getAxis('bottom').setLabel('Time', units='s')
self.plot.getAxis('left').setLabel('Force', units='lbf', color="#0000ff")
p1.getAxis('right').setLabel('Extension', units='in.', color="#ff0000")
def updateViews():
p2.setGeometry(p1.vb.sceneBoundingRect())
p2.linkedViewChanged(p1.vb, p2.XAxis)
updateViews()
p1.vb.sigResized.connect(updateViews)
self.it1 = p1.plot()
self.it2 = pg.PlotCurveItem()
p2.addItem(self.it2)