-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
ENH: reuse oldgridspec is possible... #17347
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
Subplot and subplot2grid can now work with constrained layout | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
``constrained_layout`` depends on a single ``GridSpec`` | ||
for each logical layout on a figure. Previously, ``plt.subplot`` and | ||
``plt.subplot2grid`` added a new ``GridSpec`` each time they were called and | ||
were therefore incompatible with ``constrained_layout``. | ||
|
||
Now ``plt.subplot`` attempts to reuse the ``GridSpec`` if the number of rows | ||
and columns is the same as the top level gridspec already in the figure. | ||
i.e. ``plt.subplot(2, 1, 2)`` will use the same gridspec as | ||
``plt.subplot(2, 1, 1)`` and the ``constrained_layout=True`` option to | ||
`~.figure.Figure` will work. | ||
|
||
In contrast, mixing ``nrows`` and ``ncols`` will *not* work with | ||
``constrained_lyaout``: ``plt.subplot(2, 2, 1)`` followed by | ||
``plt.subplots(2, 1, 2)`` will still produce two gridspecs, and | ||
``constrained_layout=True`` will give bad results. In order to get the | ||
desired effect, the second call can specify the cells the second axes is meant | ||
to cover: ``plt.subplots(2, 2, (2, 4))``, or the more pythonic | ||
``plt.subplot2grid((2, 2), (0, 1), rowspan=2)`` can be used. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -492,40 +492,43 @@ def docomplicated(suptitle=None): | |
# Incompatible functions | ||
# ---------------------- | ||
# | ||
# ``constrained_layout`` will not work on subplots created via | ||
# `.pyplot.subplot`. The reason is that each call to `.pyplot.subplot` creates | ||
# a separate `.GridSpec` instance and ``constrained_layout`` uses (nested) | ||
# gridspecs to carry out the layout. So the following fails to yield a nice | ||
# layout: | ||
# ``constrained_layout`` will work with `.pyplot.subplot`, but only if the | ||
# number of rows and columns is the same for each call. | ||
# The reason is that each call to `.pyplot.subplot` will create a new | ||
# `.GridSpec` instance if the geometry is not the same, and | ||
# ``constrained_layout``. So the following works fine: | ||
|
||
|
||
fig = plt.figure() | ||
|
||
ax1 = plt.subplot(221) | ||
ax2 = plt.subplot(223) | ||
ax3 = plt.subplot(122) | ||
ax1 = plt.subplot(2, 2, 1) | ||
ax2 = plt.subplot(2, 2, 3) | ||
# third axes that spans both rows in second column: | ||
ax3 = plt.subplot(2, 2, (2, 4)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. perhaps add a comment explaining what this does? |
||
|
||
example_plot(ax1) | ||
example_plot(ax2) | ||
example_plot(ax3) | ||
plt.suptitle('Homogenous nrows, ncols') | ||
|
||
############################################################################### | ||
# Of course that layout is possible using a gridspec: | ||
# but the following leads to a poor layout: | ||
|
||
fig = plt.figure() | ||
gs = fig.add_gridspec(2, 2) | ||
|
||
ax1 = fig.add_subplot(gs[0, 0]) | ||
ax2 = fig.add_subplot(gs[1, 0]) | ||
ax3 = fig.add_subplot(gs[:, 1]) | ||
ax1 = plt.subplot(2, 2, 1) | ||
ax2 = plt.subplot(2, 2, 3) | ||
ax3 = plt.subplot(1, 2, 2) | ||
|
||
example_plot(ax1) | ||
example_plot(ax2) | ||
example_plot(ax3) | ||
plt.suptitle('Mixed nrows, ncols') | ||
|
||
############################################################################### | ||
# Similarly, | ||
# :func:`~matplotlib.pyplot.subplot2grid` doesn't work for the same reason: | ||
# each call creates a different parent gridspec. | ||
# :func:`~matplotlib.pyplot.subplot2grid` works with the same limitation | ||
# that nrows and ncols cannot change for the layout to look good. | ||
|
||
fig = plt.figure() | ||
|
||
|
@@ -538,23 +541,7 @@ def docomplicated(suptitle=None): | |
example_plot(ax2) | ||
example_plot(ax3) | ||
example_plot(ax4) | ||
|
||
############################################################################### | ||
# The way to make this plot compatible with ``constrained_layout`` is again | ||
# to use ``gridspec`` directly | ||
|
||
fig = plt.figure() | ||
gs = fig.add_gridspec(3, 3) | ||
|
||
ax1 = fig.add_subplot(gs[0, 0]) | ||
ax2 = fig.add_subplot(gs[0, 1:]) | ||
ax3 = fig.add_subplot(gs[1:, 0:2]) | ||
ax4 = fig.add_subplot(gs[1:, -1]) | ||
|
||
example_plot(ax1) | ||
example_plot(ax2) | ||
example_plot(ax3) | ||
example_plot(ax4) | ||
fig.suptitle('subplot2grid') | ||
|
||
############################################################################### | ||
# Other Caveats | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like the comment above this line no longer belongs there.