python - Redisplaying modified plot in subsequent IPython notebook cells -


i creating demo using ipython notebook. launch notebook in pylab inline mode, e.g. ipython notebook --pylab=inline, , progressively build plot, modifying aspects of plot in subsequent cells, , having chart redisplay after each modification. instance, have consecutive cells,

cell 1:

from pandas.io.data import datareader datetime import datetime import matplotlib.pyplot plt  goog = datareader("goog",  "yahoo", datetime(2000,1,1), datetime(2012,1,1)) close_vals = goog['close'] plot(close_vals.index, close_vals.values)  chart displayed inline 

cell 2:

xlim(datetime(2009,1,1), datetime(2010,1,1))  modified chart displayed inline 

however, original chart doesn't seem make it's way subsequent cells, , chart displayed in cell 2 empty. in order see original plot modification, have re-issue plot command,

cell 2:

plot(close_vals.index, close_vals.values) xlim(datetime(2009,1,1), datetime(2010,1,1)) 

this gets clunky , inelegant add moving average trend lines , labels. also, working ipython console, method of progressively building plot works fine. know of better way create kind of demo in notebook? thanks.

update:

my final code ended looking this.

cell 1:

from pandas.io.data import datareader datetime import datetime import matplotlib.pyplot plt  goog = datareader("goog",  "yahoo", datetime(2000,1,1), datetime(2012,1,1)) close_vals = goog['close'] fig, ax = subplots(1,1) ax.plot(close_vals.index, close_vals.values,label='goog stock price') 

cell 2:

ax.set_xlim(datetime(2009,1,1), datetime(2010,1,1)) fig 

cell 3:

avg_20 = [ sum(close_vals.values[i-20:i])/20.0 in range(20,len(close_vals))] avg_20_times = close_vals.index[20:] ax.plot(avg_20_times, avg_20, label='20 day trailing average') ax.legend() fig 

after updating ax in each subsequent cell, calling fig redisplays plot; looking for. thanks!

you can use variables reference figure , axe objects:

in cell 1:

fig, ax = subplots(1, 1) plot(randn(100)); 

in cell 2:

ax.set_xlim(20, 40) fig 

Comments