Removing legend items

1,684 views
Skip to first unread message

Luke M

unread,
Jun 25, 2014, 8:52:24 AM6/25/14
to pyqt...@googlegroups.com
It looks like there is a bug when removing legend entries from anything but the last row of the legend. In the following example, everything works fine if you remove p3 and replace it, but if you remove p2, the new legend entry ('4') is placed on top of ('3'). Upon insepction of LegendItem.layout, it looks like when the items are removed from the layout, that row of the layout isn't actually removed, so ('3') stays in the 3rd row of the layout. Then when '4' is added, it attempts to add it to a row that already contains '3'.
 
This is where I hit a road block. I think what needs to happen is LegenItem.removeItem needs to move all items up when an intermediate row is removed. I looked into QGraphicsGridLayout and I couldn't figure a good way to do this. I'm hoping someone with more experience with these layouts can help.
 
import pyqtgraph as pg
import numpy as np

pw = pg.PlotWidget()
leg = pw.addLegend()
p1 = pw.plot(np.arange(10), name='1', pen=(1, 3))
p2 = pw.plot(np.arange(10)*2, name='2', pen=(2, 3))
p3 = pw.plot(np.arange(10)*3, name='3', pen=(3, 3))

pw.removeItem(p2)
pw.plotItem.legend.removeItem(p2.name())
# pw.removeItem(p3)
# leg.removeItem(p3.name())

p4 = pw.plot(np.arange(10) * 4, name='4', pen=(2,3))
pw.show()

Thanks,
Luke M

Morgan Cherioux

unread,
Jul 3, 2014, 7:15:06 AM7/3/14
to pyqt...@googlegroups.com
Hi,

I don't know if it can help but here is how I delete all items in the legend (found it on this forum and had to adjust a little) :

            self.graphicsRight.plotItem.legend.items = []
           
while self.graphicsRight.plotItem.legend.layout.count() > 0:
               
self.graphicsRight.plotItem.legend.layout.removeAt(0)

Note that graphicsRight is an instance of PlotWidget. I know it is not exactly what you want but maybe it can help you clearing your legend more properly ;)

Luke Campagnola

unread,
Jul 6, 2014, 11:05:28 AM7/6/14
to pyqt...@googlegroups.com
On Wed, Jun 25, 2014 at 8:52 AM, Luke M <lcj...@gmail.com> wrote:
It looks like there is a bug when removing legend entries from anything but the last row of the legend. In the following example, everything works fine if you remove p3 and replace it, but if you remove p2, the new legend entry ('4') is placed on top of ('3'). Upon insepction of LegendItem.layout, it looks like when the items are removed from the layout, that row of the layout isn't actually removed, so ('3') stays in the 3rd row of the layout. Then when '4' is added, it attempts to add it to a row that already contains '3'.

I think this is the expected behavior for QGraphicsLayout--items do not shift rows; rather, an empty row simply collapses.
So the bug is that LayoutItem is not selecting the correct row when it adds the next item. I have just pushed a fix for this:
Thanks for the report.

Luke M

unread,
Jul 7, 2014, 9:04:51 AM7/7/14
to pyqt...@googlegroups.com

On Sunday, July 6, 2014 11:05:28 AM UTC-4, Luke Campagnola wrote:

I think this is the expected behavior for QGraphicsLayout--items do not shift rows; rather, an empty row simply collapses.
So the bug is that LayoutItem is not selecting the correct row when it adds the next item. I have just pushed a fix for this:
Thanks for the report.
 
Perfect, thanks! 
Reply all
Reply to author
Forward
0 new messages