Remove additional axis/viewbox

1,086 views
Skip to first unread message

David Verstraeten

unread,
Dec 27, 2014, 11:04:25 AM12/27/14
to pyqt...@googlegroups.com

Hi,

thanks a lot for the great work on PyQTGraph - I was looking for an alternative to matplotlib that provides better interactivity and this fits the bill perfectly.

I am currently stuck on an issue I can't seem to find any information on in the docs or online. I would like to be able to dynamically add and remove Axes. The idea is that I can plot different datasets, each with a different Y-range and corresponding Y axis on the right hand side. If a dataset is removed, the corresponding Y axis should also disappear.

Starting from the MultiplePlotAxis.py, I brought it down to a minimal example of what I suspected should work, but for some reason doesn't:

import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtGui

pg.mkQApp()

pw = pg.PlotWidget()
pw.show()
p1 = pw.plotItem

# Second Viewbox and axis
p2 = pg.ViewBox()
ax2 = pg.AxisItem('right')
ax2.setLabel('Axis 2')
p1.layout.addItem(ax2, 2, 4)
p1.scene().addItem(p2)

# Plot some data
p1.plot([1, 2, 4, 8, 16, 32])
p2.addItem(pg.PlotCurveItem([3200, 1600, 800, 400, 200, 100], pen='r'))

# Try to remove them again
p1.layout.removeItem(ax2)
p1.scene().removeItem(p2)

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


As you can see in the screenshot attached, the axis is still visible in the upper left corner of the window. 

Any idea on how I can delete this axis completely? Or, alternatively, is there a way to 'reset' the PlotWidget itself and re-add all the axes? I cannot simply delete and recreate it because in the actual application it is part of a Qt UI created in QT Designer.
Screen Shot 2014-12-27 at 17.01.15.png

Peter Maunz

unread,
Dec 27, 2014, 5:28:53 PM12/27/14
to pyqt...@googlegroups.com
Hi David,

Please see my very recent post custom axisItems bug in 0.9.10.
I had the same problem and fixed it by changing one line in the pyqtgraph code.


--Peter

Peter Maunz

unread,
Dec 27, 2014, 6:15:12 PM12/27/14
to pyqt...@googlegroups.com
Sorry for not reading your post carefully. The suggested won't help.


David Verstraeten

unread,
Dec 28, 2014, 6:02:26 AM12/28/14
to pyqt...@googlegroups.com
No problem - thanks for replying anyway!

David

On 28 Dec 2014, at 00:15, Peter Maunz <peter...@gmail.com> wrote:

Sorry for not reading your post carefully. The suggested won't help.



--
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/3ZrIeqIqve4/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/5ba8b265-58c6-40c0-8983-338dde91baec%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Luke Campagnola

unread,
Dec 28, 2014, 9:41:56 AM12/28/14
to pyqt...@googlegroups.com
On Sat, Dec 27, 2014 at 11:04 AM, David Verstraeten <david.ve...@gmail.com> wrote:
Any idea on how I can delete this axis completely?

You just need to add `p1.scene().removeItem(ax2)`. Removing the axis from its layout is necessary but still leaves the axis inside the scene.

 
Or, alternatively, is there a way to 'reset' the PlotWidget itself and re-add all the axes? I cannot simply delete and recreate it because in the actual application it is part of a Qt UI created in QT Designer.

You should still be able to swap out the widget even if you are using designer (although ideally you should not need to). It would look something like:

    ui.pw.setParent(None)
    ui.pw = pg.PlotWidget()
    ui.someLayout.addWidget(ui.pw, ...)

David Verstraeten

unread,
Dec 28, 2014, 10:34:10 AM12/28/14
to pyqt...@googlegroups.com
Yep, that does it! Thanks a lot for your reply.

David

--
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/3ZrIeqIqve4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to pyqtgraph+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages