【问题标题】:Multiple overlayed plots in pyqtgraph (trouble binding an axis)pyqtgraph 中的多个叠加图(无法绑定轴)
【发布时间】:2018-01-27 10:17:01
【问题描述】:

我的图表有点问题。我可以同时绘制两条曲线,但第二条曲线似乎也绑定到左轴,有谁知道我是否可以用第二条曲线绘制右轴比例? 这是一个有效的简短示例:

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_()

【问题讨论】:

  • 是否要显示两个图表的最后 n 个元素?
  • 是的,我明白这一点,但我的问题是你是想从头显示所有数据还是只显示最后 n 个数据。
  • 抱歉,所有数据

标签: python python-2.7 pyqt pyqt4 pyqtgraph


【解决方案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)

【讨论】:

  • 如果我们有任何问题,我可以通过什么方式获取您的联系信息以便与我的公司合作?
猜你喜欢
  • 2018-07-13
  • 2013-09-21
  • 2015-06-10
  • 1970-01-01
  • 1970-01-01
  • 2015-06-10
  • 1970-01-01
  • 2019-09-01
  • 2012-11-30
相关资源
最近更新 更多