Dr. Fabien Tarrade
Dr. Fabien Tarrade

Reputation: 1696

How to update matpplotlib lib plots in a python loop with the plot sitting in the same place in a Jupyter notebook?

I am using python 3.6.7, matplotlib 2.2.2, ipython 7.0.1, notebook 5.6.0 and jupyter 1.0.0.

I would like create 10 plots displayed on 2 lines that will stay in the same place on the jupyter notebook during the loop and new results will be display every 10 iteration in the same 10 plots. Here some dummy code for illustration only.

import matplotlib.pyplot as plt
fig, ax = plt.subplots(2, 5, sharex='col', sharey='row', figsize=(40,15))
ax = ax.flatten()
for i in range(TRAIN_STEPS):
    if i%10 == 0:
        for j in range(10):
            ax[j].set_title(j+i,fontsize=40)
            ax[j].axes.get_xaxis().set_visible(False)
            ax[j].axes.get_yaxis().set_visible(False)
            ax[j].imshow(weight[i]), cmap=plt.get_cmap('seismic'))
    if i%100 == 0:
         print('Step:' + str(i)) 

I created the title with i+j to better see what was updated. In this example, the set of plots are only showed at the end (not during the loop). Example 1

If I use plt.show(), then I only see the first set of plots Example 2

I don't want to save the plots at each set and do an animation later. Any idea if we can do that and how ? (I look at many things but I am missing something plt.ion() plt.show(block=False) plt.pause(1) time.sleep(0.1) f = plt.figure() f.canvas.update() f.canvas.flush_events() f.canvas.draw_idle()

Upvotes: 0

Views: 2108

Answers (2)

Guto Schettini
Guto Schettini

Reputation: 299

I'm doing something similar... inspecting data creating a plot in a loop and visualizing them each one at time. Your code help me to make it right!

In my case I make a new plot each time, and to avoid the 2nd figure at the end of the loop I used plt.close(). But in your example this will not allow to hold the previous plots. The workaround is to create the numbers and save them in a list to be plotted in a second loop.

%matplotlib inline
import time
import pylab as pl
from IPython import display

numbers = []
for i in range(3):
    
    numbers.append(pl.randn(10))
    for j in range(i+1):
        pl.plot(numbers[j]) 
        
    display.clear_output(wait=True) 
    display.display(pl.gcf())
    time.sleep(1.0)
    plt.close()

Upvotes: 2

Dr. Fabien Tarrade
Dr. Fabien Tarrade

Reputation: 1696

It seems that I find a way to do what I wanted by adding:

display.clear_output(wait=True)
display.display(plt.gcf())
time.sleep(1.0) 

Still 2 minor issues:

  • the info for each step is drop at each iteration and only the last entry is printed.
  • the set of 10 plots is display twice after the last iteration. It can be easily reproduce using the following code find in another thread

    %matplotlib inline
    import time
    import pylab as pl
    from IPython import display
    for i in range(10):
          pl.plot(pl.randn(100)) 
          display.clear_output(wait=True) 
          display.display(pl.gcf())
          time.sleep(1.0)
    

Upvotes: 0

Related Questions