2016-11-09 18 views
1

我遇到了AxisItem问题。只要打开x和y网格,x轴就不能再通过缩放/平移功能缩放。有任何想法吗?使用pyqtgraph中的AxisItem打开网格导致轴缩放中断

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

你能澄清你的最终目的是什么吗?目前你没有显示任何内容,所以它很难缩小范围,如果你使用setGrid命令,我可以确认这种情况发生在他们设计的例子(../examples/ViewBox.py)上。但我不确定,如果你实际上需要直接访问setGrid。例如,如果你最终会添加一个实际的绘图,那么PlotItem对象就有一个showGrid方法,它可以正确地完成这个任务,而不会把事情搞砸。 – segFaultCoder

+0

我的最终目的是使用graphItem() – user3047917

回答

1

看你最后的评论,可以考虑此选项:

的pyqtgraph示例文件夹中包含一个“GraphItem.py”例如这增加,并显示通过仅视框一个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_() 

我试用了滚动/缩放仍然使电网后的工作,所以仍然不知道为什么这样做它的其他方式没有按” T工作,但有时找到另一种方式是最好的答案:)

+0

显示大型数据集非常感谢!我尝试了类似的东西,但无法使其工作,然后我有一个viewbox覆盖另一个,并遇到更多问题。 – user3047917