Fix axes range and axes aspect ratio

3,684 views
Skip to first unread message

Milan Vukov

unread,
Feb 14, 2014, 7:58:20 AM2/14/14
to pyqt...@googlegroups.com
Hello,

I would like to fix the axes of a plot to some values. I tried something like:

for col in xrange( 4 ):
l = ledLayout.getItem(0, col)
for row in xrange( 3 ):
a = l.getItem(row + 1, 0).getAxis( "left" )
a.setRange(-1, 1200)
a.linkToView(l.getItem(row + 1, 0).getViewBox())

but this does not work.  In this example, I have a grid layout, and my attempt was to fix y-axis to (-1, 1200). I would also like (expect) that auto-scale sets this range when it get's pressed.

Another question is about aspect ration of a plot -- e.g. no matter how I zoom in/out, that the ratio of axes stays the same. I would like to extend the above code so that both x- and y-axis have fixed range (-1, 1200).

I would appreciate any help. Thanx a lot in advance!

Regards,
Milan

Luke Campagnola

unread,
Feb 14, 2014, 8:10:26 AM2/14/14
to pyqt...@googlegroups.com
On Fri, Feb 14, 2014 at 7:58 AM, Milan Vukov <mvu...@gmail.com> wrote:
Hello,

I would like to fix the axes of a plot to some values. I tried something like:

for col in xrange( 4 ):
l = ledLayout.getItem(0, col)
for row in xrange( 3 ):
a = l.getItem(row + 1, 0).getAxis( "left" )
a.setRange(-1, 1200)
a.linkToView(l.getItem(row + 1, 0).getViewBox())

but this does not work.  In this example, I have a grid layout, and my attempt was to fix y-axis to (-1, 1200). I would also like (expect) that auto-scale sets this range when it get's pressed.

The AxisItems you are accessing automatically set their own range based on the range that is visible in the ViewBox, so set your desired range there using ViewBox.setXRange and setYRange. Also note that PlotItem wraps these methods from the ViewBox for convenience:

    plotItem.setXRange(-1, 1200)
    plotItem.setYRange(-1, 1200)

 

Another question is about aspect ration of a plot -- e.g. no matter how I zoom in/out, that the ratio of axes stays the same. I would like to extend the above code so that both x- and y-axis have fixed range (-1, 1200).

By default, ViewBox does not have a fixed aspect ratio, but if something requested a fixed ratio, then you can undo this with ViewBox.setAspectLocked(False). This method is also wrapped by PlotItem.


Luke

Milan Vukov

unread,
Feb 14, 2014, 11:18:11 AM2/14/14
to pyqt...@googlegroups.com
Hi Luke,

Thanx a lot for the answer! :) setXRange and setYRange solved one of the problems.  I tried also setAspectLocked( False ) but it did not help :( I called this method in an instance of PlotItem class. Maybe I was not so clear last time but my point was that if I set the same range for x and y axis, I would like to always see a square-sized plot on the screen no matter how the window that holds the plot is sized/shaped...

One another thing. Is it possible to configure the "A" button to scale to some predefined values? -- in my case the same values I set with setXRange and setYRange.... I tried to find something in the docs, but w/o success.

Cheers,
Milan

Milan Vukov

unread,
Feb 27, 2014, 10:29:34 AM2/27/14
to pyqt...@googlegroups.com
Hi,

Can you give me at least a hint how to solve two issues I mentioned in my last post? Thanx a lot in advance! :)

Milan


--
You received this message because you are subscribed to a topic in the Google Groups "pyqtgraph" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/pyqtgraph/sVcLiXJeUuE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to pyqtgraph+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyqtgraph/d60780a4-227e-4cd7-8673-50071560a19b%40googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.



--
Milan Vukov, PhD Student
KU Leuven, Department of Electrical Engineering (ESAT)
STADIUS Center for Dynamical Systems, Signal Processing and Data Analytics
Kasteelpark Arenberg 10, bus 2446, B-3001 Leuven-Heverlee, Belgium
e-mail: milan...@esat.kuleuven.be

Luke Campagnola

unread,
Feb 27, 2014, 11:18:16 AM2/27/14
to pyqt...@googlegroups.com
On Fri, Feb 14, 2014 at 11:18 AM, Milan Vukov <mvu...@gmail.com> wrote:
Hi Luke,

Thanx a lot for the answer! :) setXRange and setYRange solved one of the problems.  I tried also setAspectLocked( False ) but it did not help :( I called this method in an instance of PlotItem class. Maybe I was not so clear last time but my point was that if I set the same range for x and y axis, I would like to always see a square-sized plot on the screen no matter how the window that holds the plot is sized/shaped...

Sorry for delaying on this. I think I understand the question, but I'm not sure. You want to constrain the axes such that, no matter how you zoom or resize the image, (Xmax-Xmin) == (Ymax-Ymin) ?  I think the simplest way would be to override ViewBox.moseDragEvent such that, when dragging with the right mouse button, it always sees (dx == dy). Alternatively, you could disable zooming with the right mouse button and allow only zoom with the wheel, which I think already has the result you expect..
 

One another thing. Is it possible to configure the "A" button to scale to some predefined values? -- in my case the same values I set with setXRange and setYRange.... I tried to find something in the docs, but w/o success.

For this, just override PlotItem.autoBtnClicked() either by subclassing or monkey-patch. 

Milan Vukov

unread,
Mar 1, 2014, 7:16:44 AM3/1/14
to pyqt...@googlegroups.com
Hi Luke,


On Thu, Feb 27, 2014 at 5:18 PM, Luke Campagnola <luke.ca...@gmail.com> wrote:
On Fri, Feb 14, 2014 at 11:18 AM, Milan Vukov <mvu...@gmail.com> wrote:
Hi Luke,

Thanx a lot for the answer! :) setXRange and setYRange solved one of the problems.  I tried also setAspectLocked( False ) but it did not help :( I called this method in an instance of PlotItem class. Maybe I was not so clear last time but my point was that if I set the same range for x and y axis, I would like to always see a square-sized plot on the screen no matter how the window that holds the plot is sized/shaped...

Sorry for delaying on this. I think I understand the question, but I'm not sure. You want to constrain the axes such that, no matter how you zoom or resize the image, (Xmax-Xmin) == (Ymax-Ymin) ?  I think the simplest way would be to override ViewBox.moseDragEvent such that, when dragging with the right mouse button, it always sees (dx == dy). Alternatively, you could disable zooming with the right mouse button and allow only zoom with the wheel, which I think already has the result you expect..

Yes, the idea is that I always see a (physical) square on the screen. I will take a look at your suggestions :) Thanx a lot!
 
 

One another thing. Is it possible to configure the "A" button to scale to some predefined values? -- in my case the same values I set with setXRange and setYRange.... I tried to find something in the docs, but w/o success.

For this, just override PlotItem.autoBtnClicked() either by subclassing or monkey-patch. 

I did it this way, like you suggested:

```
class CustomPlotItem( pg.PlotItem ):
def autoBtnClicked( self ):
self.setXRange(0, 1200)
self.setYRange(0, 1200)
```

... and it works like a charm!

Regards,
Milan
 

--
You received this message because you are subscribed to a topic in the Google Groups "pyqtgraph" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/pyqtgraph/sVcLiXJeUuE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to pyqtgraph+...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.
Reply all
Reply to author
Forward
0 new messages