Autoscaling y-axis to *visible data

2,361 views
Skip to first unread message

Andy

unread,
Jul 3, 2018, 9:07:30 AM7/3/18
to pyqtgraph
I have a time series waveform plot made with pyqtgraph. The user should be able to zoom in and out to various points in time (x-axis), and the y-axis should automatically scale to whatever data is visible within the current range of x values. I believe that `enableAutoRange` and `setAutoVisible` should allow this, but it doesn't seem to work. I have unlocked the aspect ratio but this doesn't seem to help.

A minimal example is below, please could anyone suggest how to get this working? Ideally when you zoom in to the low-amplitude data on the right hand side, the y-axis should scale up so that the sine wave is visible. (run by pasting into python -i)

import numpy        
import pyqtgraph                                                                
                                                                                
x_data = numpy.linspace(0, 100, num=10000)                                      
y_data = 1000*numpy.sin(x_data)                                                 
y_data[5000:] = numpy.sin(x_data[5000:])                                        
                                                                                
plt = pyqtgraph.plot(x_data, y_data)                                            
plt.setMouseEnabled(x=True,y=False)                                             
                                                                                
vb = plt.getViewBox()                                                           
vb.setAspectLocked(lock=False)                                                  
vb.setAutoVisible(y=1.0)                                                        
vb.enableAutoRange(axis='y', enable=True)  

One interesting thing is that if you zoom to the right hand side, and then right click and select Y-Axis>Auto, it works as expected.

Luke Campagnola

unread,
Jul 3, 2018, 11:56:59 AM7/3/18
to pyqt...@googlegroups.com
The example you posted works as expected for me in version 0.10.0 and in the latest develop commit--if I zoom in on the right side, the plot is scaled to fill the full height of the view, without needing to manually set automatic scaling. Can you send the output of pg.systemInfo() ?

--
You received this message because you are subscribed to the Google Groups "pyqtgraph" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyqtgraph+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyqtgraph/3e6f5369-7a70-4d76-86c4-9acbf20ca35e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Andy

unread,
Jul 3, 2018, 12:34:23 PM7/3/18
to pyqtgraph
[andrew@arambol ~]$ python
Python 2.7.15 (default, May 16 2018, 17:50:09) 
[GCC 8.1.1 20180502 (Red Hat 8.1.1-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyqtgraph
>>> pyqtgraph.systemInfo()
sys.platform: linux2
sys.version: 2.7.15 (default, May 16 2018, 17:50:09) 
[GCC 8.1.1 20180502 (Red Hat 8.1.1-1)]
qt bindings: PyQt4 4.12.1 Qt 4.8.7
pyqtgraph: 0.10.0; None
config:
{'antialias': False,
 'background': 'k',
 'crashWarning': False,
 'editorCommand': None,
 'enableExperimental': False,
 'exitCleanup': True,
 'foreground': 'd',
 'imageAxisOrder': 'col-major',
 'leftButtonPan': True,
 'useOpenGL': False,
 'useWeave': False,
 'weaveDebug': False}

This is running on a fully updated Fedora 28 system.

Thanks
To unsubscribe from this group and stop receiving emails from it, send an email to pyqtgraph+...@googlegroups.com.

Andy

unread,
Jul 6, 2018, 5:08:56 AM7/6/18
to pyqtgraph
I would be happy to have a go at debugging this, but I'm not very familiar with the codebase. Do you have any suggestions about where to start? Thanks

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/GJSFLSXhxpE/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/158b08eb-0790-4192-9084-ece48d310f92%40googlegroups.com.

Luke Campagnola

unread,
Jul 7, 2018, 2:22:36 AM7/7/18
to pyqt...@googlegroups.com

But I am pretty surprised about this; your platform is almost identical to mine. Just to be super clear, here is what I did:

1. Start `python -i`
2. Paste your example code into the console
3. A plot window appears, with the y axis auto-scaled from -1000 to +1000
4. Use left mouse button to drag the view toward the left, until the x range is about 60 to 160
5. Now the y axis has auto-scaled to a new range from -1 to +1


To unsubscribe from this group and all its topics, send an email to pyqtgraph+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "pyqtgraph" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyqtgraph+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyqtgraph/CAOGhvDjXK-AZW%2B_B6XJOZrWG_WgPd8UDe1zr2vBE4qT%2BMYy%2BaA%40mail.gmail.com.

Andy

unread,
Jul 9, 2018, 7:49:43 AM7/9/18
to pyqt...@googlegroups.com
When I follow your steps, the y-axis scales correctly. However, if I zoom in to the low-amplitude region (using either two finger gestures or the scroll wheel), the y-axis does not rescale. Could you try it this way and see what the behaviour is for you?

Also, I've noticed that once I have done any type of zoom, the y-axis auto scaling is turned off, and therefore the autoscaling stops functioning even with your steps above. So perhaps this is the root cause of my problem. Is there a way to prevent mouse zooming from turning the autoscaling off in the dimensions where mouse zooming is not enabled?

To unsubscribe from this group and all its topics, send an email to pyqtgraph+...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "pyqtgraph" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyqtgraph+...@googlegroups.com.

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

--
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/GJSFLSXhxpE/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/CACZXET-ex3qktjVoy5nm8Qt4g_tW%3DVAL2yQfF2uFugjbL_iAbw%40mail.gmail.com.

Andy

unread,
Jul 9, 2018, 12:59:52 PM7/9/18
to pyqt...@googlegroups.com
Further to the above, I have been looking at the code and I believe the following is happening:
* User scrolls with mouse wheel
* wheelEvent calls scaleBy with a scaling array and centre coordinates
* scaleBy calculates whether each axis is affected by checking whether the scale value is None

However, I believe the scale array calculated by wheelEvent has the scaling value set to 1.0 for no scaling, rather than None. Therefore either:
* wheelEvent should replace scale values of 1.0 with None, or
* scaleBy should check if scale values are equal to 1.0 when deciding if the axis has been affected or not


Andy

unread,
Jul 9, 2018, 1:24:54 PM7/9/18
to pyqt...@googlegroups.com

Luke Campagnola

unread,
Jul 10, 2018, 9:02:16 AM7/10/18
to pyqt...@googlegroups.com
Got it--the mouse wheel handler was causing auto range to be disabled (whereas I only tested using left/right mouse drag). This is fixed in https://github.com/pyqtgraph/pyqtgraph/pull/721.

Thanks!

To unsubscribe from this group and all its topics, send an email to pyqtgraph+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "pyqtgraph" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyqtgraph+unsubscribe@googlegroups.com.

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

--
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/GJSFLSXhxpE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to pyqtgraph+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "pyqtgraph" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyqtgraph+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyqtgraph/CAOGhvDhoegwTEoz5eJWtN3LW9sL%3D-3Qn7R%2B1N8-ZGGxUoo_LJA%40mail.gmail.com.

Luke Campagnola

unread,
Jul 10, 2018, 9:11:07 AM7/10/18
to pyqt...@googlegroups.com
..and I just saw your basically identical PR 720
:D

Andy

unread,
Jul 10, 2018, 10:07:28 AM7/10/18
to pyqt...@googlegroups.com
Brilliant, thanks for your help and fix!

To unsubscribe from this group and all its topics, send an email to pyqtgraph+...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "pyqtgraph" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyqtgraph+...@googlegroups.com.

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

--
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/GJSFLSXhxpE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to pyqtgraph+...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "pyqtgraph" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyqtgraph+...@googlegroups.com.

--
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/GJSFLSXhxpE/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/CACZXET_7--RS5S4L96Fy5GMJi%2BoeNgp7xLrBgR1T%3DkAkjTohQw%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages