-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Increased padding using tight_layout with pad=0 #7806
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Is this worth anything to you? Or are you more interested in why |
I was interested in why Setting |
You can visualise and set the bbox padding with something like this |
So, I think I’ve stumbled upon this issue too. As long as I don’t put labels, I’m going to post about several cases I’ve tried, in hope this helps understanding what is going wrong. |
So first let’s use the default example from here (with some small modifications):
tight_default.pdf |
Note btw that |
Now, a small test: using a letter going low for the x label. It just works. So it seems that the code is reserving the space for low letters, and we can see that for the Title too were the bbox is including this space even with 0 padding. |
Now, some more considerations. When adding |
bounding boxes are not determined optically - they are determined by what the renderer can determine from the properties of the plot. In the case of I'm not clear what the use-case is that would justify the extra effort to make this work flawlessly. There are image post processing tools that will crop out the whitespace, which seems a lot easier than expecting the composer to do it: http://www.imagemagick.org/script/command-line-options.php#trim |
@jklymak The use case is having the desired figure width exactly with no amount of white space whatsoever. Cropping is not an option. I was going to say that saving with |
For instance here I get way too much white space on the left. |
Constrained does a little better though, so there is still that. Which means you probably did a good job in implementing it @jklymak. ;) |
Actually it even does way better when just using the default settings outside |
On a small side note, it seems that the right border is partly cropped in all of my plots. Is that a known tight/constrained layout bug and else should I open one? |
By "use case" I mean an instance where this matters to the final presentation. Most plots get put in a paper or on a website, and will have whitespace naturally around the figure as part of the publishing process. Having that being 2 or 3pts off would be pretty hard to determine by eye. Note that there are also pdf cropping tools: http://pdfcrop.sourceforge.net Again this is an easier post-rendering problem, than trying to do this before rendering. OTOH, if someone wanted to take the care to make this all be exact pre-rendering, if that is even possible, then I'm sure a PR would be gratefully accepted. |
Again, cropping in post-processing is not an option, because it will change the width of the figure (which is fixed to fit exactly in my LaTeX file). I would just use I understand that for most people the difference does not matter, but it does for me. It’s kind of an OCD I have when it comes to layout and typography. I’m not really comfortable with you (nothing personal though, that the case with everyone) using ' for ’ and " for “ and ” for instance. Neither am I with some additional white space around my figure. ;) Unfortunately I don’t have enough knowledge (or time to acquire it) to contribute code for this, maybe in some distant future… And I understand no-one will want to spend time on this since this seems to require a lot of investment for something that don’t really matter in most cases. But at least now I know the quirks and will be able to get as close as I can. ;) In the meantime, if you could just have a look and answer my previous comment, that would be amazing. :) |
And actually, even with custom fonts your I’m digging a bit more around this, but there seems to be only one case where I get white space on the left (I’m OK ignoring the bottom one, I actually think it’s more consistent to always assume a descender so that two figures side by side would be identical). I’ll report when I will have been able to pinpoint the problematic case. And then there is just the small cropping on the right side remaining. |
OK, found it. I think it is a bug (or maybe not, depending on the desired behaviour) that is not pertaining to this discussion but more general: So if there is no descender, the y-label is “right” (or bottom, it’s a matter of POV) aligned in comparison to the other case. Note that the bbox remains exactly the same in both cases. I think we would want the baseline (where letters like “a” seat) of text to always have the same distance to the figure, don’t we? If so, should I open a new bug report for this? If that and the small cropping on the right are fixed, then this whole issue can be considered as fixed for me thanks to |
I have a PR in for aligning the labels with respect to the axes that changes the default to aligning the baseline of the text. I think the correct thing to do is for the size of the axes to be the same regardless of whether a particular label has a descender or not. |
I think #11499 |
With respect to the cropping on the right hand spine... Its possible this is fixed in master because the bbox of the spine is now included, but I can’t check right now. If not the issue is that the width of the line is not being taken into account. I’d actually be surprised if this was really cropped if you embedded the figure in a surrounding document but the line will of course overspill by half the linewidth. |
You work in #11499 likely solves the alignment part. Great work! Regarding the right hand spine… here is your surprise: Just took my
|
Ok well I guess the clip path is the clip path. I don’t think it makes too much sense to check the line thickness of the spine when checking it’s bbox, but someone could try and add that if they felt compelled. To my thinking the thickness of that line shouldn’t change the size of the plot. If people want to make sure it’s not clipped they can include a reasonable pad. |
Can you point me to where in the code this is checked? Maybe that’s a very small and easy change to be made, so I might have a look. Else I’ll probably live with a Anyway with everything else being sorted, I’m already moving toward my next issue. ;) |
I’m not on computer but it’d be in spines.py under get_window_extent I think |
I’ve setup a master install, and I can say that:
is in fact not fixed in master. I’ve took a look at I’ll leave this like that for now and eventually use |
Just as summary for anyone coming here: |
This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help! |
Given the summary in #7806 (comment) I am going to close this as resolved. |
I have found that in the rc2 version of matplotlib that there is extra padding when using
plt.tight_layout(pad=0)
for axis labels. It also is worse for when usingusetex=True
for the axis label.I ran the above code twice, once in 1.5.1 and again in 2.0.0rc2 (for 1.5.1 you must comment out
matplotlib.rcParams['xtick.top'] = True
andmatplotlib.rcParams['ytick.top'] = True
). The screenshot below shows the saved png for 1.5.1 on the left and 2.0.0rc2 on the right:In 1.5.1 you can see that the text for both the "y label" and "x label" are right next to the edge of the figure. However in 2.0.0rc2 the x and y labels are significantly spaced from the edge of the figure and the spacing is even worse for the y label where
usetex=True
.I am using Python 2.7 installed on Windows from anaconda.
The text was updated successfully, but these errors were encountered: