Using PyQt4 and matplotlib how can I link the extent (i.e. length) of a slider to the extent of a plot axis in the same window, such that the slider has the same length as the plot axis even when the window is rescaled?
Asked
Active
Viewed 80 times
1
-
This a Qt slider or a mpl.widgets slider? – tacaswell Apr 26 '16 at 01:48
-
@fen Did my answer solve your problem? – mfitzp May 21 '16 at 14:23
1 Answers
1
You can get the limits of an axis in a matplotlib
plot using ax.get_xlim()
or ax.get_ylim()
. These returns a tuple
containing minimum and maximum value for that axis in data coordinates. The advantage of using data coordinates is that they remain the same, no matter the scaling of the plot.
Helpfully Qt auto-scales the range of a QSlider
to put the min/max values at the limit of the slider. Therefore, you can simply use the axis limit values to set the range for the QSlider
to get a 1:1 relationship between the position of the slider and the position on the axis.
xlims = ax.get_xlim() # e.g. (0, 200)
slider = QSlider()
slider.setRange(*xlims) # unpack the tuple into setRange
# alternatively slider.setRange(xlims[0], xlims[1])

mfitzp
- 15,275
- 7
- 50
- 70