question about tight_layout()

Just trying out the latest mpl 1.1.0 and the tight_layout() method. I saw the guide written about it, but am a unsure how to use this when using the OO approach to using Matplotlib.

When using pyplot, the method is: plt.tight_layout(). When using the OO form of mpl, is it: figure.tight_layout() ?

I assume it is, because I tried this and it didn’t give me a name error, but I did get an error: ValueError: left cannot be >= right.

What am I doing wrong?

Thanks,
Che

Figure.tight_layout() is a correct way.
Do you see that error only when you use Figure.tight_plot (and not
when you use plt.tight_layout)?

What happen you try the script below.

import matplotlib.pyplot as plt
fig = plt.figure(1)ax = fig.add_subplot(111)fig.tight_layout()
Regards,

-JJ

···

On Sat, Oct 15, 2011 at 9:13 AM, C M <cmpython@...287...> wrote:

Just trying out the latest mpl 1.1.0 and the tight_layout() method. I saw
the guide written about it, but am a unsure how to use this when using the
OO approach to using Matplotlib.

When using pyplot, the method is: plt.tight_layout(). When using the OO
form of mpl, is it: figure.tight_layout() ?

I assume it is, because I tried this and it didn't give me a name error, but
I did get an error: ValueError: left cannot be >= right.

What am I doing wrong?

Thanks,
Che

------------------------------------------------------------------------------
All the data continuously generated in your IT infrastructure contains a
definitive record of customers, application performance, security
threats, fraudulent activity and more. Splunk takes this data and makes
sense of it. Business sense. IT sense. Common sense.
http://p.sf.net/sfu/splunk-d2d-oct
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Figure.tight_layout() is a correct way.

Do you see that error only when you use Figure.tight_plot (and not

when you use plt.tight_layout)?

Yes.

What happen you try the script below.

import matplotlib.pyplot as plt

fig = plt.figure(1)ax = fig.add_subplot(111)fig.tight_layout()

That runs without error.

However, I can’t get it to work correct with Figure. I’m either getting that same error or failure to adjust the Figure’s size to accommodate the axes’ labels. I attach a minimal runnable sample that demonstrates these problems for those that embed in wxPython.

Thanks,
Che

testing_tight_layout.py (1.17 KB)

···

On Sat, Oct 15, 2011 at 10:15 AM, Jae-Joon Lee <lee.j.joon@…287…> wrote:

In your example code, do you see the error raised only when you
include the tight_layout call? Your comments seem to suggest that the
error is raised regardless of the tight_layout call, in which case I
doubt if this has anything to do with tight_layout.

Anyhow, your code runs fine in my installment, linux + python 2.7.2 +
wxgtk 2.8.11.0 + mpl master branch.
On which platform you're running this?

Regards,

-JJ

···

On Sun, Oct 16, 2011 at 1:55 PM, C M <cmpython@...287...> wrote:

However, I can't get it to work correct with Figure. I'm either getting
that same error or failure to adjust the Figure's size to accommodate the
axes' labels. I attach a minimal runnable sample that demonstrates these
problems for those that embed in wxPython.

In your example code, do you see the error raised only when you
include the tight_layout call?

Yes. To see this (at least on my platform), you take the example code
and try two things:

1) Comment IN this line: self.panel.Layout(). Run it and you'll get the error.
3) Now comment OUT the tight_layout() call. Run it and you won't get the error.

So you have to Layout() a parent panel (the one the canvas is sitting
on) for this error to occur, but that is a common thing to do in
wxPython when using sizers and shouldn't cause errors. Here is the
traceback:

Traceback (most recent call last):
  File "C:\Documents and
Settings\user\Desktop\testing_tight_layout.py", line 35, in <module>
    frame = Frame1(None)
  File "C:\Documents and
Settings\user\Desktop\testing_tight_layout.py", line 14, in __init__
    self.Add_MPL_Plot()
  File "C:\Documents and
Settings\user\Desktop\testing_tight_layout.py", line 30, in
Add_MPL_Plot
    self.figure.tight_layout()
  File "C:\Python25\Lib\site-packages\matplotlib\figure.py", line
1393, in tight_layout
    self.subplots_adjust(**kwargs)
  File "C:\Python25\Lib\site-packages\matplotlib\figure.py", line
1218, in subplots_adjust
    self.subplotpars.update(*args, **kwargs)
  File "C:\Python25\Lib\site-packages\matplotlib\figure.py", line 183, in update
    raise ValueError('left cannot be >= right')
ValueError: left cannot be >= right

I just don't understand what this means and why it would be thrown.

Anyhow, your code runs fine in my installment, linux + python 2.7.2 +
wxgtk 2.8.11.0 + mpl master branch.
On which platform you're running this?

WinXP, Python 2.5, wxPython 2.8.10.1 (msw-unicode), mpl 1.1.0 as
downloaded from the site three days ago.

When you say the code runs fine, did you try steps (1) and (2) above?
Also, if you just run it without making those changes, does the
tight_layout() work to resize the plot such that it allows the axis
labels to be seen at all times? (it doesn't for me).

So, to summarize:

- Error if I call self.panel.Layout() before I call tight_layout().
- If I don't do this, no error, but it still isn't doing a proper tight layout.

Thanks,
Che

- Error if I call self.panel.Layout() before I call tight_layout().

In my system, I don't see any error whether ` self.panel.Layout() is in or not.

- If I don't do this, no error, but it still isn't doing a proper tight layout.

Your script DOES work in my system (but I'm using it with wxGTK) and
it DOES a proper tight_layout.

(However, the figure size never gets smaller than the original figure
size so it shows only the part of the figure when the window is
smaller than the original figure size).

So, it seems that the issue is platform-dependent.

As for the error message, it seems that the subplot_params values
(left, right, top, bottom, etc) calculated by the "tight_layout"
routine is somehow corrupted.
Why this happens is hard to track down unless I can reproduce the error.
And I hope any other windows developer out there can take a look at this issue.

Meanwhile, can you play a little bit more with your script?

1) check the size of the figure (print self.figure.get_size_inches())
before a tight_layout is called.

2) see if calling the draw method (self.canvas.draw()) before the
tight_layout helps.

Also, please open a git issue on this so that others can take a look.

Regards,

-JJ

···

On Mon, Oct 17, 2011 at 1:03 AM, C M <cmpython@...287...> wrote:

So, it seems that the issue is platform-dependent.

OK.

As for the error message, it seems that the subplot_params values
(left, right, top, bottom, etc) calculated by the "tight_layout"
routine is somehow corrupted.
Why this happens is hard to track down unless I can reproduce the error.
And I hope any other windows developer out there can take a look at this issue.

Meanwhile, can you play a little bit more with your script?

1) check the size of the figure (print self.figure.get_size_inches())
before a tight_layout is called.

What it prints is this:
[ 8. 6.]

2) see if calling the draw method (self.canvas.draw()) before the
tight_layout helps.

It didn't help.

Also, please open a git issue on this so that others can take a look.

Sure. It's issue #532. (I tried to include the sample script but the
indenting got removed from the issue window, not sure how to fix
that).

Thanks,
Che