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() self.graphicsRight.plotItem.legend.items = []
while self.graphicsRight.plotItem.legend.layout.count() > 0:
self.graphicsRight.plotItem.legend.layout.removeAt(0)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.