Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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
15 changes: 9 additions & 6 deletions 15 examples/images_contours_and_fields/plot_streamplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,30 @@
V = 1 + X - Y**2
speed = np.sqrt(U**2 + V**2)

fig = plt.figure(figsize=(7, 9))
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.

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


# Varying density along a streamline
ax0 = fig.add_subplot(gs[0, 0])
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.

strm = ax1.streamplot(X, Y, U, V, color=U, linewidth=2, cmap='autumn')
fig.colorbar(strm.lines)
ax1.set_title('Varying Color')

# Varying line width along a streamline
ax2 = fig.add_subplot(gs[1, 0])
lw = 5*speed / speed.max()
ax2.streamplot(X, Y, U, V, density=0.6, color='k', linewidth=lw)
ax2.set_title('Varying Line Width')

# Controlling the starting points of the streamlines
seed_points = np.array([[-2, -1, 0, 1, 2, -1], [-2, -1, 0, 1, 2, 2]])

ax3 = fig.add_subplot(gs[1, 1])
plt.sca(ax3)
strm = ax3.streamplot(X, Y, U, V, color=U, linewidth=2,
cmap='autumn', start_points=seed_points.T)
fig.colorbar(strm.lines)
Expand All @@ -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().

ax4.remove()
ax5.remove()
ax4 = fig.add_subplot(gs[2:, :])
ax4.streamplot(X, Y, U, V, color='r')
ax4.set_title('Streamplot with Masking')
Expand Down
2 changes: 1 addition & 1 deletion 2 examples/pie_and_polar_charts/polar_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
r = np.arange(0, 2, 0.01)
theta = 2 * np.pi * r

ax = plt.subplot(111, projection='polar')
fig, ax = plt.subplots(subplot_kw={'projection': 'polar'})
ax.plot(theta, r)
ax.set_rmax(2)
ax.set_rticks([0.5, 1, 1.5, 2]) # Less radial ticks
Expand Down
8 changes: 4 additions & 4 deletions 8 examples/showcase/xkcd.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
# Based on "Stove Ownership" from XKCD by Randall Munroe
# https://xkcd.com/418/

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.

ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.set_xticks([])
Expand Down Expand Up @@ -44,8 +44,8 @@
# Based on "The Data So Far" from XKCD by Randall Munroe
# https://xkcd.com/373/

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))
ax.bar([0, 1], [0, 100], 0.25)
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
Expand Down
18 changes: 9 additions & 9 deletions 18 examples/subplots_axes_and_figures/subplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

plt.plot(x1, y1, 'o-')
plt.title('A tale of 2 subplots')
plt.ylabel('Damped oscillation')

plt.subplot(2, 1, 2)
plt.plot(x2, y2, '.-')
plt.xlabel('time (s)')
plt.ylabel('Undamped')
fig, (ax1, ax2) = plt.subplots(2, 1)
fig.suptitle('A tale of 2 subplots')

ax1.plot(x1, y1, 'o-')
ax1.set_ylabel('Damped oscillation')

ax2.plot(x2, y2, '.-')
ax2.set_xlabel('time (s)')
ax2.set_ylabel('Undamped')

plt.show()
Morty Proxy This is a proxified and sanitized view of the page, visit original site.