I am having a bit of a problem with my graph. I can get both curves to plot at the same time, but the second plot seems to be bound to the left axis as well, does anyone know if I can have the right axis scale with the 2nd curve? Here is a working short example:
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_()
Based on the example shown in this link I have implemented the following solution, I have added the necessary colors to the labels of each axis to easily recognize the data, I have also implemented the updateViews() function so that when I change the size of the Screen is displayed properly.
Note: I have modified the function that generates the data so that the changes are observed.
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)