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

时间:2016-11-09 22:48:05

标签: python pyqt pyqtgraph

我遇到AxisItem问题。一旦我打开x和y网格,x轴就不再能够使用缩放/平移功能进行缩放。有什么想法吗?

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

1 个答案:

答案 0 :(得分:1)

查看您的上一条评论,请考虑以下选项:

pyqtgraph examples文件夹包含一个“GraphItem.py”示例,该示例仅通过ViewBox向窗口添加和显示GraphItem对象。但是它们不使用网格,所以如果你想使用带有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_()

我测试了这个并且在启用网格后滚动/缩放仍然有效,所以仍然不确定为什么以其他方式执行它不起作用,但有时找到另一种方式是最好的答案:)