问题
I have a long running Python loop (used for machine learning), which periodically prints output and displays figures (using matplotlib). When run in Jupyter Notebook, all the text (stdout) is displayed in real-time, but the figures are all queued and not displayed until the entire loop is done.
I'd like to see the figures in real-time, on each iteration of the loop. During cell execution, not when the entire cell execution is done.
For example, if my code is:
for i in range(10):
print(i)
show_figure(FIG_i)
do_a_10_second_calculation()
I currently see:
0
1
2
...
9
FIG_0
FIG_1
...
FIG_9
What I'd like is:
0
FIG_0
1
FIG_1
2
FIG_2
...
Most importantly, I'd like to see the figures as they are calculated, as opposed to not seeing any figures on the screen until the entire loop is done.
回答1:
I suppose the problem lies in the part of the code you do not show here. Because it should work as expected. Making it runnable,
%matplotlib inline
import matplotlib.pyplot as plt
def do_a_1_second_calculation():
plt.pause(1)
def show_figure(i):
plt.figure(i)
plt.plot([1,i,3])
plt.show()
for i in range(10):
print(i)
show_figure(i)
do_a_1_second_calculation()
results in the desired outcome
回答2:
The display
function from IPython.display can be used to immediately flush a figure to cell output. Assuming that FIG_i
in your code is an actual Matplotlib figure object, you can just replace show_figure(FIG_i)
with display(FIG_i)
and the figures will output in real time.
Here's a complete example of display
in action:
from matplotlib import pyplot as plt
import numpy as np
from IPython.display import display
from time import sleep
for eps in range(0, 11, 5):
data = np.random.randint(eps, eps+10, size=(2,10))
fig = plt.figure()
ax = fig.gca()
ax.plot(*data)
print('eps %f' % eps)
display(fig)
plt.close() # .close prevents the normal figure display at end of cell execution
sleep(2)
print('slept 2 sec')
Here's a screenshot of the output:
来源:https://stackoverflow.com/questions/53450189/get-jupyter-notebook-to-display-matplotlib-figures-in-real-time