Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python pyqtgraph how to set x and y axis limits on graph, no autorange

I would like to know how I can set the x and y axis limits that are displayed for a pyqtgraph.GraphicsWindow.addPlot object. I need to display a lot of data inside a loop (hence using pyqtgraph) but I would rather preallocate my axes as opposed to allowing autorange to potentially enhance speed. As an example,

from pyqtgraph.Qt import QtGui, QtCore
import numpy as np
import pyqtgraph as pg
app = QtGui.QApplication([])
win = pg.GraphicsWindow(title="My plotting examples")
win.resize(1000,600)
win.setWindowTitle('pyqtgraph example: Plotting')
p1 = win.addPlot(title="plot1")
p2 = win.addPlot(title="plot2")
curve1 = p1.plot(pen='y')
curve2 = p1.plot(pen='r')
curve3 = p2.plot(pen='b')
x = np.linspace(0,10,1000)
x_current = x[0]
p1.setXRange((5,20), padding=0)
for i in range(1,len(x)):
    x_current = np.append(x_current,x[i])
    curve1.setData(x_current,np.sin(x_current))
    curve2.setData(x_current,np.cos(x_current))
    curve3.setData(x_current,np.tan(x_current))
    app.processEvents()

if __name__ == '__main__':
    import sys
    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
        QtGui.QApplication.instance().exec_()

The problem lies in the line p1.setXRange((5,20),padding=0). This results in the error: TypeError: setXRange() takes at least 3 arguments (3 given)

I think this should be a very simple question, just setting the axis ranges before plotting.

like image 436
Michael Avatar asked Apr 13 '15 05:04

Michael


3 Answers

As the error message indicates, you have not supplied the correct number of arguments to setXRange(). The correct line should look like:

p1.setXRange(5, 20, padding=0)

Documentation here.

like image 94
Luke Avatar answered Nov 15 '22 13:11

Luke


Michael.

The easiest way would be to simply specify your ranges as elements of a list. Replace the p1.setXRange with something like this:

p1.setRange(xRange=[5,20])

And that's it!

Check the documentation of this class in: http://www.pyqtgraph.org/documentation/graphicsItems/viewbox.html#pyqtgraph.ViewBox.setRange

like image 41
Mario García Avatar answered Nov 15 '22 12:11

Mario García


The questions asks how to set a limit to the view which none of the responses to this point answers. For others who come across this the view of the plot can be limited to a section of the axis like this:

p1.plotItem.vb.setLimits(xMin=a, xMax=b, yMin=c, yMax=d)

Documentation: http://www.pyqtgraph.org/documentation/graphicsItems/viewbox.html

like image 40
Andrew R Derringer Avatar answered Nov 15 '22 12:11

Andrew R Derringer