[IPython-User] [SciPy-User] IPython inline plots of stacked bars graphs

MinRK benjaminrk@gmail....
Sat Aug 20 22:07:32 CDT 2011


Hi,

This is indeed an IPython issue.

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.

Currently, you have two choices:

1. do all your drawing to a given figure in a single multiline cell
2. get a reference to the axes/figure you want to add to, and use its
methods to update the plot across input cells

1. is easy if you know what you are doing beforehand, but that's not often
true of interactive sessions.

In your example, let's say you have done:

In [10]: plt.bar(ind, menMeans, color='r')
Out[10]: <Container...>

The container is a collection of Rectangle objects which have references to
the Axes object the barplot is on:

In [21]: ax = _10.get_children()[0].get_axes()

There are a few different tricks for these, depending on the kind of plot
you want to get back,
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.

Now that you have your axes, you can continue to draw on it, resize it, etc.
at any time:

In [32]: ax.bar(ind, womenMeans, color='y', bottom=menMeans)

But these calls will *not* cause the figure to be redrawn immediately.
Any time you would trigger the displayhook on a Figure (e.g. return it at
the end of a cell), it gets redrawn:

In [33]: ax.get_figure()

<plot with stacked bars>

And you can call ax.get_figure() as many times as you like, to draw the plot
again.

If you want to force a figure to be drawn in the middle of a block when it
wouldn't trigger the displayhook,
you can call `display(fig)`.

Similarly, if you have accumulated figures fig1...fign throughout your
session, you can draw them together with:

In [37]: display(fig1, fig2, fig3, ...)

<plot1>
<plot2>
...

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:

In [40]:  ax = plt.subplot(111)
      ...:  ax.bar(ind, menMeans, color='r')

<single bar plot>
# add to the plot and redraw in one cell:

In [45]: ax.bar(ind, womenMeans, color='y', bottom=menMeans)
      ...:  ax.get_figure()

<stacked bar plot>

-MinRK

On Sat, Aug 20, 2011 at 19:31, Chris Withers <chris@simplistix.co.uk> wrote:

> Asking on the correct list this time:
>
> > On Sat, Aug 20, 2011 at 5:12 PM, Chris Withers <chris@simplistix.co.uk
> >     If I do the following in an IPython 0.11 Qt shell:
> >
> >     import matplotlib.pyplot as plt
> >     menMeans   = (20, 35, 30, 35, 27)
> >     womenMeans = (25, 32, 34, 20, 25)
> >     plt.bar(ind, menMeans, color='r')
> >     plt.bar(ind, womenMeans, color='y', bottom=menMeans)
> >
> >     I get, as I'd expect, a stacked bar graph.
> >
> >     However, if I do:
> >
> >     plt.bar(ind, menMeans, color='r')
> >
> >     ...hit enter, and then do:
> >
> >     plt.bar(ind, womenMeans, color='y', bottom=menMeans)
> >
> >     ...I get two separate plots.
> >
> >     How can I add to an existing inline plot?
> >
> >     Also, and I guess this might be more of a matplotlib question, how do
> I
> >     "reach inside" an existing plot to, for example, adjust the width of
> the
> >     bars used?
>
> On 20/08/2011 16:46, Charles R Harris wrote:
> >
> > I think it is more of an ipython question, possibly a matplotlib
> > question ;) You might try the hold(True) command.
>
> Sadly, that didn't work :-S
>
> cheers,
>
> Chris
>
> --
> Simplistix - Content Management, Batch Processing & Python Consulting
>             - http://www.simplistix.co.uk
> _______________________________________________
> IPython-User mailing list
> IPython-User@scipy.org
> http://mail.scipy.org/mailman/listinfo/ipython-user
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/ipython-user/attachments/20110820/c25d75af/attachment.html 


More information about the IPython-User mailing list