Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 72d9a86

Browse files
authored
Merge pull request #12940 from QuLogic/showcase-pyplot
Avoid pyplot in showcase examples.
2 parents 99d8900 + a4a6306 commit 72d9a86

File tree

4 files changed

+39
-39
lines changed

4 files changed

+39
-39
lines changed

examples/showcase/bachelors_degrees_by_gender.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,18 @@
1919
asfileobj=False)
2020
gender_degree_data = np.genfromtxt(fname, delimiter=',', names=True)
2121

22-
# These are the colors that will be used in the plot
23-
color_sequence = ['#1f77b4', '#aec7e8', '#ff7f0e', '#ffbb78', '#2ca02c',
24-
'#98df8a', '#d62728', '#ff9896', '#9467bd', '#c5b0d5',
25-
'#8c564b', '#c49c94', '#e377c2', '#f7b6d2', '#7f7f7f',
26-
'#c7c7c7', '#bcbd22', '#dbdb8d', '#17becf', '#9edae5']
27-
2822
# You typically want your plot to be ~1.33x wider than tall. This plot
2923
# is a rare exception because of the number of lines being plotted on it.
3024
# Common sizes: (10, 7.5) and (12, 9)
3125
fig, ax = plt.subplots(1, 1, figsize=(12, 14))
3226

27+
# These are the colors that will be used in the plot
28+
ax.set_prop_cycle(color=[
29+
'#1f77b4', '#aec7e8', '#ff7f0e', '#ffbb78', '#2ca02c', '#98df8a',
30+
'#d62728', '#ff9896', '#9467bd', '#c5b0d5', '#8c564b', '#c49c94',
31+
'#e377c2', '#f7b6d2', '#7f7f7f', '#c7c7c7', '#bcbd22', '#dbdb8d',
32+
'#17becf', '#9edae5'])
33+
3334
# Remove the plot frame lines. They are unnecessary here.
3435
ax.spines['top'].set_visible(False)
3536
ax.spines['bottom'].set_visible(False)
@@ -47,22 +48,23 @@
4748
ax.set_xlim(1969.5, 2011.1)
4849
ax.set_ylim(-0.25, 90)
4950

50-
# Make sure your axis ticks are large enough to be easily read.
51-
# You don't want your viewers squinting to read your plot.
52-
plt.xticks(range(1970, 2011, 10), fontsize=14)
53-
plt.yticks(range(0, 91, 10), fontsize=14)
51+
# Set a fixed location and format for ticks.
52+
ax.set_xticks(range(1970, 2011, 10))
53+
ax.set_yticks(range(0, 91, 10))
5454
ax.xaxis.set_major_formatter(plt.FuncFormatter('{:.0f}'.format))
5555
ax.yaxis.set_major_formatter(plt.FuncFormatter('{:.0f}%'.format))
5656

5757
# Provide tick lines across the plot to help your viewers trace along
5858
# the axis ticks. Make sure that the lines are light and small so they
5959
# don't obscure the primary data lines.
60-
plt.grid(True, 'major', 'y', ls='--', lw=.5, c='k', alpha=.3)
60+
ax.grid(True, 'major', 'y', ls='--', lw=.5, c='k', alpha=.3)
6161

6262
# Remove the tick marks; they are unnecessary with the tick lines we just
63-
# plotted.
64-
plt.tick_params(axis='both', which='both', bottom=False, top=False,
65-
labelbottom=True, left=False, right=False, labelleft=True)
63+
# plotted. Make sure your axis ticks are large enough to be easily read.
64+
# You don't want your viewers squinting to read your plot.
65+
ax.tick_params(axis='both', which='both', labelsize=14,
66+
bottom=False, top=False, labelbottom=True,
67+
left=False, right=False, labelleft=True)
6668

6769
# Now that the plot is prepared, it's time to actually plot the data!
6870
# Note that I plotted the majors in order of the highest % in the final year.
@@ -80,14 +82,12 @@
8082
'Math and Statistics': 0.75, 'Architecture': -0.75,
8183
'Computer Science': 0.75, 'Engineering': -0.25}
8284

83-
for rank, column in enumerate(majors):
85+
for column in majors:
8486
# Plot each line separately with its own color.
8587
column_rec_name = column.replace('\n', '_').replace(' ', '_')
8688

87-
line = plt.plot(gender_degree_data['Year'],
88-
gender_degree_data[column_rec_name],
89-
lw=2.5,
90-
color=color_sequence[rank])
89+
line, = ax.plot('Year', column_rec_name, data=gender_degree_data,
90+
lw=2.5)
9191

9292
# Add a text label to the right end of every line. Most of the code below
9393
# is adding specific offsets y position because some labels overlapped.
@@ -98,7 +98,7 @@
9898

9999
# Again, make sure that all labels are large enough to be easily read
100100
# by the viewer.
101-
plt.text(2011.5, y_pos, column, fontsize=14, color=color_sequence[rank])
101+
ax.text(2011.5, y_pos, column, fontsize=14, color=line.get_color())
102102

