在 pyqtgraph 中使用 AxisItem 打开网格会导致轴缩放中断

turning grid on with AxisItem in pyqtgraph causes axis scaling to break

我在使用 AxisItem 时遇到问题。一旦我同时打开 x 和 y 网格,x 轴就不再能够使用 zoom/pan 函数进行缩放。有什么想法吗?

from PyQt4 import QtCore, QtGui 
from pyqtgraph import Point 
import pyqtgraph as pg


pg.setConfigOption('background', 'w')
pg.setConfigOption('foreground', 'k')

class plotClass(QtGui.QMainWindow):  
    def setupUi(self, MainWindow):

        self.centralwidget = QtGui.QWidget(MainWindow)      
        MainWindow.resize(1900, 1000)

        self.viewbox = pg.GraphicsView(MainWindow, useOpenGL=None, background='default')
        self.viewbox.setGeometry(QtCore.QRect(0, 0, 1600, 1000))

        self.layout = pg.GraphicsLayout()
        self.viewbox.setCentralWidget(self.layout)
        self.viewbox.show()

        self.view = self.layout.addViewBox()

        self.axis1 = pg.AxisItem('bottom', linkView=self.view, parent=self.layout)
        self.axis2 = pg.AxisItem('right', linkView=self.view, parent=self.layout)

        self.axis1.setGrid(255)
        self.axis2.setGrid(255)

        self.layout.addItem(self.axis1, row=1, col=0)
        self.layout.addItem(self.axis2, row=0, col=1)

if __name__== "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    MainWindow = QtGui.QMainWindow()
    ui = plotClass()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

查看您最后的评论,考虑这个选项:

pyqtgraph 示例文件夹包含一个 "GraphItem.py" 示例,它仅通过 ViewBox 添加和显示 GraphItem 对象到 window。然而,他们不使用网格,所以如果你想使用带有 GraphItem 的网格,只需先添加一个 PlotItem(它已经有一个关联的 ViewBox ......你猜对了,......网格的 AxisItems!) ,... 然后获取 ViewBox 以添加您的 GraphItems。修改后的 GraphItem.py 看起来像这样(带有随附的 showGrid):

import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtGui
import numpy as np

# Enable antialiasing for prettier plots
pg.setConfigOptions(antialias=True)

w = pg.GraphicsWindow()
w.setWindowTitle('pyqtgraph example: GraphItem')

### comment out their add of the viewbox
### since the PlotItem we're adding will have it's 
### own ViewBox

#v = w.addViewBox()


pItem1 = w.addPlot()  # this is our new PlotItem
v = pItem1.getViewBox()  # get the PlotItem's ViewBox
v.setAspectLocked()  # same as before

g = pg.GraphItem() # same as before
v.addItem(g)  # same as before

pItem1.showGrid(x=True,y=True)  # now we can turn on the grid

### remaining code is the same as their example

## Define positions of nodes
pos = np.array([
    [0,0],
    [10,0],
    [0,10],
    [10,10],
    [5,5],
    [15,5]
    ])

## Define the set of connections in the graph
adj = np.array([
    [0,1],
    [1,3],
    [3,2],
    [2,0],
    [1,5],
    [3,5],
    ])

## Define the symbol to use for each node (this is optional)
symbols = ['o','o','o','o','t','+']

## Define the line style for each connection (this is optional)
lines = np.array([
    (255,0,0,255,1),
    (255,0,255,255,2),
    (255,0,255,255,3),
    (255,255,0,255,2),
    (255,0,0,255,1),
    (255,255,255,255,4),
    ], dtype=[('red',np.ubyte),('green',np.ubyte),('blue',np.ubyte),('alpha',np.ubyte),('width',float)])

## Update the graph
g.setData(pos=pos, adj=adj, pen=lines, size=1, symbol=symbols, pxMode=False)


## 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_()

我对此进行了测试,scroll/zooming 在启用网格后仍然有效,所以仍然不确定为什么用另一种方法不起作用,但有时找到另一种方法是最好的答案:)