隐藏/隐形Matplotlib图

时间:2013-01-31 15:38:26

标签: python matplotlib

我有一个问题,不确定它是否困难,但我试图谷歌答案。没什么值得的。

我认为是全局的,可以在所有线程中访问。

但它出现在程序的开头,

我想在脚本的开头隐藏或使其不可见,然后在代码中的某一点使其可用或可见。

是否有任何Matplotlib可见False或其他

我用这个:

plt.ion()

fig = plt.figure(visible=False)

ax =fig.add_subplot(111)

提前致谢

4 个答案:

答案 0 :(得分:1)

理想情况下,避免在脚本中使用plt.ion()。它仅用于交互式会话,您希望立即查看matplotlib命令的结果。

在脚本中,绘图通常会延迟到调用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

答案 1 :(得分:1)

我必须做同样的事情,但是我首先使用这个过程将图形放在画布上(注意:这使用了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()

答案 2 :(得分:0)

如果你想隐藏整个绘图窗口,并且你正在使用tk后端 - 你可以使用tkinter库中的Toplevel()小部件。

以下是一个完整的例子:

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()

通话:

graph.withdraw()

将隐藏绘图窗口,并且:

graph.deiconify()

将再次显示。

答案 3 :(得分:0)

nice answer如何切换this question中完整matplotlib图的可见性。 我们的想法是使用图中的.set_visible方法。

这是一个完整的例子:

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()
相关问题