Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reuse plot in next jupyter cell [duplicate]

I have a jupyter notebook and wish to create a plot in one cell, then write some markdown to explain it in the next, then set the limits and plot again in the next. This is my code so far:

# %%
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 2 * np.pi)
y = np.sin(x ** 2)

plt.plot(x, y);

# %%
Some markdown text to explain what's going on before we zoom in on the interesting bit

# %%
plt.xlim(xmax=2);

The start of each cell is marked # %% above. The third cell shows an empty figure.

I'm aware of plt.subplots(2) to plot 2 plots from one cell but this does not let me have markdown between the plots.

Thanks in advance for any help.

like image 473
blokeley Avatar asked Jan 08 '17 20:01

blokeley


People also ask

How do you rerun a cell in Jupyter?

In Jupyter, click in each of the cells you want to re-run after error and go to View > Cell Toolbar > Tags. Type 'raises-exception' (no quotes) in the box at the top of the cell and Add Tag. Then select Cell > Run All. This should catch the errors and run all the cells on an infinite loop until interrupted.

What is %% capture in Jupyter?

Capturing Output With %%capture IPython has a cell magic, %%capture , which captures the stdout/stderr of a cell. With this magic you can discard these streams or store them in a variable.

How do I copy multiple cells from one Jupyter notebook to another?

Copying and pasting cells BETWEEN notebooks: Notebook 1: — Select multiple cells by holding down Shift and hit Ctrl+c to copy. Notebook 2: — Hit Esc to enter Command mode Ctrl + v to paste.


2 Answers

This answer to a similar question says you can reuse your axes and figure from a previous cell. It seems that if you just have figure as the last element in the cell it will re-display its graph:

# %%
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 2 * np.pi)
y = np.sin(x ** 2)

fig, ax = plt.subplots()
ax.plot(x, y);
fig  # This will show the plot in this cell, if you want.

# %%
Some markdown text to explain what's going on before we zoom in on the interesting bit

# %%
ax.xlim(xmax=2);  # By reusing `ax`, we keep editing the same plot.
fig               # This will show the now-zoomed-in figure in this cell.
like image 102
NHDaly Avatar answered Oct 01 '22 02:10

NHDaly


Easiest thing I can think of is to extract the plotting into a function that you can call twice. On the 2nd call you can then also call plt.xlim to zoom in. So something like (using you %% notation for new cells):

# %%
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np

# %%
def make_plot():
    x = np.linspace(0, 2 * np.pi)
    y = np.sin(x ** 2)
    plt.plot(x, y);

make_plot()

# %%
Some markdown text to explain what's going on before we zoom in on the interesting bit

# %%
make_plot()
plt.xlim(xmax=2)
like image 21
Oliver Dain Avatar answered Oct 01 '22 02:10

Oliver Dain