Reputation: 145
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
i use this:
plt.ion()
fig = plt.figure(visible=False)
ax =fig.add_subplot(111)
Thanks in advance
Upvotes: 11
Views: 40279
Reputation: 496
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.
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.
Upvotes: 1
Reputation: 339795
There is a nice answer how to toggle visibility of a complete matplotlib figure in this question.
The idea is to use the .set_visible
method of the figure.
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()
Upvotes: 2
Reputation: 881017
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.
In a script, the drawing is usually delayed until plt.show
is called:
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(range(5))
plt.show() # now the figure is shown
Upvotes: 2
Reputation: 11
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):
#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()
Upvotes: 1