8000 convert some sample plots to use plt.subplots() instead of other methods by asafmaman101 · Pull Request #17340 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

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

Merged
merged 9 commits into from
May 25, 2020

Conversation

asafmaman101
Copy link
Contributor
@asafmaman101 asafmaman101 commented May 6, 2020

PR Summary

Closes #17023. I modified the following sample plots, as issued:

  • "Multiple Subplots" (subplot),
  • "streamplot" (fig.add_subplots),
  • "Polar Plots" (subplot),
  • XKCD (fig.add_axes).

EXCLUDING

  • "Surface3d" (gca)

Since, that one was fixed in PR #16559

PR Checklist

  • Has Pytest style unit tests
  • Code is Flake 8 compliant
  • New features are documented, with examples if plot related
  • Documentation is sphinx and numpydoc compliant
  • Added an entry to doc/users/next_whats_new/ if major new feature (follow instructions in README.rst there)
  • Documented in doc/api/api_changes.rst if API changed in a backward-incompatible way

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)
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 see why this is needed.

Copy link
Contributor Author

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.

Copy link
Member

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])
Copy link
Member

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?

Copy link
Member

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)
Copy link
Member

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...

Copy link
Contributor Author

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().

Copy link
Member

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?

Copy link
Member

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!

Copy link
Contributor Author

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?

Copy link
Member

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.

@tacaswell
Copy link
Member

Thanks for working on this @asafmaman101 !

@tacaswell tacaswell added this to the v3.4.0 milestone May 7, 2020
Copy link
Member
@jklymak jklymak left a 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))
Copy link
Member

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
Copy link
Member

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])
Copy link
Member

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().

@asafmaman101
Copy link
Contributor Author

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()
Copy link
Member

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.

Copy link
Contributor Author

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

Copy link
Member
@timhoffm timhoffm left a 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.

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))
Copy link
Member

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.

Copy link
Member

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.

Copy link
Contributor Author

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).

Copy link
Contributor Author

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).

Copy link
Member

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.

Copy link
Member
@timhoffm timhoffm left a 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.

@timhoffm timhoffm modified the milestones: v3.4.0, v3.3.0 May 25, 2020
@timhoffm timhoffm merged commit 36bc7da into matplotlib:master May 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DOC: Tutorial/Sample plots should use same fig/axis creation method
5 participants
0