8000 Cleanup demo_tight_layout. by anntzer · Pull Request #21100 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Cleanup demo_tight_layout. #21100

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

Merged
merged 1 commit into from
Sep 16, 2021
Merged
Changes from all commits
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
26 changes: 7 additions & 19 deletions examples/subplots_axes_and_figures/demo_tight_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def example_plot(ax):

fig, ax = plt.subplots()
example_plot(ax)
plt.tight_layout()
fig.tight_layout()
< 8000 /td>
###############################################################################

Expand All @@ -40,61 +40,53 @@ def example_plot(ax):
example_plot(ax2)
example_plot(ax3)
example_plot(ax4)
plt.tight_layout()
fig.tight_layout()

###############################################################################

fig, (ax1, ax2) = plt.subplots(nrows=2, ncols=1)
example_plot(ax1)
example_plot(ax2)
plt.tight_layout()
fig.tight_layout()

###############################################################################

fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2)
example_plot(ax1)
example_plot(ax2)
plt.tight_layout()
fig.tight_layout()

###############################################################################

fig, axs = plt.subplots(nrows=3, ncols=3)
for ax in axs.flat:
example_plot(ax)
plt.tight_layout()
fig.tight_layout()

###############################################################################

fig = plt.figure()

plt.figure()
ax1 = plt.subplot(221)
ax2 = plt.subplot(223)
ax3 = plt.subplot(122)

example_plot(ax1)
example_plot(ax2)
example_plot(ax3)

plt.tight_layout()
Copy link
Member

Choose a reason for hiding this comment

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

I don't understand why you didn't change this (and all the rest) to fig.tight_layout instead of removing fig?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because the whole subexample is pyplot-based, and I didn't want to change that?

Copy link
Member

Choose a reason for hiding this comment

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

Ah, sure.


###############################################################################

fig = plt.figure()

plt.figure()
ax1 = plt.subplot2grid((3, 3), (0, 0))
ax2 = plt.subplot2grid((3, 3), (0, 1), colspan=2)
ax3 = plt.subplot2grid((3, 3), (1, 0), colspan=2, rowspan=2)
ax4 = plt.subplot2grid((3, 3), (1, 2), rowspan=2)

example_plot(ax1)
example_plot(ax2)
example_plot(ax3)
example_plot(ax4)

plt.tight_layout()

plt.show()

###############################################################################

fig = plt.figure()
Expand All @@ -103,20 +95,16 @@ def example_plot(ax):
ax1 = fig.add_subplot(gs1[0])
ax2 = fig.add_subplot(gs1[1])
ax3 = fig.add_subplot(gs1[2])

example_plot(ax1)
example_plot(ax2)
example_plot(ax3)

gs1.tight_layout(fig, rect=[None, None, 0.45, None])

gs2 = fig.add_gridspec(2, 1)
ax4 = fig.add_subplot(gs2[0])
ax5 = fig.add_subplot(gs2[1])

example_plot(ax4)
example_plot(ax5)

with warnings.catch_warnings():
# gs2.tight_layout cannot handle the subplots from the first gridspec
# (gs1), so it will raise a warning. We are going to match the gridspecs
Expand Down
0