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:

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

Popular posts from this blog

What is happening when Matlab is starting a "parallel pool"? -

angular - DownloadURL return null in below code -

php - Cannot override Laravel Spark authentication with own implementation -