Hide / Invisible Matplotlib Figure

I have a question, not sure if this is difficult or not, but I tried to answer Google. nothing worthy.

I have a global figure that can be accessed in all threads.

but he appears at the beginning of the program,

I want to hide or make it invisible when I run the script, and then at some point in the code make it accessible or visible.

Is there any matplotlib as visible False or something

i use this:

plt.ion()

fig = plt.figure(visible=False)

ax =fig.add_subplot(111)

Thanks in advance

+5
source share
4 answers

Ideally, avoid using plt.ion()in scripts. It is intended for use only in interactive sessions where you want to immediately see the result of matplotlib commands.

script , 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

, , , (: 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()
+1

, tk, Toplevel() tkinter.

:

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

.

0

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

All Articles