Skip to content
This repository

fixed conversion from pt to inch in tight_layout #1065

Merged
merged 2 commits into from over 1 year ago

2 participants

Peter Würtz Benjamin Root
Peter Würtz
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.

Benjamin Root
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.

Benjamin Root
Collaborator

looks like axes_grid1 is unaffected, I think.

Benjamin Root
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.

Peter Würtz
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?

Peter Würtz
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?

Benjamin Root
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.

Peter Würtz
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 ;).

Benjamin Root
Collaborator

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

Benjamin Root WeatherGod merged commit e6818e7 into from August 15, 2012
Benjamin Root WeatherGod closed this August 15, 2012
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.
2  lib/matplotlib/figure.py
@@ -1361,7 +1361,7 @@ def get_tightbbox(self, renderer):
1361 1361
 
1362 1362
         return bbox_inches
1363 1363
 
1364  
-    def tight_layout(self, renderer=None, pad=1.2, h_pad=None, w_pad=None, rect=None):
  1364
+    def tight_layout(self, renderer=None, pad=1.08, h_pad=None, w_pad=None, rect=None):
1365 1365
         """
1366 1366
         Adjust subplot parameters to give specified padding.
1367 1367
 
2  lib/matplotlib/gridspec.py
@@ -270,7 +270,7 @@ def locally_modified_subplot_params(self):
270 270
         return [k for k in self._AllowedKeys if getattr(self, k)]
271 271
 
272 272
 
273  
-    def tight_layout(self, fig, renderer=None, pad=1.2, h_pad=None, w_pad=None, rect=None):
  273
+    def tight_layout(self, fig, renderer=None, pad=1.08, h_pad=None, w_pad=None, rect=None):
274 274
         """
275 275
         Adjust subplot parameters to give specified padding.
276 276
 
2  lib/matplotlib/pyplot.py
@@ -1114,7 +1114,7 @@ def subplot_tool(targetfig=None):
1114 1114
 
1115 1115
 
1116 1116
 
1117  
-def tight_layout(pad=1.2, h_pad=None, w_pad=None, rect=None):
  1117
+def tight_layout(pad=1.08, h_pad=None, w_pad=None, rect=None):
1118 1118
     """
1119 1119
     Adjust subplot parameters to give specified padding.
1120 1120
 
4  lib/matplotlib/tests/test_tightlayout.py
@@ -110,7 +110,7 @@ def test_tight_layout6():
110 110
 
111 111
     ax.set_xlabel("x-label", fontsize=12)
112 112
 
113  
-    gs2.tight_layout(fig, rect=[0.5, 0, 1, 1], h_pad=0.5)
  113
+    gs2.tight_layout(fig, rect=[0.5, 0, 1, 1], h_pad=0.45)
114 114
 
115 115
     top = min(gs1.top, gs2.top)
116 116
     bottom = max(gs1.bottom, gs2.bottom)
@@ -119,5 +119,5 @@ def test_tight_layout6():
119 119
                                 0.5, 1 - (gs1.top-top)])
120 120
     gs2.tight_layout(fig, rect=[0.5, 0 + (bottom-gs2.bottom),
121 121
                                 None, 1 - (gs2.top-top)],
122  
-                     h_pad=0.5)
  122
+                     h_pad=0.45)
123 123
 
10  lib/matplotlib/tight_layout.py
@@ -38,7 +38,7 @@ def auto_adjust_subplotpars(fig, renderer,
38 38
                             num1num2_list,
39 39
                             subplot_list,
40 40
                             ax_bbox_list=None,
41  
-                            pad=1.2, h_pad=None, w_pad=None,
  41
+                            pad=1.08, h_pad=None, w_pad=None,
42 42
                             rect=None):
43 43
     """
44 44
     Return a dictionary of subplot parameters so that spacing between
@@ -71,15 +71,15 @@ def auto_adjust_subplotpars(fig, renderer,
71 71
 
72 72
     rows, cols = nrows_ncols
73 73
 
74  
-    pad_inches = pad * FontProperties(size=rcParams["font.size"]).get_size_in_points() / renderer.dpi
  74
+    pad_inches = pad * FontProperties(size=rcParams["font.size"]).get_size_in_points() / 72.
75 75
 
76 76
     if h_pad is not None:
77  
-        vpad_inches = h_pad * FontProperties(size=rcParams["font.size"]).get_size_in_points() / renderer.dpi
  77
+        vpad_inches = h_pad * FontProperties(size=rcParams["font.size"]).get_size_in_points() / 72.
78 78
     else:
79 79
         vpad_inches = pad_inches
80 80
 
81 81
     if w_pad is not None:
82  
-        hpad_inches = w_pad * FontProperties(size=rcParams["font.size"]).get_size_in_points() / renderer.dpi
  82
+        hpad_inches = w_pad * FontProperties(size=rcParams["font.size"]).get_size_in_points() / 72.
83 83
     else:
84 84
         hpad_inches = pad_inches
85 85
 
@@ -210,7 +210,7 @@ def get_renderer(fig):
210 210
 
211 211
 
212 212
 def get_tight_layout_figure(fig, axes_list, renderer,
213  
-                            pad=1.2, h_pad=None, w_pad=None, rect=None):
  213
+                            pad=1.08, h_pad=None, w_pad=None, rect=None):
214 214
     """
215 215
     return subplot parameters for tigh-layouted- figure with
216 216
     specified padding.
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.