Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spyder interactive plot : wait for the plot to be closed to continue

I'm working with windows using Spyder, I plot with matplotlib. My problem is that I want to do interactive plot (or sometimes plotting a lot of things) and I want spyder to wait that I close the figure to continue the code (same way as a traditional terminal would).

I tried plt.ion(), %mpl TkAgg before loading matplotlib, Ipython and python console... And I can't find any solution.

If you want an example, the goal is that the "hello" prints only when I close the figure, with Spyder on windows 10.

import matplotlib.pyplot as plt
plt.figure('Close me to get hello')
plt.plot(0,0,'*')
plt.show()

print("hello")
like image 480
DaluS Avatar asked Oct 23 '25 09:10

DaluS


1 Answers

You need to deactivate Spyder's Matplotlib support by going to

Tools > Preferences > IPython console > Graphics

and deselecting the option called

Activate support

Then you need to change your code like this

import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
plt.figure('Close me to get hello')
plt.plot(0,0,'*')
plt.show()

print("hello")

to set your backend (TkAgg in this case) by hand before creating your plot.

like image 182
Carlos Cordoba Avatar answered Oct 26 '25 14:10

Carlos Cordoba