显示/隐藏情节的图例 [英] Show/hide a plot's legend
问题描述
我是python的新手,正在开发pyqt GUI.我想提供一个复选框选项来显示/隐藏情节的图例.有没有隐藏图例的方法?
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?
我尝试使用 pyplot 的 '_nolegend_
',它似乎适用于选择的图例条目,但如果应用于所有条目,则会创建 ValueError.
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.
对此表示感谢.
推荐答案
以下是您可以在命令行上尝试的内容:
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()
在GUI程序中,最好避免pyplot并使用面向对象的API,即 ax.legend
和 canvas.draw
.
In a GUI program it's best to avoid pyplot and use the object-oriented API, i.e., ax.legend
and canvas.draw
.
这篇关于显示/隐藏情节的图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!