【问题标题】:pyqtgraph plotwidget multiple Y axis plots in wrong areapyqtgraph plotwidget 错误区域中的多个 Y 轴图
【发布时间】:2018-07-13 09:46:53
【问题描述】:

我在通过 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"))

【问题讨论】:

    标签: python plot axis pyqtgraph


    【解决方案1】:

    我在这里找到了答案MultiplePlotAxes.py

    将此self.p2.setGeometry(self.p1.vb.sceneBoundingRect()) 添加到函数'update_graph_plot' 会在每次更新场景时调整视图框的大小,但它必须在更新循环中。

    或者将这个self.p1.vb.sigResized.connect(self.updateViews)添加到函数'pg_plot_setup'作为设置的一部分,然后它会自动调用这个函数

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

    每次 self.p1 更新时调整视图框 (self.p2) 的大小。

    【讨论】:

      猜你喜欢
      • 2015-06-10
      • 2019-02-10
      • 2018-05-20
      • 2017-07-30
      • 2017-07-04
      • 1970-01-01
      • 2013-09-24
      • 1970-01-01
      • 2014-12-06
      相关资源
      最近更新 更多