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
2 changes: 1 addition & 1 deletion 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 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
29 changes: 29 additions & 0 deletions examples/subplots_axes_and_figures/subplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,35 @@
import numpy as np
import matplotlib.pyplot as plt

###############################################################################

x1 = np.linspace(0.0, 5.0)
x2 = np.linspace(0.0, 2.0)

y1 = np.cos(2 * np.pi * x1) * np.exp(-x1)
y2 = np.cos(2 * np.pi * x2)

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

#############################################################################
#
#
# Alternative Method For Creating Multiple Plots
# """"""""""
#
# Subplots can also be generated using `subplot()` as in the following example:
#


x1 = np.linspace(0.0, 5.0)
x2 = np.linspace(0.0, 2.0)
Expand Down
0