How to overlay plots from different cells?

There are essentially two ways to tackle this.

A. Object-oriented approach

Use the object-oriented approach, i.e. keep handles to the figure and/or axes and reuse them in later cells.

import matplotlib.pyplot as plt
%matplotlib inline

fig, ax=plt.subplots()
ax.plot([1,2,3])

Then in a later cell,

ax.plot([4,5,6])

Suggested reading:

B. Keep figure in pyplot

The other option is to tell the matplotlib inline backend to keep the figures open at the end of a cell.

import matplotlib.pyplot as plt
%matplotlib inline

%config InlineBackend.close_figures=False # keep figures open in pyplot

plt.plot([1,2,3])

Then in a later cell

plt.plot([4,5,6])

Suggested reading:

Leave a Comment