-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
convert some sample plots to use plt.subplots() instead of other methods #17340
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
convert some sample plots to use plt.subplots() instead of other methods #17340
Conversation
ax0.streamplot(X, Y, U, V, density=[0.5, 1]) | ||
ax0.set_title('Varying Density') | ||
|
||
# Varying color along a streamline | ||
ax1 = fig.add_subplot(gs[0, 1]) | ||
plt.sca(ax1) |
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.
I don't see why this is needed.
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.
Hi, thanks for the review. I deleted line 34 to fix the issue (of consistant use of .subplots() instead of different method in each sample) and in line 35 I added sca() in order to display the colorbar next to the right axes.
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.
I think it would be better to pass the axes to fig.colorbar
(as fig.calorbar(strm.lines, ax=axN)
) instead. That will explicitly tell colorbar which axes to steal the space from rather than relying on the global state.
@@ -61,6 +61,9 @@ | |||
U[:20, :20] = np.nan | |||
U = np.ma.array(U, mask=mask) | |||
|
|||
gs = gridspec.GridSpec(nrows=3, ncols=2, height_ratios=[1, 1, 2]) |
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.
Not sure this is the best way to do this. @jklymak?
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.
Yeah, don't create a new gridspec. You can get the old one with gs = axs.get_subpltospec().get_gridspec()
.
@@ -15,14 +15,14 @@ | |||
y1 = np.cos(2 * np.pi * x1) * np.exp(-x1) | |||
y2 = np.cos(2 * np.pi * x2) | |||
|
|||
plt.subplot(2, 1, 1) |
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.
From the filename, I'm not sure if this file is actually about subplot
...
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.
Did you mean it should be an example of the subplot() function? The page is referenced from the section “ Multiple Subplots”, so I guess it does not specifically refer to subplot().
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.
I think its reasonable to have what we feel is the canonical way here.
Can I suggest you add a second figure that shows subplot
though with a bit of preceding text that says it is an alternate way using pyplot
?
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.
BTW, the rest seems fine. Ping for a review, and thanks for working on this!
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.
Not sure I fully understand what you mean, you recommand adding a second example in the same page with the original method of subplot
?
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.
Yes. Each example can have multiple figures on it.
Thanks for working on this @asafmaman101 ! |
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.
So I don't agree with the use of subplots
for the stream plot example. Adding axes and then erasing them is confusing. If you are going to have multi-row or column axes based on gridspec, the original code is the best way to do it, in my opinion.
gs = gridspec.GridSpec(nrows=3, ncols=2, height_ratios=[1, 1, 2]) | ||
|
||
gs = {'height_ratios': [1, 1, 2]} | ||
fig, axes = plt.subplots(nrows=3, ncols=2, gridspec_kw=gs, figsize=(7, 9)) |
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.
We usually use axs
for a list of axes.
|
||
gs = {'height_ratios': [1, 1, 2]} | ||
fig, axes = plt.subplots(nrows=3, ncols=2, gridspec_kw=gs, figsize=(7, 9)) | ||
((ax0, ax1), (ax2, ax3), (ax4, ax5)) = axes |
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.
There is really no need to unpack these, so don't do so. Just refer to axs[0, 1]
etc.
@@ -61,6 +61,9 @@ | |||
U[:20, :20] = np.nan | |||
U = np.ma.array(U, mask=mask) | |||
|
|||
gs = gridspec.GridSpec(nrows=3, ncols=2, height_ratios=[1, 1, 2]) |
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.
Yeah, don't create a new gridspec. You can get the old one with gs = axs.get_subpltospec().get_gridspec()
.
…ng and changing varibale name
Hi, I modified the files as discussed above. How does it look now? Thanks. |
|
||
# Create a mask | ||
mask = np.zeros(U.shape, dtype=bool) | ||
mask[40:60, 40:60] = True | ||
U[:20, :20] = np.nan | ||
U = np.ma.array(U, mask=mask) | ||
|
||
gs = axs[0, 0].get_subplotspec().get_gridspec() | ||
axs[2, 0].remove() | ||
axs[2, 1].remove() |
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.
Again, I don't think we should use example with ax.remove()
in them. I'd prefer we just added each of these (i.e. the old behaviour was fine). But maybe other folks have other opinions.
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.
Hi,
I agree it was a bit messy to use the subplots method here. I reverted the file to the original version, as you recommended.
How are the other files?
Also, I cannot figure out why the codecov test does not pass sometimes.
Thanks
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.
Thanks for working on this. And sorry we're requesting to revert parts. We should have been better curating #17023 for desired changes.
examples/showcase/xkcd.py
Outdated
fig = plt.figure() | ||
ax = fig.add_axes((0.1, 0.2, 0.8, 0.7)) | ||
fig, ax = plt.subplots() | ||
ax.set_position((0.1, 0.2, 0.8, 0.7)) |
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.
add_axes()
is specifically designed for adding axes at arbitrary locations. IMHO it's the right method to use here. Creating a figure and then creating an axes at a specific position is conceptually simpler than creating a figure and an axes in the default layout and then moving the existing axes raound.
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.
subplots
sets up a gridspec for the underlying axes, whereas if you are just going to move it that gridspec is not needed. So there is some advantage to add_axes
here.
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.
Hi, no problem I can revert it to the original version, but I want to make sure I understand - you suggest to keep it the same way it was in the beginning? or some other method using both subplots
and add_axes
(if it's feasible, didn't already thought about it to details).
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.
Hi, no problem I can revert it to the original version, but I want to make sure I understand - you suggest to keep it the same way it was in the beginning? or some other method using both subplots
and add_axes
(if it's feasible, didn't already thought about it to details).
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.
That example was already correct as it was originally. Please revert your change.
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.
The commented issues cause the sphinx build failure.
PR Summary
Closes #17023. I modified the following sample plots, as issued:
EXCLUDING
Since, that one was fixed in PR #16559
PR Checklist