Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: In pyqtgraph, how to obtain the axes range of a PlotWidget

I'm working with the plot of pyqtgraph. There is a built-in function viewRange() returns the axes range of the view box, rather than the plot itself. Is there any function or variables store the axes range of the plot?

like image 981
lwangreen Avatar asked Oct 26 '25 08:10

lwangreen


2 Answers

Note that the range will give you the current view limits (e.g. if you are zoomed), you can get both the current view range and full x/y axis limits with:

plot.getViewBox().state

which is a dict with quite a few useful parameters.

like image 76
Joseph Avatar answered Oct 28 '25 22:10

Joseph


You can access to axes range from AxisItem. Here is sample code.

import sys
from pyqtgraph.Qt import QtGui #from PyQt4 import QtGui
import pyqtgraph

# make window
app = QtGui.QApplication(sys.argv) # construct a QApplication (must).

mw = QtGui.QMainWindow()
mw.resize(800,800)
mw.setWindowTitle('my qt window')

# make view
view = pyqtgraph.GraphicsLayoutWidget()
# view.show()
mw.setCentralWidget(view)
mw.show()

# make plot area
w1 = view.addPlot(title = "my plot area")

# plot data
# mkPen(color,width,name)
w1.addLegend(offset=(0, 0))
w1.plot([0,1,2,3,4],[3,6,5,8,7],pen=pyqtgraph.mkPen(1, width=1),name="foo")
w1.plot([0,1,2,3,4],[5,7,6,2,9],pen=pyqtgraph.mkPen(2, width=1),name="bar")

axX = w1.getAxis('bottom')
print('x axis range: {}'.format(axX.range)) # <------- get range of x axis
axY = w1.getAxis('left')
print('x axis range: {}'.format(axY.range)) # <------- get range of y axis

sys.exit(app.exec_())
like image 20
rkoyama1623 Avatar answered Oct 28 '25 20:10

rkoyama1623



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!