Skip to content Skip to sidebar Skip to footer

Specify Where In Output Matplotlib Figures Are Rendered In Jupyter Using Figure Handles

When working with matplotlib, a common pattern for me is to figure in a function, return it, and let the caller decide what t do with it (render to screen, save to file etc...). In

Solution 1:

figure.show() can only be used in interactive backends with an event loop already running.

In jupyter notebook with the inline backend, you have no event loop. However, similar to Matplotlib - sequence is off when using plt.imshow() and and this answer you can instead just do what the backend would also do, namely call display(figure).

cell 1

%matplotlib inline
import matplotlib.pyplot as plt
from IPython.display import display

defmake_figure(i):
    f = plt.figure(i)
    plt.plot([1,i,3])
    return f

cell 2

%%capturefs= [(i,make_figure(i)) for i in range(3)]

cell 3

for i, f in fs[::-1]:
    display(f)

enter image description here

Post a Comment for "Specify Where In Output Matplotlib Figures Are Rendered In Jupyter Using Figure Handles"