On Thu, Feb 16, 2012 at 3:09 PM, Martin Mokrejs <mmokr...@fold.natur.cuni.cz
> wrote:

> Hi Benjamin,
>  thank you for you explanation. My comment is below in the text:
>
> Benjamin Root wrote:
> >
> >
> > On Tue, Feb 14, 2012 at 4:43 PM, Martin Mokrejs <
> mmokr...@fold.natur.cuni.cz <mailto:mmokr...@fold.natur.cuni.cz>> wrote:
> >
> >     Ah, this seems to be the issue that my figsize was growing all the
> time so it
> >     went over the maximum limits.
> >
> >     I thought this is valid:
> >     DefaultSize = F.get_size_inches()
> >     print str(DefaultSize)
> >     blah
> >     F.set_size_inches(DefaultSize)
> >
> >     See http://matplotlib.sourceforge.net/api/figure_api.html
> >
> >     <quote>
> >     set_size_inches(*args, **kwargs)
> >
> >        set_size_inches(w,h, forward=False)
> >
> >        Set the figure size in inches
> >
> >        Usage:
> >
> >        fig.set_size_inches(w,h)  # OR
> >        fig.set_size_inches((w,h) )
> >
> >        optional kwarg forward=True will cause the canvas size to be
> automatically updated; eg you can resize the figure window from the shell
> >
> >        ACCEPTS: a w,h tuple with w,h in inches
> >     </quote>
> >
> >     Nope, it does not work. The print call gives me: [ 8.  6.]. So, this
> is not a tuple?
> >     Or python-2.7 issue how is it printed ... I fear? ;-)
> >     Anyway, doing
> >
> >     F.set_size_inches(11.2, 15)
> >
> >     works for me.
> >
> >     Martin
> >
> >
> > I am a little bit confused by your code example.  You get the figure
> size and print it, and *then* you set it with the exact same values, and
> you are surprised that it came out as [8. 6.]?  Note that the figure size
> is stored internally as a numpy array, so when you do "print
> str(DefaultSize)", you will get the string representation of the numpy
> array.  You can still pass in a tuple, list, or two separate elements.  Try
> this code:
>
> No, in my experience it did NOT work. I suspect F.set_size_inches() either
> did not like the input tuple or something else. Now. after reading your
> clarification, are you sure it can input the numpy array as well? What I
> also tried was to re-set the figsize to original values.
>
>
Yes, it can.  I found the source of the problem, see further down.


> Ouch, I use pylab not matplotlib directly. :(
>
>
Doesn't matter.


> $ python
> Python 2.7.2 (default, Feb  7 2012, 19:33:08)
> [GCC 4.5.3] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> >>> import pylab
> >>> F = pylab.gcf()
> >>> print F.get_size_inches()
> [ 8.  6.]
> >>> DefaultSize = F.get_size_inches()
> >>> print DefaultSize
> [ 8.  6.]
> >>> F.set_size_inches(10, 10)
> >>> print F.get_size_inches()
> [ 10.  10.]
> >>> F.set_size_inches(DefaultSize[0], DefaultSize[1])
> >>> print F.get_size_inches()
> [ 10.  10.]
> >>>
>
>
The bug here is assuming that DefaultSize still contained the values you
printed earlier.  This is subtle (and I missed it before), but what you are
getting back from F.get_size_inches() is a view of the internal numpy
array.  When you set the new size, the internal array was updated, not
replaced.  This is much in the same vein as Python mutables, but taken a
bit further than you are probably used to.  Because the internal array was
updated, the view (stored in DefaultSize) showed the new data as well.  So,
when you tried to set (what you thought was still) the original size, it
was merely setting the current values back to itself.  Therefore, no change.

So, to force DefaultSize to be immutable, just cast it as a tuple:

>>> DefaultSize = tuple(F.get_size_inches())



> Why in the above example I cannot return back to figsize [ 8.  6.] ?
>
> >
> > import matplotlib.pyplot as plt
> > fig = plt.figure()
> > print fig.get_size_inches()
> > fig.set_size_inches(11.2, 15.0)
> > print fig.get_size_inches()
> > fig.set_size_inches((4.0, 7.2))
> > print fig.get_size_inches()
> > fig.set_size_inches([9.3, 11.1])
> > print fig.get_size_inches()
> >
> >
> > You should see:
> >
> > [ 8.  6.]
> > [ 11.2  15. ]
> > [ 4.   7.2]
> > [  9.2  11.1]
>
> Yes, this works.
>
> >
> > Everything works as expected.  There is nothing special about python 2.7
> in this regard.  Let us know if you are still having problems updating your
> figures and include a stand-alone example showing how the figure size is
> not being updated.
>
> What does the internal numpy array representation bring good to the
> figsize? ;-)
> Why don't you use a simple list/tuple? I am sure you know what you're
> doing,
> am just curious. Especially if slicing behaves differently compared to
> list/tuple
> and the .__str__() also gives in my eyes weird output. Sure, matter of
> taste. ;)
>
>
We use numpy arrays internally for several reasons.  Primarially is that we
have to do mathematical operations with this information.  list/tuple do
not lend itself to that (which is why numpy exists in the first place).
numpy arrays also enforce type checking (so you can't put a string for a
size value, or anything else that doesn't make sense).  Another reason is
that the slicing is significantly more advanced than for list/tuples.
Internally, there is a 2x2 array.  I can slice in either dimension very
easily, while a list of lists would not be easy.  Lastly, we need a
combination of the immutability of tuples (don't want people changing the
array size), but the mutability of lists (we want to be able to change the
individual values).

I will admit that this issue is not immediately intuitive, and some
documentation should probably be added to help prevent users like yourself
from falling into this trap.

I hope that helps!
Ben Root
------------------------------------------------------------------------------
Virtualization & Cloud Management Using Capacity Planning
Cloud computing makes use of virtualization - but cloud computing 
also focuses on allowing computing to be delivered as a service.
http://www.accelacomm.com/jaw/sfnl/114/51521223/
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Reply via email to