Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
another attempt; add a test
  • Loading branch information
ResidentMario committed Mar 10, 2017
commit c23e5f60ab49c383345220ab98ad360441546881
10 changes: 10 additions & 0 deletions lib/matplotlib/tests/test_tightlayout.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ def test_tight_layout7():
ax.set_title('Right Title', loc='right', fontsize=fontsize)
plt.tight_layout()


@image_comparison(baseline_images=['tight_layout8'])
def test_tight_layout8():
'Test automatic use of tight_layout'
Expand All @@ -157,6 +158,15 @@ def test_tight_layout8():
example_plot(ax, fontsize=24)


@image_comparison(baseline_images=['tight_layout9'])
def test_tight_layout9():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is this passing? The baseline images have not been added...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bear with me here, I have no idea how matplotlib handles its tests. 😅 Was planning on getting back to this in a later commit, but you've caught me red-handed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are now two test functions in one file with the same name. So the first one gets clobbered by the second one.

Copy link
Member

@WeatherGod WeatherGod Mar 16, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, please ignore. I misread test_tight_layout8 as test_tight_layout9. Really need to fix the default fonts on this computer...

# Test tight_layout for non-visible suplots
# GH 8244
f, axarr = plt.subplots(2, 2)
axarr[1][1].set_visible(False)
plt.tight_layout()


# The following test is misleading when the text is removed.
@image_comparison(baseline_images=['outward_ticks'], remove_text=False)
def test_outward_ticks():
Expand Down
5 changes: 4 additions & 1 deletion lib/matplotlib/tight_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,10 @@ def auto_adjust_subplotpars(fig, renderer,
for subplots, ax_bbox, (num1, num2) in zip(subplot_list,
ax_bbox_list,
num1num2_list):
tight_bbox_raw = union([ax.get_tightbbox(renderer) for ax in subplots if ax.get_visible()])
if all([not ax.get_visible() for ax in subplots]):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this just be filtering out invisible axes? subplots = [ax for ox in subplots if ax.get_visible()]

Would it make more sense to make sure what ever calls method does that filtering?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, I don't understand why you changed what I originally suggested. I suggested putting a filter in the list comprehension down below. Now, it does make sense to skip this iteration if all the axes in this group are invisible. So, I would put the filter in the list comprehension down below, and also change this one from a any to an all.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

grrr, I fail reading comprehension... you do have a all. But the filter should still be down below.

Copy link
Contributor Author

@ResidentMario ResidentMario Mar 10, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first commit tried that approach (see here).

But it actually failed to fix the issue. If you try to use that code with the original minimal failing example:

import matplotlib.pyplot as plt
f, axarr = plt.subplots(2, 2)
axarr[1][1].set_visible(False)
plt.tight_layout()

The union op will fail because the list comprehension will pass it an empty list ([]).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before making a PR, please check that your commit actually fixes the issue...ahem. 😅

Skipping the laying calculations altogether (the approach here) meanwhile seems to work.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's why you need both approaches. if a subset of the subplots are not visible, then the way the code is currently, the invisible axes still have get_tightbbox called on them, which would also fail. You need to protect that (my original approach), and protect from a union() call on an empty list (your current approach).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I understand you now—I've re-added the conditional check.

continue

tight_bbox_raw = union([ax.get_tightbbox(renderer) for ax in subplots])
tight_bbox = TransformedBbox(tight_bbox_raw,
fig.transFigure.inverted())

Expand Down