相关文章推荐
一身肌肉的荒野  ·  7. Validation, Data ...·  1 年前    · 
小胡子的跑步机  ·  es6 ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I am having trouble with AxisItem. As soon as I turn on both the x and y grid, the x-axis is no longer able to scale in and out with the zoom/pan function. Any ideas?

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_())
                Can you clarify what your ultimate intentions are?  Currently you're not displaying anything so it's sort of hard to narrow down, I can confirm this happens even on their contrived example (../examples/ViewBox.py) if you do the setGrid command.  I'm not sure however, if you actually need to access setGrid directly.  If for instance you ultimately are going to add an actual plot, the PlotItem object has a showGrid method which does this properly without screwing things up.
– segFaultCoder
                Nov 14, 2016 at 4:48

Looking at your last comment, consider this option:

The pyqtgraph examples folder contains a "GraphItem.py" example which adds and displays a GraphItem object to a window via a ViewBox only. They don't use a grid however, so if you want to use a grid with a GraphItem, just add a PlotItem first (which has an associated ViewBox already... and you guessed it,...AxisItems for a grid!),... then get the ViewBox to add your GraphItems. The modified GraphItem.py would look like this (with the accompanying 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_()

I tested this and the scroll/zooming still worked after enabling the grid, so still not sure why doing it the other way DOESN'T work, but sometimes finding another way is the best answer :)

Thank you so much! I tried something similar and could not get it to work and then I was having one viewbox override the other and running into a bunch more issues. – user3047917 Nov 16, 2016 at 19:49

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.