-
-
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
Changes from 2 commits
a2765d5
3c4faca
cd25cea
2ae3dd9
1e58203
a664c8a
a9d580e
bc444fa
820bbdf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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)) | ||
((ax0, ax1), (ax2, ax3), (ax4, ax5)) = axes | ||
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. There is really no need to unpack these, so don't do so. Just refer to |
||
|
||
# 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) | ||
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. I don't see why this is needed. 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. 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 commentThe reason will be displayed to describe this comment to others. Learn more. I think it would be better to pass the axes to |
||
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) | ||
|
@@ -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 commentThe 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 commentThe 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 |
||
ax4.remove() | ||
ax5.remove() | ||
ax4 = fig.add_subplot(gs[2:, :]) | ||
ax4.streamplot(X, Y, U, V, color='r') | ||
ax4.set_title('Streamplot with Masking') | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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)) | ||
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.
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.
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. 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 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. 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 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. 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([]) | ||
|
@@ -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') | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 commentThe 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 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. 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 commentThe 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 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. 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 commentThe 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 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. 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() |
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.