views:

130

answers:

1

I'm relatively new to python and am developing a pyqt GUI. I want to provide a checkbox option to show/hide a plot's legend. Is there a way to hide a legend?

I've tried using pyplot's '_nolegend_' and it appears to work on select legend entries but it creates a ValueError if applied to all entries.

I can brute force the legend to hide by clearing and redrawing the whole plot but... it's a terrible thing to do, especially with large data sets.

Appreciate any help with this.

+1  A: 

Here's something you can try on the command line:

plot([3,1,4,1],label='foo')
lgd=legend()

# when you want it to be invisible:
lgd.set_visible(False)
draw()

# when you want it to be visible:
lgd.set_visible(True)
draw()

In a GUI program it's best to avoid pyplot and use the object-oriented API, i.e., ax.legend and canvas.draw.

Jouni K. Seppänen