python - How to overlay plots from different cells? -
in 1 of cells in notebook, plotted with
myplot = plt.figure() plt.plot(x,y)
now, in different cell, i'd plot exact same figure again, add new plots on top of (similar happens 2 consecutive calls plt.plot()). tried adding following in new cell:
myplot plt.plot(xnew,ynew)
however, thing in new cell new plot, without former one.
how can 1 achieve this?
there 2 ways tackle this.
a. object-oriented approach
use object-oriented approach, i.e. keep handles figure and/or axes , reuse them in later cells.
import matplotlib.pyplot plt %matplotlib inline fig, ax=plt.subplots() ax.plot([1,2,3]) then in later cell,
ax.plot([4,5,6]) suggested reading:
how keep current figure when using ipython notebook %matplotlib inline?
how add plot commands figure in more 1 cell, display in end?
how show same matplotlib figure several times in single ipython notebook?
b. keep figure in pyplot
the other option tell matplotlib inline backend keep figures open @ end of cell.
import matplotlib.pyplot plt %matplotlib inline %config inlinebackend.close_figures=false # keep figures open in pyplot plt.plot([1,2,3]) then in later cell
plt.plot([4,5,6]) suggested reading:
Comments
Post a Comment