Skip to main content
 首页 » 编程设计

python之如何使用 pyqtgraph 使多个左轴项具有相同的对齐/位置

2025年02月15日6zdz8207

我想知道是否有人可以帮助我。我正在尝试转换 pyqtgraph 中的 MultiplePlotAxes.py 示例,以便所有轴都位于右侧并对齐。这是我的代码,我添加了一个额外的情节:

""" 
Demonstrates a way to put multiple axes around a single plot. 
(This will eventually become a built-in feature of PlotItem) 
""" 
#import initExample  ## Add path to library (just for examples; you do not need this) 
 
import pyqtgraph as pg 
from pyqtgraph.Qt import QtCore, QtGui 
import numpy as np 
 
pg.mkQApp() 
 
pw = pg.PlotWidget() 
pw.show() 
pw.setWindowTitle('pyqtgraph example: MultiplePlotAxes') 
p1 = pw.plotItem 
p1.setLabels(left='axis 1') 
 
## create a new ViewBox, link the right axis to its coordinate system 
p2 = pg.ViewBox() 
p1.showAxis('right') 
p1.scene().addItem(p2) 
p1.getAxis('right').linkToView(p2) 
p2.setXLink(p1) 
p1.getAxis('right').setLabel('axis2', color='#0000ff') 
 
## create third ViewBox. 
## this time we need to create a new axis as well. 
p3 = pg.ViewBox() 
ax3 = pg.AxisItem('right') 
p1.layout.addItem(ax3, 2, 3) 
p1.scene().addItem(p3) 
ax3.linkToView(p3) 
p3.setXLink(p1) 
ax3.setZValue(-10000) 
ax3.setLabel('axis 3', color='#ff0000') 
 
## create forth ViewBox. 
## this time we need to create a new axis as well. 
p4 = pg.ViewBox() 
ax4 = pg.AxisItem('right') 
p1.layout.addItem(ax4, 2, 4) 
p1.scene().addItem(p4) 
ax4.linkToView(p4) 
p4.setXLink(p1) 
ax4.setZValue(-10000) 
ax4.setLabel('axis 4', color='#2EFEF7') 
 
## Handle view resizing 
def updateViews(): 
    ## view has resized; update auxiliary views to match 
    global p1, p2, p3, p4 
    p2.setGeometry(p1.vb.sceneBoundingRect()) 
    p3.setGeometry(p1.vb.sceneBoundingRect()) 
    p4.setGeometry(p1.vb.sceneBoundingRect()) 
 
    ## need to re-update linked axes since this was called 
    ## incorrectly while views had different shapes. 
    ## (probably this should be handled in ViewBox.resizeEvent) 
    p2.linkedViewChanged(p1.vb, p2.XAxis) 
    p3.linkedViewChanged(p1.vb, p3.XAxis) 
    p4.linkedViewChanged(p1.vb, p4.XAxis) 
 
updateViews() 
p1.vb.sigResized.connect(updateViews) 
 
p1.plot([1, 2, 4, 8, 16, 32]) 
p2.addItem(pg.PlotCurveItem([1, 2, 4, 9, 16, 32], pen='b')) 
p3.addItem(pg.PlotCurveItem([1, 2, 4, 7, 16, 32], pen='r')) 
p4.addItem(pg.PlotCurveItem([1, 3, 5, 7, 17, 32], pen='c')) 
 
## Start Qt event loop unless running in interactive mode or using pyside. 
if __name__ == '__main__': 
    import sys 
 
    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'): 
        QtGui.QApplication.instance().exec_() 

这会产生下面的图片,我添加了一个红色箭头来显示我想要做什么。

根据我能够弄清楚的内容,我可以使用 pg.GraphicsLayout() 来完成此操作。但是,与 plotItem 直接关联的 axisItem 和创建的 axisItem 不匹配,因为附加到 plotitem 的那个更小。

这是我认为正在发生的事情的图片:

这是我的问题,因为我希望它们全部对齐。在附加的代码中,您可以将 addItem(axes,2,1) 添加到右侧,但它在左侧不起作用。有人知道我该怎么做吗?

在此先感谢您提供的任何帮助。

请您参考如下方法:

我遇到了同样的问题,看到了这篇帖子,对没有答案感到失望,但我终于设法解决了问题,所以我想我会在这里发帖。

如果您可以发布右侧轴的代码,我可以对其进行修补,但基本上我所做的是将底部轴附加到它自己的一行。

self.main_pI = pg.PlotItem() 
self.axis_bottom = self.main_pI.getAxis('bottom') 
self.axis_bottom.setLabel('Time (seconds)', color='#ffffff') 
self.layout.addItem(self.main_pI, row=1, col=2, rowspan=2) 
self.layout.addItem(self.axis_bottom, row=3, col=2, rowspan=2) 

我是 pyqtgraph 的菜鸟,所以我确定我会废除这个术语,但我分离了“底部”轴,并将它重新连接到布局,它在布局中单独成一行。

希望对您有所帮助。