Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

fixed conversion from pt to inch in tight_layout #1065

Merged
merged 2 commits into from

2 participants

@pwuertz
Collaborator

When using tight_layout the figure padding changes for different figure.dpi settings. This is caused by an error in when converting from points to inches. A point is always 1/72 inch and does not depend on the dots per inches.

@WeatherGod
Collaborator

Ooooh, good catch! I suspect this may also require updating test images. We probably should also double-check this in axes_grid1 in the mpl_toolkits.

@WeatherGod
Collaborator

looks like axes_grid1 is unaffected, I think.

@WeatherGod
Collaborator

Indeed, the tests for tight_layout fail with this PR (which isn't a bad thing!). Note that in my test runs (using an older freetype), the layout is "less tight" now than before. This may mean that we need to adjust some of the default parameters.

@pwuertz
Collaborator

Hm, at least for my matplotlib installation the default figure.dpi is 80. I assume this is the default for the tests as well? If so, the borders will grow by 80/72 = 11% after applying this patch. You think it's best to reduce the default tight_layout defaults by that fraction?

Do you get an additional difference for other freetype libraries or is your experience explained by that 11% fraction?

@pwuertz
Collaborator

Ok, when I change all ocurences of pad=1.2 to pad=1.08 all tight_layout tests pass except tight_layout6_svg, but this seems to be a simple rounding error. I added another commit that includes these new defaults and updates the expected image. Shall I combine these commits into one for the pull request?

@WeatherGod
Collaborator

No need to combine commits into one (actually, I prefer multiple commits in a PR as it helps me to see what actions were taken to address various comments). I will test this branch out a bit and double-check for any lingering references to the old padding values.

@pwuertz
Collaborator

Rebased the commits on current master and fixed another pad=1.2 default in tight_layout.py that has been added by a recent commit. Also, correcting the padding value in test_tightlayout.py instead of uploading a new test image made the patch less noisy ;).

@WeatherGod
Collaborator

The tests pass for me (except for knownfails due to my freetype version). Merging...

@WeatherGod WeatherGod merged commit e6818e7 into matplotlib:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
2  lib/matplotlib/figure.py
@@ -1361,7 +1361,7 @@ def get_tightbbox(self, renderer):
return bbox_inches
- def tight_layout(self, renderer=None, pad=1.2, h_pad=None, w_pad=None, rect=None):
+ def tight_layout(self, renderer=None, pad=1.08, h_pad=None, w_pad=None, rect=None):
"""
Adjust subplot parameters to give specified padding.
View
2  lib/matplotlib/gridspec.py
@@ -270,7 +270,7 @@ def locally_modified_subplot_params(self):
return [k for k in self._AllowedKeys if getattr(self, k)]
- def tight_layout(self, fig, renderer=None, pad=1.2, h_pad=None, w_pad=None, rect=None):
+ def tight_layout(self, fig, renderer=None, pad=1.08, h_pad=None, w_pad=None, rect=None):
"""
Adjust subplot parameters to give specified padding.
View
2  lib/matplotlib/pyplot.py
@@ -1114,7 +1114,7 @@ def subplot_tool(targetfig=None):
-def tight_layout(pad=1.2, h_pad=None, w_pad=None, rect=None):
+def tight_layout(pad=1.08, h_pad=None, w_pad=None, rect=None):
"""
Adjust subplot parameters to give specified padding.
View
4 lib/matplotlib/tests/test_tightlayout.py
@@ -110,7 +110,7 @@ def test_tight_layout6():
ax.set_xlabel("x-label", fontsize=12)
- gs2.tight_layout(fig, rect=[0.5, 0, 1, 1], h_pad=0.5)
+ gs2.tight_layout(fig, rect=[0.5, 0, 1, 1], h_pad=0.45)
top = min(gs1.top, gs2.top)
bottom = max(gs1.bottom, gs2.bottom)
@@ -119,5 +119,5 @@ def test_tight_layout6():
0.5, 1 - (gs1.top-top)])
gs2.tight_layout(fig, rect=[0.5, 0 + (bottom-gs2.bottom),
None, 1 - (gs2.top-top)],
- h_pad=0.5)
+ h_pad=0.45)
View
10 lib/matplotlib/tight_layout.py
@@ -38,7 +38,7 @@ def auto_adjust_subplotpars(fig, renderer,
num1num2_list,
subplot_list,
ax_bbox_list=None,
- pad=1.2, h_pad=None, w_pad=None,
+ pad=1.08, h_pad=None, w_pad=None,
rect=None):
"""
Return a dictionary of subplot parameters so that spacing between
@@ -71,15 +71,15 @@ def auto_adjust_subplotpars(fig, renderer,
rows, cols = nrows_ncols
- pad_inches = pad * FontProperties(size=rcParams["font.size"]).get_size_in_points() / renderer.dpi
+ pad_inches = pad * FontProperties(size=rcParams["font.size"]).get_size_in_points() / 72.
if h_pad is not None:
- vpad_inches = h_pad * FontProperties(size=rcParams["font.size"]).get_size_in_points() / renderer.dpi
+ vpad_inches = h_pad * FontProperties(size=rcParams["font.size"]).get_size_in_points() / 72.
else:
vpad_inches = pad_inches
if w_pad is not None:
- hpad_inches = w_pad * FontProperties(size=rcParams["font.size"]).get_size_in_points() / renderer.dpi
+ hpad_inches = w_pad * FontProperties(size=rcParams["font.size"]).get_size_in_points() / 72.
else:
hpad_inches = pad_inches
@@ -210,7 +210,7 @@ def get_renderer(fig):
def get_tight_layout_figure(fig, axes_list, renderer,
- pad=1.2, h_pad=None, w_pad=None, rect=None):
+ pad=1.08, h_pad=None, w_pad=None, rect=None):
"""
return subplot parameters for tigh-layouted- figure with
specified padding.
Something went wrong with that request. Please try again.