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

Skip to content

Commit 981dd24

Browse files
committed
Use set_xticks(ticks, labels) instead of a separate set_xticklabels()
1 parent 30cbdf7 commit 981dd24

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

examples/scales/log_bar.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
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

2726
ax.set_xlabel('x')

examples/showcase/integral.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ def func(x):
4646
ax.spines.top.set_visible(False)
4747
ax.xaxis.set_ticks_position('bottom')
4848

49-
ax.set_xticks((a, b))
50-
ax.set_xticklabels(('$a$', '$b$'))
49+
ax.set_xticks([a, b], labels=['$a$', '$b$'])
5150
ax.set_yticks([])
5251

5352
plt.show()

examples/specialty_plots/mri_with_eeg.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@
6868
ax2.add_collection(lines)
6969

7070
# Set the yticks to use axes coordinates on the y axis
71-
ax2.set_yticks(ticklocs)
72-
ax2.set_yticklabels(['PG3', 'PG5', 'PG7', 'PG9'])
71+
ax2.set_yticks(ticklocs, labels=['PG3', 'PG5', 'PG7', 'PG9'])
7372

7473
ax2.set_xlabel('Time (s)')
7574

examples/statistics/barchart_demo.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,12 @@ def plot_student_results(student, scores, cohort_size):
8888
# Set the right-hand Y-axis ticks and labels
8989
ax2 = ax1.twinx()
9090

91-
# Set the tick locations
92-
ax2.set_yticks(pos)
91+
# Set the tick locations and labels
92+
ax2.set_yticks(
93+
pos, labels=[format_score(scores[k].score, k) for k in test_names])
9394
# Set equal limits on both yaxis so that the ticks line up
9495
ax2.set_ylim(ax1.get_ylim())
9596

96-
# Set the tick labels
97-
ax2.set_yticklabels([format_score(scores[k].score, k) for k in test_names])
98-
9997
ax2.set_ylabel('Test Scores')
10098

10199
xlabel = ('Percentile Ranking Across {grade} Grade {gender}s\n'

examples/statistics/boxplot_vs_violin.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,11 @@
4545
# adding horizontal grid lines
4646
for ax in axs:
4747
ax.yaxis.grid(True)
48-
ax.set_xticks([y + 1 for y in range(len(all_data))])
48+
ax.set_xticks([y + 1 for y in range(len(all_data))],
49+
labels=['x1', 'x2', 'x3', 'x4'])
4950
ax.set_xlabel('Four separate samples')
5051
ax.set_ylabel('Observed values')
5152

52-
# add x-tick labels
53-
plt.setp(axs, xticks=[y + 1 for y in range(len(all_data))],
54-
xticklabels=['x1', 'x2', 'x3', 'x4'])
5553
plt.show()
5654

5755
#############################################################################

examples/statistics/customized_violin.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ def adjacent_values(vals, q1, q3):
2929
def set_axis_style(ax, labels):
3030
ax.xaxis.set_tick_params(direction='out')
3131
ax.xaxis.set_ticks_position('bottom')
32-
ax.set_xticks(np.arange(1, len(labels) + 1))
33-
ax.set_xticklabels(labels)
32+
ax.set_xticks(np.arange(1, len(labels) + 1), labels=labels)
3433
ax.set_xlim(0.25, len(labels) + 0.75)
3534
ax.set_xlabel('Sample name')
3635

examples/statistics/multiple_histograms_side_by_side.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@
5454
lefts = x_loc - 0.5 * binned_data
5555
ax.barh(centers, binned_data, height=heights, left=lefts)
5656

57-
ax.set_xticks(x_locations)
58-
ax.set_xticklabels(labels)
57+
ax.set_xticks(x_locations, labels)
5958

6059
ax.set_ylabel("Data values")
6160
ax.set_xlabel("Data sets")

examples/style_sheets/ggplot.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@
4545
ax3.bar(x, y1, width)
4646
ax3.bar(x + width, y2, width,
4747
color=list(plt.rcParams['axes.prop_cycle'])[2]['color'])
48-
ax3.set_xticks(x + width)
49-
ax3.set_xticklabels(['a', 'b', 'c', 'd', 'e'])
48+
ax3.set_xticks(x + width, labels=['a', 'b', 'c', 'd', 'e'])
5049

5150
# circles with colors from default color cycle
5251
for i, color in enumerate(plt.rcParams['axes.prop_cycle']):

examples/style_sheets/style_sheets_reference.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ def plot_bar_graphs(ax, prng, min_value=5, max_value=25, nb_samples=5):
4545
width = 0.25
4646
ax.bar(x, ya, width)
4747
ax.bar(x + width, yb, width, color='C2')
48-
ax.set_xticks(x + width)
49-
ax.set_xticklabels(['a', 'b', 'c', 'd', 'e'])
48+
ax.set_xticks(x + width, labels=['a', 'b', 'c', 'd', 'e'])
5049
return ax
5150

5251

examples/text_labels_and_annotations/multiline.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@
3434
va="baseline", ha="right", multialignment="left",
3535
bbox=dict(fc="none"))
3636

