<div>Hi,</div><div><br></div><div>This is indeed an IPython issue.</div><div><br></div>This is a result of the fact that the inline backend flushes the active figures at each prompt, to prevent drawing all active figures over and over until you close them. <div>
<br></div><div>Currently, you have two choices:<div><br></div><div>1. do all your drawing to a given figure in a single multiline cell</div><div>2. get a reference to the axes/figure you want to add to, and use its methods to update the plot across input cells</div>
<div><br></div><div>1. is easy if you know what you are doing beforehand, but that's not often true of interactive sessions.</div><div><br></div><div>In your example, let's say you have done:</div><div><br></div>
<div>
In [10]: plt.bar(ind, menMeans, color='r')</div><div>Out[10]: <Container...></div><div><br></div><div>The container is a collection of Rectangle objects which have references to the Axes object the barplot is on:</div>
<div><br></div><div>In [21]: ax = _10.get_children()[0].get_axes()</div><div><br></div><div>There are a few different tricks for these, depending on the kind of plot you want to get back, </div><div>but getting the axes object from the return of a plot call is never more than a couple steps to an object with a get_axes() method.</div>
<div><br></div><div>Now that you have your axes, you can continue to draw on it, resize it, etc. at any time:</div><div><br></div><div>In [32]: ax.bar(ind, womenMeans, color='y', bottom=menMeans)</div><div><br></div>
<div>But these calls will *not* cause the figure to be redrawn immediately.</div><div>Any time you would trigger the displayhook on a Figure (e.g. return it at the end of a cell), it gets redrawn:</div><div><br></div><div>
In [33]: ax.get_figure()</div><div><br></div><div><plot with stacked bars></div><div><br></div><div>And you can call ax.get_figure() as many times as you like, to draw the plot again.</div><div><br></div><div>If you want to force a figure to be drawn in the middle of a block when it wouldn't trigger the displayhook,</div>
<div>you can call `display(fig)`.</div><div><br></div><div>Similarly, if you have accumulated figures fig1...fign throughout your session, you can draw them together with:</div><div><br></div><div>In [37]: display(fig1, fig2, fig3, ...)</div>
<div><br></div><div><plot1></div><div><plot2></div><div>...</div><div><br></div><div>If you know that you intend to make changes or additions to the plot, it will be easier if you create the Axes reference *first*, so you don't have to dig it out later:</div>
<div><br></div><div>In [40]: ax = plt.subplot(111)</div><div> ...: ax.bar(ind, menMeans, color='r')</div><div><br></div><div><single bar plot></div><div># add to the plot and redraw in one cell:</div>
<div><br><div>In [45]: ax.bar(ind, womenMeans, color='y', bottom=menMeans) </div><div> ...: ax.get_figure()</div><div><br></div><div><stacked bar plot></div><div><br></div><div>-MinRK</div><div><br></div>
<div class="gmail_quote">On Sat, Aug 20, 2011 at 19:31, Chris Withers <span dir="ltr"><<a href="mailto:chris@simplistix.co.uk">chris@simplistix.co.uk</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Asking on the correct list this time:<br>
<br>
> On Sat, Aug 20, 2011 at 5:12 PM, Chris Withers <<a href="mailto:chris@simplistix.co.uk">chris@simplistix.co.uk</a><br>
> If I do the following in an IPython 0.11 Qt shell:<br>
><br>
> import matplotlib.pyplot as plt<br>
> menMeans = (20, 35, 30, 35, 27)<br>
> womenMeans = (25, 32, 34, 20, 25)<br>
> plt.bar(ind, menMeans, color='r')<br>
> plt.bar(ind, womenMeans, color='y', bottom=menMeans)<br>
><br>
> I get, as I'd expect, a stacked bar graph.<br>
><br>
> However, if I do:<br>
><br>
> plt.bar(ind, menMeans, color='r')<br>
><br>
> ...hit enter, and then do:<br>
><br>
> plt.bar(ind, womenMeans, color='y', bottom=menMeans)<br>
><br>
> ...I get two separate plots.<br>
><br>
> How can I add to an existing inline plot?<br>
><br>
> Also, and I guess this might be more of a matplotlib question, how do I<br>
> "reach inside" an existing plot to, for example, adjust the width of the<br>
> bars used?<br>
<br>
On 20/08/2011 16:46, Charles R Harris wrote:<br>
><br>
> I think it is more of an ipython question, possibly a matplotlib<br>
> question ;) You might try the hold(True) command.<br>
<br>
Sadly, that didn't work :-S<br>
<br>
cheers,<br>
<br>
Chris<br>
<font color="#888888"><br>
--<br>
Simplistix - Content Management, Batch Processing & Python Consulting<br>
- <a href="http://www.simplistix.co.uk" target="_blank">http://www.simplistix.co.uk</a><br>
_______________________________________________<br>
IPython-User mailing list<br>
<a href="mailto:IPython-User@scipy.org">IPython-User@scipy.org</a><br>
<a href="http://mail.scipy.org/mailman/listinfo/ipython-user" target="_blank">http://mail.scipy.org/mailman/listinfo/ipython-user</a><br>
</font></blockquote></div><br></div></div>