What is the recommended way of using pyqtgraph.GraphicsView

2,169 views
Skip to first unread message

Papee

unread,
Nov 1, 2012, 12:23:36 PM11/1/12
to pyqt...@googlegroups.com

Hi,

I have gone through the Examples coming with pyqtgraph, but I didn’t get the idea of getting a simple plot using pyqtgraph.GraphicsView For two days I am sitting with this code without any result. Please help. This is the code:

#! /usr/bin/python

 

import sys

from PyQt4 import QtGui

from PyQt4 import QtCore

import numpy as np

import pyqtgraph as pg

 

 

class GraphDisplay(QtGui.QMainWindow):

 

    def __init__( self , parent =None):

        QtGui.QMainWindow.__init__ (self , parent)

        self.setGeometry( 200, 100 , 800, 300 )

 

        self.dataPlot = np.cos(np.linspace(0, 2*np.pi , 1000))

 

       

        self.view = pg.GraphicsView ()

        self.graphpaper = pg.ViewBox()       

        self.view.setCentralWidget(self.graphpaper)       

        self.graph = pg.PlotItem()

        self.graph.plot( self.dataPlot)       

        self.graphpaper.addItem(self.graph)      

 

 

def ShowWindow():

    app = QtGui.QApplication(sys.argv)

    qb = GraphDisplay()

    qb.show()

    sys.exit(app.exec_())

 

if __name__ == '__main__':

    ShowWindow()

Also please show me the recommended way of using pyqtgraph.GraphicsView

Luke Campagnola

unread,
Nov 1, 2012, 12:53:55 PM11/1/12
to pyqt...@googlegroups.com
Perhaps this page will help:

Importantly:
  1. ViewBox is used for displaying data (plots, images, anything that is expressed in the coordinate system of your data). It's primary feature is to allow the user to zoom / pan with the mouse to look at different parts of the data coordinate system.
  2. PlotItem is basically just a combination of a ViewBox and AxisItems. It is primarily used for displaying line and scatter plots.

In your code, you have placed a PlotItem inside another ViewBox, which doesn't really make sense in this context.
Try removing the ViewBox and make the PlotItem the central widget of the GraphicsView:

        self.view = pg.GraphicsView ()

        self.graphpaper = pg.PlotItem()       

        self.view.setCentralWidget(self.graphpaper)

        self.graphpaper.plot( self.dataPlot)       



Luke



-- [ You are subscribed to pyqt...@googlegroups.com. To unsubscribe, send email to pyqtgraph+...@googlegroups.com ]

Papee

unread,
Nov 1, 2012, 2:22:54 PM11/1/12
to pyqt...@googlegroups.com

Thanks a million Luke,
      You separated the cream from milk. You just changed my understanding of Pyqtgraph. Sorry to say your code didn’t worked for me. As pointed out by you I sat few minutes with the link you have given and changed my code. This is my new code ( I commented the code if someone else want to know how this works ). Luke, please review my comment, and if something wrong with my understanding please correct me.

 

#! /usr/bin/python

 

import sys

from PyQt4 import QtGui

from PyQt4 import QtCore

import numpy as np

import pyqtgraph as pg

 

 

class GraphDisplay(QtGui.QMainWindow):

 

    def __init__( self , parent =None):

        QtGui.QMainWindow.__init__ (self , parent)

        self.setGeometry( QtCore.QRect(200, 100 , 1000, 300 ))

 

       

        self.dataPlot = np.cos(np.linspace(0, 5 *np.pi , 1000))

 

        #--------------------------------------------------- Create a plot item;

        #-------- This is essentially your graph paper where your graph is drawn

        self.graph = pg.PlotItem()

       

        #------------- Put your data inside PlotItem to get your actual plotting

        self.graph.plot( self.dataPlot)

       

        #------------------------- You are going to show your graph paper inside

        #---------------------------------- the GraphicsView widget of Pyqtgraph

        self.view = pg.GraphicsView()

       

        #--------------------------- if you are going to use multiple plot items

        #--------- in the same graphics view get layout and add items one by one

        #--------- for that uncomment bellow codes (1) ,(2) ,(3) and comment (4)

        #---------- self.lay  = pg.GraphicsLayout()                     #----(1)

        #---------- self.lay.addItem(self.graph)                        #----(2)

        #---------- self.view.setCentralItem(self.lay)                  #----(3)

 

        #-------------------------------- If you display only one graph use this

        self.view.setCentralItem(self.graph)                #----(4)

       

        #---------------------------------------- Since we are using QMainWindow

        #--------------------------- put our view as CentralWidget in MainWindow

        self.setCentralWidget(self.view)

       

 

def ShowWindow():

    app = QtGui.QApplication(sys.argv)

    qb = GraphDisplay()

    qb.show()

    sys.exit(app.exec_())

 

if __name__ == '__main__':

    ShowWindow()

 

One more question (not directly connected to pyqtgraph), when using eclipse for editing code imported from pyqtgraph it is not showing certain attributes/methods like pg.PlotItem() or pg.ViewBox() on ‘auto complete’ but it is showing other attribute names like pg.GraphicsView() from the package correctly. Any idea ?

Luke Campagnola

unread,
Nov 1, 2012, 5:19:15 PM11/1/12
to pyqt...@googlegroups.com
Your comments are quite correct. Thanks for writing that--it actually helps me to understand how people perceive the pyqtgraph API so I can write better documentation. 

  

One more question (not directly connected to pyqtgraph), when using eclipse for editing code imported from pyqtgraph it is not showing certain attributes/methods like pg.PlotItem() or pg.ViewBox() on ‘auto complete’ but it is showing other attribute names like pg.GraphicsView() from the package correctly. Any idea ?


No idea. I have not used Eclipse much, but I have seen the same problem. It is very odd that it would pick up QWidgets and not QGraphicsItems, since these are all imported into the pyqtgraph namespace by the same mechanism.

 
Luke

Papee

unread,
Nov 5, 2012, 1:02:55 PM11/5/12
to pyqt...@googlegroups.com

Regarding the eclipse problem I found out the solution. If importing pygtgraph still shows unavailable module error, then goto Eclipse -> Preferences -> PyDev -> Interpreter – Python

click the "Forced Builtins" tab, click "New...", type "pyqtgraph", and click "OK".

Now it should show all the modules from pyqtgraph

Courtesy: Alex Bigelow [http://alexstroubleshooting.blogspot.in/2012/05/pyside-in-eclipse.html]

Reply all
Reply to author
Forward
0 new messages