103103
# Make the title big enough so it spans the entire plot, but don't make it
104104
# so big that it requires two lines to show.
@@ -111,5 +111,5 @@
111111
# Finally, save the figure as a PNG.
112112
# You can also save it as a PDF, JPEG, etc.
113113
# Just change the file extension in this call.
114-
# plt.savefig('percent-bachelors-degrees-women-usa.png', bbox_inches='tight')
114+
# fig.savefig('percent-bachelors-degrees-women-usa.png', bbox_inches='tight')
115115
plt.show()

examples/showcase/integral.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ def func(x):
2626
y = func(x)
2727

2828
fig, ax = plt.subplots()
29-
plt.plot(x, y, 'r', linewidth=2)
30-
plt.ylim(ymin=0)
29+
ax.plot(x, y, 'r', linewidth=2)
30+
ax.set_ylim(bottom=0)
3131

3232
# Make the shaded region
3333
ix = np.linspace(a, b)
@@ -36,11 +36,11 @@ def func(x):
3636
poly = Polygon(verts, facecolor='0.9', edgecolor='0.5')
3737
ax.add_patch(poly)
3838

39-
plt.text(0.5 * (a + b), 30, r"$\int_a^b f(x)\mathrm{d}x$",
40-
horizontalalignment='center', fontsize=20)
39+
ax.text(0.5 * (a + b), 30, r"$\int_a^b f(x)\mathrm{d}x$",
40+
horizontalalignment='center', fontsize=20)
4141

42-
plt.figtext(0.9, 0.05, '$x$')
43-
plt.figtext(0.1, 0.9, '$y$')
42+
fig.text(0.9, 0.05, '$x$')
43+
fig.text(0.1, 0.9, '$y$')
4444

4545
ax.spines['right'].set_visible(False)
4646
ax.spines['top'].set_visible(False)

examples/showcase/mandelbrot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def mandelbrot_set(xmin, xmax, ymin, ymax, xn, yn, maxiter, horizon=2.0):
6161
light = colors.LightSource(azdeg=315, altdeg=10)
6262
M = light.shade(M, cmap=plt.cm.hot, vert_exag=1.5,
6363
norm=colors.PowerNorm(0.3), blend_mode='hsv')
64-
plt.imshow(M, extent=[xmin, xmax, ymin, ymax], interpolation="bicubic")
64+
ax.imshow(M, extent=[xmin, xmax, ymin, ymax], interpolation="bicubic")
6565
ax.set_xticks([])
6666
ax.set_yticks([])
6767

examples/showcase/xkcd.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,27 @@
1212

1313
with plt.xkcd():
1414
# Based on "Stove Ownership" from XKCD by Randall Munroe
15-
# http://xkcd.com/418/
15+
# https://xkcd.com/418/
1616

1717
fig = plt.figure()
1818
ax = fig.add_axes((0.1, 0.2, 0.8, 0.7))
1919
ax.spines['right'].set_color('none')
2020
ax.spines['top'].set_color('none')
21-
plt.xticks([])
22-
plt.yticks([])
21+
ax.set_xticks([])
22+
ax.set_yticks([])
2323
ax.set_ylim([-30, 10])
2424

2525
data = np.ones(100)
2626
data[70:] -= np.arange(30)
2727

28-
plt.annotate(
28+
ax.annotate(
2929
'THE DAY I REALIZED\nI COULD COOK BACON\nWHENEVER I WANTED',
3030
xy=(70, 1), arrowprops=dict(arrowstyle='->'), xytext=(15, -10))
3131

32-
plt.plot(data)
32+
ax.plot(data)
3333

34-
plt.xlabel('time')
35-
plt.ylabel('my overall health')
34+
ax.set_xlabel('time')
35+
ax.set_ylabel('my overall health')
3636
fig.text(
3737
0.5, 0.05,
3838
'"Stove Ownership" from xkcd by Randall Munroe',
@@ -42,7 +42,7 @@
4242

4343
with plt.xkcd():
4444
# Based on "The Data So Far" from XKCD by Randall Munroe
45-
# http://xkcd.com/373/
45+
# https://xkcd.com/373/
4646

4747
fig = plt.figure()
4848
ax = fig.add_axes((0.1, 0.2, 0.8, 0.7))
@@ -51,12 +51,12 @@
5151
ax.spines['top'].set_color('none')
5252
ax.xaxis.set_ticks_position('bottom')
5353
ax.set_xticks([0, 1])
54+
ax.set_xticklabels(['CONFIRMED BY\nEXPERIMENT', 'REFUTED BY\nEXPERIMENT'])
5455
ax.set_xlim([-0.5, 1.5])
56+
ax.set_yticks([])
5557
ax.set_ylim([0, 110])
56-
ax.set_xticklabels(['CONFIRMED BY\nEXPERIMENT', 'REFUTED BY\nEXPERIMENT'])
57-
plt.yticks([])
5858

59-
plt.title("CLAIMS OF SUPERNATURAL POWERS")
59+
ax.set_title("CLAIMS OF SUPERNATURAL POWERS")
6060

6161
fig.text(
6262
0.5, 0.05,

0 commit comments

Comments
 (0)