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

Skip to content

Commit 269ed42

Browse files
committed
Cleanup examples
- Use `set_xticks(positions, labels)` instead of `set_xticks(positions); set_xticklabels(labels)`. - Use batch `ax.set()` instead of multiple `set_*` calls when appropritate.
1 parent 30cbdf7 commit 269ed42

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+93
-180
lines changed

examples/animation/animate_decay.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ def data_gen():
2222

2323

2424
def init():
25-
ax.set_ylim(-1.1, 1.1)
26-
ax.set_xlim(0, 10)
25+
ax.set(xlim=(0, 10), ylim=(-1.1, 1.1))
2726
del xdata[:]
2827
del ydata[:]
2928
line.set_data(xdata, ydata)

examples/axes_grid1/make_room_for_ylabel_using_axesgrid.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
plt.figure()
1515
ax = plt.axes([0, 0, 1, 1])
1616

17-
ax.set_yticks([0.5])
18-
ax.set_yticklabels(["very long label"])
17+
ax.set_yticks([0.5], labels=["very long label"])
1918

2019
make_axes_area_auto_adjustable(ax)
2120

@@ -26,8 +25,7 @@
2625
ax1 = plt.axes([0, 0, 1, 0.5])
2726
ax2 = plt.axes([0, 0.5, 1, 0.5])
2827

29-
ax1.set_yticks([0.5])
30-
ax1.set_yticklabels(["very long label"])
28+
ax1.set_yticks([0.5], labels=["very long label"])
3129
ax1.set_ylabel("Y label")
3230

3331
ax2.set_title("Title")
@@ -53,8 +51,7 @@
5351
divider.add_auto_adjustable_area(use_axes=[ax1, ax2], pad=0.1,
5452
adjust_dirs=["top", "bottom"])
5553

56-
ax1.set_yticks([0.5])
57-
ax1.set_yticklabels(["very long label"])
54+
ax1.set_yticks([0.5], labels=["very long label"])
5855

5956
ax2.set_title("Title")
6057
ax2.set_xlabel("X - Label")

examples/axes_grid1/simple_axisline4.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
ax.plot(xx, np.sin(xx))
1414

1515
ax2 = ax.twin() # ax2 is responsible for "top" axis and "right" axis
16-
ax2.set_xticks([0., .5*np.pi, np.pi, 1.5*np.pi, 2*np.pi])
17-
ax2.set_xticklabels(["$0$", r"$\frac{1}{2}\pi$",
18-
r"$\pi$", r"$\frac{3}{2}\pi$", r"$2\pi$"])
16+
ax2.set_xticks([0., .5*np.pi, np.pi, 1.5*np.pi, 2*np.pi],
17+
labels=["$0$", r"$\frac{1}{2}\pi$",
18+
r"$\pi$", r"$\frac{3}{2}\pi$", r"$2\pi$"])
1919

2020
ax2.axis["right"].major_ticklabels.set_visible(False)
2121
ax2.axis["top"].major_ticklabels.set_visible(True)

examples/axisartist/axis_direction.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
def setup_axes(fig, pos):
1212
ax = fig.add_subplot(pos, axes_class=axisartist.Axes)
1313

14-
ax.set_ylim(-0.1, 1.5)
15-
ax.set_yticks([0, 1])
14+
ax.set(ylim=(-0.1, 1.5), yticks=[0, 1])
1615

1716
ax.axis[:].set_visible(False)
1817

examples/axisartist/demo_ticklabel_alignment.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,25 @@
1212

1313
def setup_axes(fig, pos):
1414
ax = fig.add_subplot(pos, axes_class=axisartist.Axes)
15-
ax.set_yticks([0.2, 0.8])
16-
ax.set_yticklabels(["short", "loooong"])
17-
ax.set_xticks([0.2, 0.8])
18-
ax.set_xticklabels([r"$\frac{1}{2}\pi$", r"$\pi$"])
15+
ax.set_yticks([0.2, 0.8], labels=["short", "loooong"])
16+
ax.set_xticks([0.2, 0.8], labels=[r"$\frac{1}{2}\pi$", r"$\pi$"])
1917
return ax
2018

