How to switch matplotlib digits visibility?

Is there a way I can make the matplotlib shape disappear and reappear in response to some kind of event? (i.e. keystroke)

I tried using fig.set_visible(False), but for me it does nothing.

A simple code example:

import matplotlib
import matplotlib.pyplot as plt

fig=matplotlib.pyplot.figure(figsize=(10, 10))

# Some other code will go here

def toggle_plot():
  # This function is called by a keypress to hide/show the figure
  fig.set_visible(not fig.get_visible()) # This doesn't work for me

plt.show()

The reason I'm trying to do this is because I have a bunch of plots / animations on the diagram that show the result of the simulation, but displaying them all the time slows down my computer. Any ideas?

+3
source share
3 answers

You have to call plt.draw()to actually create any changes. This should work:

def toggle_plot():
  # This function is called by a keypress to hide/show the figure
  fig.set_visible(not fig.get_visible())
  plt.draw()
+2
source

matplotlib . set_visible get_visible(), . matplotlib AxesImage, Figure, . , .

0

You can use the Toplevel () widget from the tkinter library along with the matplotlib backend.

Here is a complete example:

from tkinter import *
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg

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().grid()
canvas.show()

import pdb; pdb.set_trace()

Vocation:

graph.withdraw()

hide the chart and:

graph.deiconify()

will display it again.

0
source

All Articles