37-
ax1.set_xticks([0.2, 0.4, 0.6, 0.8, 1.])
38-
ax1.set_xticklabels(["Jan\n2009", "Feb\n2009", "Mar\n2009", "Apr\n2009",
39-
"May\n2009"])
37+
ax1.set_xticks([0.2, 0.4, 0.6, 0.8, 1.],
38+
labels=["Jan\n2009", "Feb\n2009", "Mar\n2009", "Apr\n2009",
39+
"May\n2009"])
4040

4141
ax1.axhline(0.4)
4242
ax1.set_title("test line spacing for multiline text")

examples/ticks_and_spines/spines_bounds.py

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

2222
# set ticks and tick labels
2323
ax.set_xlim((0, 2*np.pi))
24-
ax.set_xticks([0, np.pi, 2*np.pi])
25-
ax.set_xticklabels(['0', r'$\pi$', r'2$\pi$'])
24+
ax.set_xticks([0, np.pi, 2*np.pi], labels=['0', r'$\pi$', r'2$\pi$'])
2625
ax.set_ylim((-1.5, 1.5))
2726
ax.set_yticks([-1, 0, 1])
2827

examples/units/bar_unit_demo.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@
3333
label='Women')
3434

3535
ax.set_title('Scores by group and gender')
36-
ax.set_xticks(ind + width / 2)
37-
ax.set_xticklabels(('G1', 'G2', 'G3', 'G4', 'G5'))
36+
ax.set_xticks(ind + width / 2, labels=['G1', 'G2', 'G3', 'G4', 'G5'])
3837

3938
ax.legend()
4039
ax.yaxis.set_units(inch)

tutorials/toolkits/axes_grid.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,9 @@
161161
tick-formatter for bottom(or left)-axis. ::
162162
163163
ax2 = ax.twin() # now, ax2 is responsible for "top" axis and "right" axis
164-
ax2.set_xticks([0., .5*np.pi, np.pi, 1.5*np.pi, 2*np.pi])
165-
ax2.set_xticklabels(["0", r"$\frac{1}{2}\pi$",
166-
r"$\pi$", r"$\frac{3}{2}\pi$", r"$2\pi$"])
164+
ax2.set_xticks([0., .5*np.pi, np.pi, 1.5*np.pi, 2*np.pi],
165+
labels=["0", r"$\frac{1}{2}\pi$",
166+
r"$\pi$", r"$\frac{3}{2}\pi$", r"$2\pi$"])
167167
168168
.. figure:: ../../gallery/axes_grid1/images/sphx_glr_simple_axisline4_001.png
169169
:target: ../../gallery/axes_grid1/simple_axisline4.html

0 commit comments

Comments
 (0)