我使用pyqtgraph绘制2条曲线(curve1和curve2),其中有一个X轴和两个Y轴,Y1(左)和Y2(右)。 如何将Curve1与Y1轴关联,以及curve2与Y2轴关联。 如果在Y1轴(分别为Y2)上平移或缩放,则仅曲线1(分别为Y2)必须移动。 如果我平移或缩放绘图区域,则X,Y1和Y2轴必须适应。 提前致谢。 代码:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
#This example uses pyqtgraph to display a graph with 2 random curves.
import pyqtgraph.Qt
import numpy as np
import pyqtgraph
class To_Plot:
def __init__(self, eo_win):
self.ao_win = eo_win
pyqtgraph.setConfigOptions(antialias=True)
def create_plot(self):
self.axisY1 = pyqtgraph.AxisItem(orientation='left') # Y1 axis
self.axisY2 = pyqtgraph.AxisItem(orientation='right') # Y2 axis
self.plt1 = self.ao_win.addPlot(title="Updating plot")
self.plt1.showAxis('left')
self.plt1.showAxis('right')
self.curve1 = self.plt1.plot(pen=(255,0,0)) # Red curve
self.curve2 = self.plt1.plot(pen=(0,255,0)) # Green curve
self.data1 = np.random.normal(size=(10,1000))
self.data2 = np.random.normal(size=(10,1000))
self.ptr = 0
def update_plot(self):
self.curve1.setData(self.data1[self.ptr%10])
self.curve2.setData(self.data2[self.ptr%10])
if self.ptr == 0:
self.plt1.enableAutoRange('xy', False) # Stop auto-scaling after the first data set is plotted
self.ptr += 1
def init_timer(self):
self.timer = pyqtgraph.Qt.QtCore.QTimer()
self.timer.timeout.connect(self.update_plot)
self.timer.start(50)
#------------------------------------------------------------------------
#Create the main window.
#Return the main window created.
def create_main_win():
vo_win = pyqtgraph.GraphicsWindow(title="Basic plotting examples")
vo_win.resize(1200,600)
vo_win.setWindowTitle('pyqtgraph example: Plotting')
return vo_win
def main():
import sys
# Create the application and the main window
vo_app = pyqtgraph.Qt.QtGui.QApplication([])
vo_win = create_main_win()
# Create the plot
vo_plot1 = To_Plot(vo_win)
vo_plot1.create_plot()
vo_plot1.init_timer()
pyqtgraph.Qt.QtGui.QApplication.instance().exec_()
if __name__ == '__main__':
main()