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

Skip to content

MNT: Reduce the use of get_xticklabels() in examples #29602

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 2 additions & 13 deletions galleries/examples/subplots_axes_and_figures/shared_axis_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,10 @@

The example below shows how to customize the tick labels on the
various axes. Shared axes share the tick locator, tick formatter,
view limits, and transformation (e.g., log, linear). But the ticklabels
view limits, and transformation (e.g., log, linear). But the tick labels
themselves do not share properties. This is a feature and not a bug,
because you may want to make the tick labels smaller on the upper
axes, e.g., in the example below.

If you want to turn off the ticklabels for a given Axes (e.g., on
subplot(211) or subplot(212)), you cannot do the standard trick::

setp(ax2, xticklabels=[])

because this changes the tick Formatter, which is shared among all
Axes. But you can alter the visibility of the labels, which is a
property::

setp(ax2.get_xticklabels(), visible=False)

"""
import matplotlib.pyplot as plt
import numpy as np
Expand All @@ -42,6 +30,7 @@

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

# share x only
Expand Down
3 changes: 1 addition & 2 deletions galleries/examples/text_labels_and_annotations/date.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
# Text in the x-axis will be displayed in 'YYYY-mm' format.
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%b'))
# Rotates and right-aligns the x labels so they don't crowd each other.
for label in ax.get_xticklabels(which='major'):
label.set(rotation=30, horizontalalignment='right')
ax.xaxis.set_tick_params(rotation=30, rotation_mode='xtick')

plt.show()
18 changes: 6 additions & 12 deletions galleries/examples/ticks/centered_ticklabels.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@
Center labels between ticks
===========================

Ticklabels are aligned relative to their associated tick. The alignment
'center', 'left', or 'right' can be controlled using the horizontal alignment
property::

for label in ax.get_xticklabels():
label.set_horizontalalignment('right')
Tick labels are aligned relative to their associated tick, and are by default
centered.

However, there is no direct way to center the labels between ticks. To fake
this behavior, one can place a label on the minor ticks in between the major
ticks, and hide the major tick labels and minor ticks.
this behavior, one can place a minor tick in between the major ticks. Then
label the minor tick, and hide the minor tick lines and the major tick labels.

Here is an example that labels the months, centered between the ticks.
"""
Expand All @@ -34,15 +30,13 @@
# 16 is a slight approximation since months differ in number of days.
ax.xaxis.set_minor_locator(dates.MonthLocator(bymonthday=16))

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

# Remove the tick lines
# Remove the minor tick lines
ax.tick_params(axis='x', which='minor', tick1On=False, tick2On=False)

# Align the minor tick label
for label in ax.get_xticklabels(minor=True):
label.set_horizontalalignment('center')
imid = len(r) // 2
ax.set_xlabel(str(r["date"][imid].item().year))
plt.show()
5 changes: 1 addition & 4 deletions galleries/examples/ticks/date_concise_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,7 @@
for nn, ax in enumerate(axs):
ax.plot(dates, y)
ax.set_xlim(lims[nn])
# rotate_labels...
for label in ax.get_xticklabels():
label.set_rotation(40)
label.set_horizontalalignment('right')
ax.tick_params(axis='x', rotation=40, rotation_mode='xtick')
axs[0].set_title('Default Date Formatter')
plt.show()

Expand Down
4 changes: 2 additions & 2 deletions galleries/examples/units/evans_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ def default_units(x, axis):
# plot specifying units
ax2.plot(x, y, 'o', xunits=2.0)
ax2.set_title("xunits = 2.0")
plt.setp(ax2.get_xticklabels(), rotation=30, ha='right')
ax2.tick_params(axis='x', rotation=30, rotation_mode='xtick')

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

plt.show()
3 changes: 1 addition & 2 deletions lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -1390,8 +1390,7 @@ def align_xlabels(self, axs=None):
Example with rotated xtick labels::

fig, axs = plt.subplots(1, 2)
for tick in axs[0].get_xticklabels():
tick.set_rotation(55)
axs[0].tick_params(axis='x', rotation=55)
axs[0].set_xlabel('XLabel 0')
axs[1].set_xlabel('XLabel 1')
fig.align_xlabels()
Expand Down
Loading