2119

2220
fig = plt.figure(figsize=(3, 5))
2321
fig.subplots_adjust(left=0.5, hspace=0.7)
2422

2523
ax = setup_axes(fig, 311)
26-
ax.set_ylabel("ha=right")
27-
ax.set_xlabel("va=baseline")
24+
ax.set(xlabel="va=baseline", ylabel="ha=right")
2825

2926
ax = setup_axes(fig, 312)
3027
ax.axis["left"].major_ticklabels.set_ha("center")
3128
ax.axis["bottom"].major_ticklabels.set_va("top")
32-
ax.set_ylabel("ha=center")
33-
ax.set_xlabel("va=top")
29+
ax.set(xlabel="va=top", ylabel="ha=center")
3430

3531
ax = setup_axes(fig, 313)
3632
ax.axis["left"].major_ticklabels.set_ha("left")
3733
ax.axis["bottom"].major_ticklabels.set_va("bottom")
38-
ax.set_ylabel("ha=left")
39-
ax.set_xlabel("va=bottom")
34+
ax.set(xlabel="va=bottom", ylabel="ha=left")
4035

4136
plt.show()

examples/axisartist/demo_ticklabel_direction.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111

1212
def setup_axes(fig, pos):
1313
ax = fig.add_subplot(pos, axes_class=axislines.Axes)
14-
ax.set_yticks([0.2, 0.8])
15-
ax.set_xticks([0.2, 0.8])
14+
ax.set(xticks=[0.2, 0.8], yticks=[0.2, 0.8])
1615
return ax
1716

1817

examples/axisartist/simple_axis_direction03.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111

1212
def setup_axes(fig, pos):
1313
ax = fig.add_subplot(pos, axes_class=axisartist.Axes)
14-
ax.set_yticks([0.2, 0.8])
15-
ax.set_xticks([0.2, 0.8])
14+
ax.set(xticks=[0.2, 0.8], yticks=[0.2, 0.8])
1615
return ax
1716

1817

examples/event_handling/poly_editor.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ def on_mouse_move(self, event):
205205
ax.add_patch(poly)
206206
p = PolygonInteractor(ax, poly)
207207

208-
ax.set_title('Click and drag a point to move it')
209-
ax.set_xlim((-2, 2))
210-
ax.set_ylim((-2, 2))
208+
ax.set(xlim=(-2, 2), ylim=(-2, 2),
209+
title='Click and drag a point to move it')
210+
211211
plt.show()

examples/images_contours_and_fields/affine_image.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ def do_plot(ax, Z, transform):
4040
x1, x2, y1, y2 = im.get_extent()
4141
ax.plot([x1, x2, x2, x1, x1], [y1, y1, y2, y2, y1], "y--",
4242
transform=trans_data)
43-
ax.set_xlim(-5, 5)
44-
ax.set_ylim(-4, 4)
43+
ax.set(xlim=(-5, 5), ylim=(-4, 4))
4544

4645

4746
# prepare image and figure

examples/images_contours_and_fields/image_annotated_heatmap.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,9 @@
5959
fig, ax = plt.subplots()
6060
im = ax.imshow(harvest)
6161

62-
# We want to show all ticks...
63-
ax.set_xticks(np.arange(len(farmers)))
64-
ax.set_yticks(np.arange(len(vegetables)))
65-
# ... and label them with the respective list entries
66-
ax.set_xticklabels(farmers)
67-
ax.set_yticklabels(vegetables)
62+
# Show all ticks and label them with the respective list entries
63+
ax.set_xticks(np.arange(len(farmers)), labels=farmers)
64+
ax.set_yticks(np.arange(len(vegetables)), labels=vegetables)
6865

