Hi,

Hi,

You can remove the legend(s) altogether with something like:
# Remove legends in your list
for l in self.l:
    plt.scene().removeItem(l)
    self.l.removeItem(l)

# or indiscriminately remove anything that looks like a legend...
 
for item in plt.childItems():
    if isinstance(item, pg.LegendItem):
        plt.scene().removeItem(item)

Also, I can see another problem you may have. If you are keeping a list of 
legend items (with self.l = [] and self.l.addItem(MyItemSample(c1), s[4]) 
etc.), but are then are doing self.l = pg.LegendItem((100, 60), offset=(70, 
30)), then you're clobbering your list. Any items inside the list and added 
to the scene will still exist, but you've lost the references to them, so 
you won't be able to remove them. Try something like
l = pg.LegendItem((100, 60), offset=(70, 30))
l.setParentItem(plotCanv.graphicsItem())
# ...
self.l.append(l)

Patrick




On Saturday, 11 May 2019 09:24:29 UTC+9:30, Jaime Heiss wrote:
>
> Hi again.
> Actually I'm not being able to delete the legend. It works fine as long as 
> I don't add additional legends. If the number of legends increase, they 
> stay there forever.
>
> I have a qt designer GUI and  when I add more signals to be plotted, I get 
> a large legend box that never shrinks back.
> I'm rather new to this whole PyQt thing, so sorry if I am making obvious 
> mistakes.
> Since the plotting routine is been called very frequently, I defined the L 
> object in the init as self.l=[]
> Then, within my updateplot(self) method I have the following:
>
> self.l = pg.LegendItem((100, 60), offset=(70, 30))  # args are (size, offset)
> self.l.setParentItem(plotCanv.graphicsItem())  # Note we do NOT call 
> plt.addItem in this case
> # Crude patch of the legend paint() method
> self.l.paint = types.MethodType(paint, self.l)
> # Fix the spacing between legend symbol and text.
> # This is probably needs to be added to the __init__() of LegendItem...
> self.l.layout.setHorizontalSpacing(20)
>
>
> and then for plotting on my plotCanv graphicsView I do
>
> plotCanv.plot(s[2][p0:pf],s[3][p0:pf],
>               pen=(i,len(self.sig2p)),connect="finite")
>
> and add the respective legend, which I try to store in self.addedplots 
> list so I can delete it latter.
>
>
> c1 = plotCanv.plot([1,3,2,4], pen=(i,len(self.sig2p)),
>                    fillLevel=100, fillBrush=(255,255,255),name=s[4])
> self.l.addItem(MyItemSample(c1), s[4])
> self.addedplots.append(c1)
>
>
>
>
> So, before doing any of this plotting I am trying to delete the previous 
> stuff with this:
>
> plotCanv.clear()
>
> for sample in self.addedplots:
>     self.l.removeItem(sample)
>
>
> But it's not working as prior legends just stay there, allthough the 
> previous plot gets deleted.
> What I need (if I can't just remove the legends) is to completely wipe out 
> or restart the whole graphicsview widget.
>
> Any advice will be appreciated.
>
> Thanks!
>
>
>
> On Tuesday, April 23, 2019 at 8:37:18 PM UTC-7, Patrick wrote:
>>
>> Hi,
>>
>> Glad it is of some use to you! Items can be removed from the legend in 
>> two ways:
>> # Remove by label
>> l.removeItem("my red plot")
>> # Remove by reference to plot item
>> l.removeItem(c2)
>>
>>
>> There isn't a l.clear() method or similar, which would be nice, but 
>> keeping with the theme of this thread, here's a hacky one-liner to do that:
>> # Remove all items
>> [ l.removeItem(sample.item) for sample, label in list(l.items) ]
>>
>> Patrick
>>
>> On Wednesday, 24 April 2019 09:22:58 UTC+9:30, Jaime Heiss wrote:
>>>
>>> Thank you so much Patrick.
>>> This is almost perfect!
>>> I just have one stupid question:
>>> How do I clear the legend every time I clear the plot?
>>>
>>> Thanks!
>>>
>>> On Saturday, April 20, 2019 at 12:25:53 PM UTC-7, Jaime Heiss wrote:
>>>>
>>>> Hello.
>>>> Is there any updated documentation about legend handling?
>>>> I need to remove the box, change background color and make thicker 
>>>> lines as well as horizontal lines instead of the diagonals. If anybody 
>>>> could post an example, I will appreciate it. 
>>>>
>>>> Thanks.
>>>>
>>>>

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/pyqtgraph/a994b5c3-6da6-4718-9afb-bc882d691809%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to