Greetings,

I am hoping someone can help with this simple problem. I am trying to toggle the visibility of the legend of a plot but nothing seems to work.

I have tried the following but nothing happens. The legend just remains visible:
Qt Code:
  1. plot.legend().setVisible(False)/plot.legend().setVisible(True)
  2.  
  3. plot.legend().hide()/plot.legend().show()
To copy to clipboard, switch view to plain text mode 

I have tried:
Qt Code:
  1. plot.legend().contentsWidget().setVisible(False)/plot.legend().contentsWidget().setVisible(True)
To copy to clipboard, switch view to plain text mode 

but this just hides the clickable legend items, the box of the legend is still visible which is not what I want. I would like the entire legend to appear or disappear.

I have also tried keeping the legend around in my class after I create it by going:
Qt Code:
  1. self.plotLegend=QwtLegend()
  2. #insert legend into plot here
To copy to clipboard, switch view to plain text mode 
Then, to make the legend “not visible” I remove it by calling
Qt Code:
  1. plot.insertLegend(None)
To copy to clipboard, switch view to plain text mode 

but then when I try to reinsert my legend to make it “visible” again:
Qt Code:
  1. plot.insertLegend(self.plotLegend, locationNotImportant)
To copy to clipboard, switch view to plain text mode 
I get an error stating the underlying C++ object has been deleted.

Is there seriously no way to do this without creating a new legend every time? This would seem to be so simple…

Any help is appreciated, thank you.

(Using PyQwt 5.1.0)