Skip to content
This repository

tight_layout: fix regression for figures with non SubplotBase Axes #1163

Merged
merged 1 commit into from over 1 year ago

3 participants

Peter Würtz Eric Firing Michael Droettboom
Peter Würtz
Collaborator

When encountering axes instances that do not descent from SubplotBase, tight_layout tries to print a warning, but there is a typo in that code block causing an exception and thus stopping the whole process.

Also, in my thesis I have a figure that uses an inset (mpl_toolkits.axes_grid1.inset_locator.inset_axes). I usually remove the figure borders using tight_layout(0.0). This worked fine in matplotlib 1.1.1, but the current master just bails out from tight_layout when encountering the inset axes. I think showing the warning to the user is enough, the result might turn out to be just fine...

Peter Würtz
Collaborator

The commit that introduced this change is ea0630a
@efiring I'm letting tight_layout process the SubplotBase instances only + warn the user that some axis might be off.. you think this is fine?

lib/matplotlib/figure.py
... ...
@@ -1426,14 +1426,14 @@ def tight_layout(self, renderer=None, pad=1.08, h_pad=None, w_pad=None, rect=Non
1426 1426
 
1427 1427
         no_go = [ax for ax in self.axes if not isinstance(ax, SubplotBase)]
1428 1428
         if no_go:
1429  
-            warnings.Warn("Cannot use tight_layout;"
1430  
-                          " all Axes must descend from SubplotBase")
1431  
-            return
  1429
+            warnings.warn("tight_layout can only process Axes that descend "
  1430
+            "from SubplotBase, results might be incorrect")
1
Eric Firing Owner
efiring added a note August 28, 2012

Please indent the continuation line(s), preferably by lining up the LH quote marks.
I would change the comma in the second line to a semicolon, and end with a period.

Rather than cycle through self.axes twice, why not move your axes calculation below up to above
the "no_go" line, then change that line to "dangerous = len(axes) < len(self.axes)" or something like that.

Maybe also change "axes" to "subplot_axes", to make it clear we are not operating on all axes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Eric Firing
Owner

Apart from the inline note above, I think your idea is fine. The no_go approach was overkill.

Peter Würtz
Collaborator

Fixed up the commit according to your suggestions.

Eric Firing efiring merged commit 0c7cdaa into from August 28, 2012
Eric Firing efiring closed this August 28, 2012
Michael Droettboom mdboom commented on the diff August 29, 2012
lib/matplotlib/figure.py
@@ -1424,16 +1424,15 @@ def tight_layout(self, renderer=None, pad=1.08, h_pad=None, w_pad=None, rect=Non
1424 1424
 
1425 1425
         from tight_layout import get_renderer, get_tight_layout_figure
1426 1426
 
1427  
-        no_go = [ax for ax in self.axes if not isinstance(ax, SubplotBase)]
1428  
-        if no_go:
1429  
-            warnings.Warn("Cannot use tight_layout;"
1430  
-                          " all Axes must descend from SubplotBase")
1431  
-            return
  1427
+        subplot_axes = [ax for ax in self.axes if isinstance(ax, SubplotBase)]
  1428
+        if len(subplot_axes) < len(self.axes):
  1429
+            warnings.warn("tight_layout can only process Axes that descend "
2
Michael Droettboom Owner
mdboom added a note August 29, 2012

I think the word "descend" here is a bit confusing. I think the more commonly used term in OO is "inherit". If there's no objections, since this is already merged, I'll just go ahead and fix this directly on master.

Eric Firing Owner
efiring added a note August 29, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Aug 29, 2012
tight_layout: fix regression for figures with non SubplotBase Axes 85438a0
This page is out of date. Refresh to see the latest.

Showing 1 changed file with 5 additions and 6 deletions. Show diff stats Hide diff stats

  1. 11  lib/matplotlib/figure.py
11  lib/matplotlib/figure.py
@@ -1424,16 +1424,15 @@ def tight_layout(self, renderer=None, pad=1.08, h_pad=None, w_pad=None, rect=Non
1424 1424
 
1425 1425
         from tight_layout import get_renderer, get_tight_layout_figure
1426 1426
 
1427  
-        no_go = [ax for ax in self.axes if not isinstance(ax, SubplotBase)]
1428  
-        if no_go:
1429  
-            warnings.Warn("Cannot use tight_layout;"
1430  
-                          " all Axes must descend from SubplotBase")
1431  
-            return
  1427
+        subplot_axes = [ax for ax in self.axes if isinstance(ax, SubplotBase)]
  1428
+        if len(subplot_axes) < len(self.axes):
  1429
+            warnings.warn("tight_layout can only process Axes that descend "
  1430
+                          "from SubplotBase; results might be incorrect.")
1432 1431
 
1433 1432
         if renderer is None:
1434 1433
             renderer = get_renderer(self)
1435 1434
 
1436  
-        kwargs = get_tight_layout_figure(self, self.axes, renderer,
  1435
+        kwargs = get_tight_layout_figure(self, subplot_axes, renderer,
1437 1436
                                          pad=pad, h_pad=h_pad, w_pad=w_pad,
1438 1437
                                          rect=rect)
1439 1438
 
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.