Python 隐藏/不可见的 Matplotlib 图形
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14629438/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hide / Invisible Matplotlib figure
提问by Hasan
I have a question, not sure if its difficult or not, but i tried to google the answer. nothing worthy.
我有一个问题,不确定它是否困难,但我试着用谷歌搜索答案。没什么值得的。
I have figure as global, which can be accessed in all threads.
我把图设为全局,可以在所有线程中访问。
but it appears in the beginning of the program,
但它出现在程序的开头,
I want to hide or making it invisible in the starting of the script then at one point in the code make it available or visible.
我想在脚本开始时隐藏或使其不可见,然后在代码中的某一点使其可用或可见。
Is there is any Matplotlib like visible False or something
是否有任何 Matplotlib 之类的可见 False 之类的
i use this:
我用这个:
plt.ion()
fig = plt.figure(visible=False)
ax =fig.add_subplot(111)
Thanks in advance
提前致谢
回答by unutbu
Ideally, avoid using plt.ion()in scripts. It is meant to be used only in interactive sessions, where you want to see the result of matplotlib commands immediately.
理想情况下,避免plt.ion()在脚本中使用。它仅用于交互式会话,您希望在其中立即查看 matplotlib 命令的结果。
In a script, the drawing is usually delayed until plt.showis called:
在脚本中,绘图通常会延迟到plt.show调用:
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(range(5))
plt.show() # now the figure is shown
回答by Brandon Boyce
I have to do the same thing that you're asking for, but I put the figure on a canvas first using this process (NOTE: this uses matplotlib, pyplot, and wxPython):
我必须做你要求的同样的事情,但我首先使用这个过程将图形放在画布上(注意:这使用 matplotlib、pyplot 和 wxPython):
#Define import and simplify how things are called
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
import matplotlib.pyplot as plt
#Make a panel
panel = wx.Panel(self)
#Make a figure
self.figure = plt.figure("Name")
#The "Name" is not necessary, but can be useful if you have lots of plots
#Make a canvas
self.canvas = FigureCanvas(panel, -1, self.figure)
#Then use
self.canvas.Show(True)
#or
self.canvas.Show(False)
#You can also check the state of the canvas using (probably with an if statement)
self.canvas.IsShown()
回答by Tjaart
In case you want to hide the entire plot window, and you are using the tk backend - you can use the Toplevel() widget from the tkinter library.
如果您想隐藏整个绘图窗口,并且您正在使用 tk 后端 - 您可以使用 tkinter 库中的 Toplevel() 小部件。
Here is a full example:
这是一个完整的例子:
from tkinter import *
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
fig,(ax) = plt.subplots()
x = np.linspace(0, 2 * np.pi)
y = np.transpose([np.sin(x)])
ax.plot(y)
graph = Toplevel()
canvas = FigureCanvasTkAgg(fig, master=graph)
canvas.get_tk_widget().pack()
canvas.show()
toolbar = NavigationToolbar2TkAgg(canvas, graph)
toolbar.update()
Calling:
调用:
graph.withdraw()
will hide the plot window, and:
将隐藏绘图窗口,并且:
graph.deiconify()
will display it again.
将再次显示它。
回答by ImportanceOfBeingErnest
There is a nice answerhow to toggle visibility of a complete matplotlib figure in this question.
The idea is to use the .set_visiblemethod of the figure.
在这个问题中如何切换完整 matplotlib 图的可见性有一个很好的答案。思路是用图的方法。.set_visible
Here a complete example:
这里有一个完整的例子:
import matplotlib.pyplot as plt
plt.scatter([1,2,3], [2,3,1], s=40)
def toggle_plot(event):
# This function is called by a keypress to hide/show the figure
plt.gcf().set_visible(not plt.gcf().get_visible())
plt.draw()
cid = plt.gcf().canvas.mpl_connect("key_press_event", toggle_plot)
plt.show()

