Skip to content

Commit 8c4600b

Browse files
authored
Merge pull request #29602 from timhoffm/reduce-get_xticklabels
MNT: Reduce the use of get_xticklabels() in examples
2 parents 70d0bf7 + 5908acb commit 8c4600b

File tree

6 files changed

+13
-35
lines changed

6 files changed

+13
-35
lines changed

galleries/examples/subplots_axes_and_figures/shared_axis_demo.py

+2-13
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,10 @@
1515
1616
The example below shows how to customize the tick labels on the
1717
various axes. Shared axes share the tick locator, tick formatter,
18-
view limits, and transformation (e.g., log, linear). But the ticklabels
18+
view limits, and transformation (e.g., log, linear). But the tick labels
1919
themselves do not share properties. This is a feature and not a bug,
2020
because you may want to make the tick labels smaller on the upper
2121
axes, e.g., in the example below.
22-
23-
If you want to turn off the ticklabels for a given Axes (e.g., on
24-
subplot(211) or subplot(212)), you cannot do the standard trick::
25-
26-
setp(ax2, xticklabels=[])
27-
28-
because this changes the tick Formatter, which is shared among all
29-
Axes. But you can alter the visibility of the labels, which is a
30-
property::
31-
32-
setp(ax2.get_xticklabels(), visible=False)
33-
3422
"""
3523
import matplotlib.pyplot as plt
3624
import numpy as np
@@ -42,6 +30,7 @@
4230

4331
ax1 = plt.subplot(311)
4432
plt.plot(t, s1)
33+
# reduce the fontsize of the tick labels
4534
plt.tick_params('x', labelsize=6)
4635

4736
# share x only

galleries/examples/text_labels_and_annotations/date.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@
5858
# Text in the x-axis will be displayed in 'YYYY-mm' format.
5959
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%b'))
6060
# Rotates and right-aligns the x labels so they don't crowd each other.
61-
for label in ax.get_xticklabels(which='major'):
62-
label.set(rotation=30, horizontalalignment='right')
61+
ax.xaxis.set_tick_params(rotation=30, rotation_mode='xtick')
6362

6463
plt.show()

galleries/examples/ticks/centered_ticklabels.py

+6-12
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,12 @@
33
Center labels between ticks
44
===========================
55
6-
Ticklabels are aligned relative to their associated tick. The alignment
7-
'center', 'left', or 'right' can be controlled using the horizontal alignment
8-
property::
9-
10-
for label in ax.get_xticklabels():
11-
label.set_horizontalalignment('right')
6+
Tick labels are aligned relative to their associated tick, and are by default
7+
centered.
128
139
However, there is no direct way to center the labels between ticks. To fake
14-
this behavior, one can place a label on the minor ticks in between the major
15-
ticks, and hide the major tick labels and minor ticks.
10+
this behavior, one can place a minor tick in between the major ticks. Then
11+
label the minor tick, and hide the minor tick lines and the major tick labels.
1612
1713
Here is an example that labels the months, centered between the ticks.
1814
"""
@@ -34,15 +30,13 @@
3430
# 16 is a slight approximation since months differ in number of days.
3531
ax.xaxis.set_minor_locator(dates.MonthLocator(bymonthday=16))
3632

33+
# The NullFormatter removes the major tick labels
3734
ax.xaxis.set_major_formatter(ticker.NullFormatter())
3835
ax.xaxis.set_minor_formatter(dates.DateFormatter('%b'))
3936

40-
# Remove the tick lines
37+
# Remove the minor tick lines
4138
ax.tick_params(axis='x', which='minor', tick1On=False, tick2On=False)
4239

43-
# Align the minor tick label
44-
for label in ax.get_xticklabels(minor=True):
45-
label.set_horizontalalignment('center')
4640
imid = len(r) // 2
4741
ax.set_xlabel(str(r["date"][imid].item().year))
4842
plt.show()

galleries/examples/ticks/date_concise_formatter.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,7 @@
4040
for nn, ax in enumerate(axs):
4141
ax.plot(dates, y)
4242
ax.set_xlim(lims[nn])
43-
# rotate_labels...
44-
for label in ax.get_xticklabels():
45-
label.set_rotation(40)
46-
label.set_horizontalalignment('right')
43+
ax.tick_params(axis='x', rotation=40, rotation_mode='xtick')
4744
axs[0].set_title('Default Date Formatter')
4845
plt.show()
4946

galleries/examples/units/evans_test.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,11 @@ def default_units(x, axis):
7777
# plot specifying units
7878
ax2.plot(x, y, 'o', xunits=2.0)
7979
ax2.set_title("xunits = 2.0")
80-
plt.setp(ax2.get_xticklabels(), rotation=30, ha='right')
80+
ax2.tick_params(axis='x', rotation=30, rotation_mode='xtick')
8181

8282
# plot without specifying units; will use the None branch for axisinfo
8383
ax1.plot(x, y) # uses default units
8484
ax1.set_title('default units')
85-
plt.setp(ax1.get_xticklabels(), rotation=30, ha='right')
85+
ax1.tick_params(axis='x', rotation=30, rotation_mode='xtick')
8686

8787
plt.show()

lib/matplotlib/figure.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1390,8 +1390,7 @@ def align_xlabels(self, axs=None):
13901390
Example with rotated xtick labels::
13911391
13921392
fig, axs = plt.subplots(1, 2)
1393-
for tick in axs[0].get_xticklabels():
1394-
tick.set_rotation(55)
1393+
axs[0].tick_params(axis='x', rotation=55)
13951394
axs[0].set_xlabel('XLabel 0')
13961395
axs[1].set_xlabel('XLabel 1')
13971396
fig.align_xlabels()

0 commit comments

Comments
 (0)