Python 在pyqtgraph中使用AxisItem打开栅格会导致轴缩放中断
我对这个项目有困难。一旦我同时启用x和y轴网,x轴就无法通过缩放/平移功能进行缩放。有什么想法吗Python 在pyqtgraph中使用AxisItem打开栅格会导致轴缩放中断,python,pyqt,pyqtgraph,Python,Pyqt,Pyqtgraph,我对这个项目有困难。一旦我同时启用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):
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 examples文件夹包含一个“GraphItem.py”示例,该示例仅通过ViewBox向窗口添加和显示GraphItem对象。但是,它们不使用网格,因此如果要使用带有GraphItem的网格,只需先添加一个PlotItem(它已经有一个关联的ViewBox…您猜到了,…网格的AxisItems!),。。。然后让ViewBox添加您的石墨。修改后的GraphItem.py如下所示(附带showGrid):
我对此进行了测试,启用网格后滚动/缩放仍然有效,因此仍然不确定为什么用其他方法不起作用,但有时找到另一种方法是最好的答案:)你能澄清你的最终意图吗?目前您没有显示任何内容,因此很难缩小范围。如果您使用setGrid命令,我可以确认即使在他们设计的示例(../examples/ViewBox.py)上也会出现这种情况。但是,我不确定您是否真的需要直接访问setGrid。例如,如果您最终要添加一个实际的绘图,那么PlotItem对象有一个showGrid方法,它可以正确地执行此操作,而不会把事情搞砸。我的最终目的是使用graphItem()显示大型数据集。非常感谢!我尝试了一些类似的方法,但无法使其工作,然后我让一个viewbox覆盖另一个,并遇到了更多的问题。
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_()