Skip to content

Commit ed4f814

Browse files
authored
Merge pull request matplotlib#20636 from timhoffm/doc-xticks-labels
Use set_xticks(ticks, labels) instead of a separate set_xticklabels()
2 parents dba02be + 50fa443 commit ed4f814

File tree

23 files changed

+43
-75
lines changed

23 files changed

+43
-75
lines changed

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/demo_ticklabel_alignment.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@
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

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/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/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/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

0 commit comments

Comments
 (0)