On Tue, Jun 2, 2009 at 07:33, John Hunter<jdh2...@gmail.com> wrote:
> On Tue, Jun 2, 2009 at 9:03 AM, Tom Vaughan <t...@software6.net> wrote:
>> Is it possible to add subplots to a figure if I don't know in advance
>> how many subplots I need to add?
>>
>> What I do now is I call add_subplot like add_subplot(i, 1, i) where i
>> is 1 initially, and just increases by 1 on each call. This almost
>> works. Except the first plot takes up the whole figure, the second
>> plot is placed on top of the bottom half of the first plot, etc. Is
>> there a way to "resize" the plots when a subplot is added? Or how
>> would I "re-plot" the previous subplots?
>
> See the Axes.change_geometry command
>
>  http://matplotlib.sourceforge.net/api/axes_api.html#matplotlib.axes.SubplotBase.change_geometry
>
> As in this example::
>
>    import matplotlib.pyplot as plt
>
>    # start with one
>    fig = plt.figure()
>    ax = fig.add_subplot(111)
>    ax.plot([1,2,3])
>
>    # now later you get a new subplot; change the geometry of the existing
>    n = len(fig.axes)
>    for i in range(n):
>        fig.axes[i].change_geometry(n+1, 1, i+1)

Awesome. Thanks.

Strangely this doesn't quite work for me. Luckily I keep a list of my
subplots. So I do:

        def new_subplot(self):
                nsubplots = len(self.__subplots) + 1

                for i, subplot in enumerate(self.__subplots):
                        subplot.change_geometry(nsubplots, 1, i + 1)

                subplot = self.figure.add_subplot(nsubplots, 1, nsubplots)
                subplot.grid(True)

                self.__subplots.append(subplot)
                self.__subplot = subplot

Interestingly, if I were to 'print dir(self.figure.axes[i])' I can see
the change_geometry attribute, but when I attempt to call it, I am
told "AttributeError: 'AxesSubplot' object has no attribute
'change_geomtry'" This lead me to what I have above.

Thanks.

-Tom

------------------------------------------------------------------------------
OpenSolaris 2009.06 is a cutting edge operating system for enterprises 
looking to deploy the next generation of Solaris that includes the latest 
innovations from Sun and the OpenSource community. Download a copy and 
enjoy capabilities such as Networking, Storage and Virtualization. 
Go to: http://p.sf.net/sfu/opensolaris-get
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Reply via email to