pyqtgraph plotwidget错误区域中的多个Y轴图

时间:2018-02-02 20:23:31

标签: python plot axis pyqtgraph

我在通过PlotWidget连接的QT5 gui中有一个嵌入式小部件。 我试图绘制2个实时数据流电压(self.p1)&当前(self.p2)。左轴上的电压&目前在右边。到目前为止,我将每个数据流与其相关轴相关联。 但是我的问题是当前的情节(self.p2)不在显示器的正确区域。此特定图表显示在窗口小部件的左上角,它出现在LHD轴之前。最好查看图像以查看问题。

查看我

我知道问题在于设置功能& self.p2(当前)被放置在错误的位置,但我的搜索没有产生答案。 有人可以帮忙吗?

用于生成图表的代码在启动时调用一次:

def pg_plot_setup(self): # not working still on left axis
    self.p1 = self.graphicsView.plotItem    

# x axis    
    self.p1.setLabel('bottom', 'Time', units='s', color='g', **{'font-size':'12pt'})
    self.p1.getAxis('bottom').setPen(pg.mkPen(color='g', width=3))

# Y1 axis   
    self.p1.setLabel('left', 'Voltage', units='V', color='r', **{'font-size':'12pt'})
    self.p1.getAxis('left').setPen(pg.mkPen(color='r', width=3))

    self.p2 = pg.ViewBox()  
    self.p1.showAxis('right')
    self.p1.scene().addItem(self.p2)
    self.p1.getAxis('right').linkToView(self.p2)
    self.p2.setXLink(self.p1)

# Y2 axis
    self.p1.setLabel('right', 'Current', units="A", color='c', **{'font-size':'12pt'})  #<font>&Omega;</font>
    self.p1.getAxis('right').setPen(pg.mkPen(color='c', width=3))

和用于更新显示的代码通过QTimer调用:

def update_graph_plot(self):
    start = time.time()
    X = np.asarray(self.graph_X, dtype=np.float32)
    Y1 = np.asarray(self.graph_Y1, dtype=np.float32)
    Y2 = np.asarray(self.graph_Y2, dtype=np.float32)

    pen1=pg.mkPen(color='r',width=1.0)
    pen2=pg.mkPen(color='c',width=1.0)

    self.p1.plot(X,Y1,pen=pen1, name="V", clear=True)
    self.p2.addItem(pg.PlotCurveItem(X,Y2,pen=pen2, name="I"))

1 个答案:

答案 0 :(得分:1)

I found the answer buried in here MultiplePlotAxes.py

adding this self.p2.setGeometry(self.p1.vb.sceneBoundingRect()) to function 'update_graph_plot' adjusts the size of viewbox each time the scene is updated, but it has to be in the update loop.

or add this self.p1.vb.sigResized.connect(self.updateViews) to function 'pg_plot_setup' as part of the setup, which shall then automatically call this function

def updateViews(self):
        self.p2.setGeometry(self.p1.vb.sceneBoundingRect())

to resize viewbox (self.p2) each time self.p1 updates. enter image description here