6966
# Rotate the tick labels and set their alignment.
7067
plt.setp(ax.get_xticklabels(), rotation=45, ha="right",
@@ -133,12 +130,9 @@ def heatmap(data, row_labels, col_labels, ax=None,
133130
cbar = ax.figure.colorbar(im, ax=ax, **cbar_kw)
134131
cbar.ax.set_ylabel(cbarlabel, rotation=-90, va="bottom")
135132

136-
# We want to show all ticks...
137-
ax.set_xticks(np.arange(data.shape[1]))
138-
ax.set_yticks(np.arange(data.shape[0]))
139-
# ... and label them with the respective list entries.
140-
ax.set_xticklabels(col_labels)
141-
ax.set_yticklabels(row_labels)
133+
# Show all ticks and label them with the respective list entries.
134+
ax.set_xticks(np.arange(data.shape[1]), labels=col_labels)
135+
ax.set_yticks(np.arange(data.shape[0]), labels=row_labels)
142136

143137
# Let the horizontal axes labeling appear on top.
144138
ax.tick_params(top=True, bottom=False,

examples/images_contours_and_fields/pcolormesh_grids.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,7 @@ def _annotate(ax, x, y, title):
4242
# this all gets repeated below:
4343
X, Y = np.meshgrid(x, y)
4444
ax.plot(X.flat, Y.flat, 'o', color='m')
45-
ax.set_xlim(-0.7, 5.2)
46-
ax.set_ylim(-0.7, 3.2)
47-
ax.set_title(title)
45+
ax.set(xlim=(-0.7, 5.2), ylim=(-0.7, 3.2), title=title)
4846

4947
_annotate(ax, x, y, "shading='flat'")
5048

examples/lines_bars_and_markers/bar_label_demo.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@
4040
ax.axhline(0, color='grey', linewidth=0.8)
4141
ax.set_ylabel('Scores')
4242
ax.set_title('Scores by group and gender')
43-
ax.set_xticks(ind)
44-
ax.set_xticklabels(('G1', 'G2', 'G3', 'G4', 'G5'))
43+
ax.set_xticks(ind, labels=['G1', 'G2', 'G3', 'G4', 'G5'])
4544
ax.legend()
4645

4746
# Label with label_type 'center' instead of the default 'edge'
@@ -66,8 +65,7 @@
6665
fig, ax = plt.subplots()
6766

6867
hbars = ax.barh(y_pos, performance, xerr=error, align='center')
69-
ax.set_yticks(y_pos)
70-
ax.set_yticklabels(people)
68+
ax.set_yticks(y_pos, labels=people)
7169
ax.invert_yaxis() # labels read top-to-bottom
7270
ax.set_xlabel('Performance')
7371
ax.set_title('How fast do you want to go today?')
@@ -84,8 +82,7 @@
8482
fig, ax = plt.subplots()
8583

8684
hbars = ax.barh(y_pos, performance, xerr=error, align='center')
87-
ax.set_yticks(y_pos)
88-
ax.set_yticklabels(people)
85+
ax.set_yticks(y_pos, labels=people)
8986
ax.invert_yaxis() # labels read top-to-bottom
9087
ax.set_xlabel('Performance')
9188
ax.set_title('How fast do you want to go today?')

examples/lines_bars_and_markers/barchart.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@
2525
# Add some text for labels, title and custom x-axis tick labels, etc.
2626
ax.set_ylabel('Scores')
2727
ax.set_title('Scores by group and gender')
28-
ax.set_xticks(x)
29-
ax.set_xticklabels(labels)
28+
ax.set_xticks(x, labels)
3029
ax.legend()
3130

3231
ax.bar_label(rects1, padding=3)

examples/lines_bars_and_markers/barh.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222
error = np.random.rand(len(people))
2323

2424
ax.barh(y_pos, performance, xerr=error, align='center')
25-
ax.set_yticks(y_pos)
26-
ax.set_yticklabels(people)
25+
ax.set_yticks(y_pos, labels=people)
2726
ax.invert_yaxis() # labels read top-to-bottom
2827
ax.set_xlabel('Performance')
2928
ax.set_title('How fast do you want to go today?')

examples/lines_bars_and_markers/broken_barh.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
ax.set_ylim(5, 35)
1515
ax.set_xlim(0, 200)
1616
ax.set_xlabel('seconds since start')
17-
ax.set_yticks([15, 25])
18-
ax.set_yticklabels(['Bill', 'Jim'])
17+
ax.set_yticks([15, 25], labels=['Bill', 'Jim'])
1918
ax.grid(True)
2019
ax.annotate('race interrupted', (61, 25),
2120
xytext=(0.8, 0.9), textcoords='axes fraction',

examples/lines_bars_and_markers/eventcollection_demo.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,7 @@
5252
ax.add_collection(yevents2)
5353

5454
# set the limits
55-
ax.set_xlim([0, 1])
56-
ax.set_ylim([0, 1])
57-
58-
ax.set_title('line plot with data points')
55+
ax.set(xlim=[0, 1], ylim=[0, 1], title='line plot with data points')
5956

6057
# display the plot
6158
plt.show()

examples/lines_bars_and_markers/hat_graph.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ def label_bars(heights, rects):
4040

4141
values = np.asarray(values)
4242
x = np.arange(values.shape[1])
43-
ax.set_xticks(x)
44-
ax.set_xticklabels(xlabels)
43+
ax.set_xticks(x, labels=xlabels)
4544
spacing = 0.3 # spacing between hat groups
4645
width = (1 - spacing) / values.shape[0]
4746
heights0 = values[0]

examples/lines_bars_and_markers/markevery_demo.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,7 @@ def trim_axs(axs, N):
6565
axs = plt.figure(figsize=figsize, constrained_layout=True).subplots(rows, cols)
6666
axs = trim_axs(axs, len(cases))
6767
for ax, case in zip(axs, cases):
68-
ax.set_title('markevery=%s' % str(case))
69-
ax.set_xscale('log')
70-
ax.set_yscale('log')
68+
ax.set(xscale='log', yscale='log', title=f'markevery={case!r}')
7169
ax.plot(x, y, 'o', ls='-', ms=4, markevery=case)
7270

7371
###############################################################################
@@ -79,10 +77,8 @@ def trim_axs(axs, N):
7977
axs = plt.figure(figsize=figsize, constrained_layout=True).subplots(rows, cols)
8078
axs = trim_axs(axs, len(cases))
8179
for ax, case in zip(axs, cases):
82-
ax.set_title('markevery=%s' % str(case))
8380
ax.plot(x, y, 'o', ls='-', ms=4, markevery=case)
84-
ax.set_xlim((6, 6.7))
85-
ax.set_ylim((1.1, 1.7))
81+
ax.set(xlim=(6, 6.7), ylim=(1.1, 1.7), title=f'markevery={case!r}')
8682

8783
# define data for polar plots
8884
r = np.linspace(0, 3.0, 200)
@@ -95,7 +91,7 @@ def trim_axs(axs, N):
9591
rows, cols, subplot_kw={'projection': 'polar'})
9692
axs = trim_axs(axs, len(cases))
9793
for ax, case in zip(axs, cases):
98-
ax.set_title('markevery=%s' % str(case))
94+
ax.set_title(f'markevery={case!r}')
9995
ax.plot(theta, r, 'o', ls='-', ms=4, markevery=case)
10096

10197
plt.show()

examples/misc/demo_agg_filter.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,7 @@ def drop_shadow_line(ax):
224224
shadow.set_agg_filter(gauss)
225225
shadow.set_rasterized(True) # to support mixed-mode renderers
226226

227-
ax.set_xlim(0., 1.)
228-
ax.set_ylim(0., 1.)
227+
ax.set(xlim=(0, 1), ylim=(0, 1))
229228

230229
ax.xaxis.set_visible(False)
231230
ax.yaxis.set_visible(False)

examples/misc/demo_ribbon_box.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,7 @@ def main():
7878
ax.add_artist(RibbonBoxImage(ax, bbox, bc, interpolation="bicubic"))
7979
ax.annotate(str(h), (year, h), va="bottom", ha="center")
8080

81-
ax.set_xlim(years[0] - 0.5, years[-1] + 0.5)
82-
ax.set_ylim(0, 10000)
81+
ax.set(xlim=(years[0] - 0.5, years[-1] + 0.5), ylim=(0, 10000))
8382

8483
background_gradient = np.zeros((2, 2, 4))
8584
background_gradient[:, :, :3] = [1, 1, 0]

examples/mplot3d/contour3d_3.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,8 @@
2626
cset = ax.contour(X, Y, Z, zdir='x', offset=-40, cmap=cm.coolwarm)
2727
cset = ax.contour(X, Y, Z, zdir='y', offset=40, cmap=cm.coolwarm)
2828

29-
ax.set_xlim(-40, 40)
30-
ax.set_ylim(-40, 40)
31-
ax.set_zlim(-100, 100)
32-
33-
ax.set_xlabel('X')
34-
ax.set_ylabel('Y')
35-
ax.set_zlabel('Z')
29+
ax.set(xlabel='X', xlim=(-40, 40),
30+
ylabel='Y', ylim=(-40, 40),
31+
zlabel='Z', zlim=(-100, 100))
3632

3733
plt.show()

examples/mplot3d/contourf3d_2.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,8 @@
2626
cset = ax.contourf(X, Y, Z, zdir='x', offset=-40, cmap=cm.coolwarm)
2727
cset = ax.contourf(X, Y, Z, zdir='y', offset=40, cmap=cm.coolwarm)
2828

29-
ax.set_xlim(-40, 40)
30-
ax.set_ylim(-40, 40)
31-
ax.set_zlim(-100, 100)
32-
33-
ax.set_xlabel('X')
34-
ax.set_ylabel('Y')
35-
ax.set_zlabel('Z')
29+
ax.set(xlabel='X', xlim=(-40, 40),
30+
ylabel='Y', ylim=(-40, 40),
31+
zlabel='Z', zlim=(-100, 100))
3632

3733
plt.show()

examples/pyplots/auto_subplots_adjust.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,12 @@
4343

4444
fig, ax = plt.subplots()
4545
ax.plot(range(10))
46-
ax.set_yticks((2, 5, 7))
47-
labels = ax.set_yticklabels(('really, really, really', 'long', 'labels'))
46+
ax.set_yticks([2, 5, 7], labels=['really, really, really', 'long', 'labels'])
4847

4948

5049
def on_draw(event):
5150
bboxes = []
52-
for label in labels:
51+
for label in ax.get_yticklabels():
5352
# Bounding box in pixels
5453
bbox_px = label.get_window_extent()
5554
# Transform to relative figure coordinates. This is the inverse of

examples/pyplots/text_commands.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@
1313

1414
ax = fig.add_subplot()
1515
fig.subplots_adjust(top=0.85)
16-
ax.set_title('axes title')
17-
18-
ax.set_xlabel('xlabel')
19-
ax.set_ylabel('ylabel')
16+
ax.set(xlabel='xlabel', ylabel='ylabel', title='axes title')
2017

2118
ax.text(3, 8, 'boxed italics text in data coords', style='italic',
2219
bbox={'facecolor': 'red', 'alpha': 0.5, 'pad': 10})

examples/scales/log_bar.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,9 @@
2020
y = [d[i] for d in data]
2121
b = ax.bar(x + i * dimw, y, dimw, bottom=0.001)
2222

23-
ax.set_xticks(x + dimw / 2)
24-
ax.set_xticklabels(map(str, x))
23+
ax.set_xticks(x + dimw / 2, labels=map(str, x))
2524
ax.set_yscale('log')
2625

27-
ax.set_xlabel('x')
28-
ax.set_ylabel('y')
26+
ax.set(xlabel='x', ylabel='y')
2927

3028
plt.show()

0 commit comments

Comments
 (0)