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

Skip to content

Commit 70313d0

Browse files
committed
Update ticklabel alignment examples.
- Cross-reference the two (standard and mpl_toolkits) ticklabel alignment examples. - The standard example (align_ticklabels) is specifically about left-aligned yticklabels; update the title accordingly. - Present an alternative solution for left-aligned yticklabels, which should be more robust in practice (the padding needs to be guessed, whereas one can probably more often see what will be the widest string -- if needed, one can probably actually measure it using get_text_width_height_descent, though that would obscure the example here).
1 parent 2153774 commit 70313d0

2 files changed

Lines changed: 49 additions & 13 deletions

File tree

galleries/examples/axisartist/demo_ticklabel_alignment.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
Ticklabel alignment
44
===================
55
6+
Because axisartist groups all ticks into a single object, the global alignment
7+
of the ticks can be set directly.
8+
9+
See also :doc:`/gallery/ticks/align_ticklabels` for some workarounds that can
10+
be used when working with standard axes.
611
"""
712

813

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
1-
"""
2-
=================
3-
Align tick labels
4-
=================
1+
r"""
2+
==========================
3+
Left-aligned y tick labels
4+
==========================
55
66
By default, tick labels are aligned towards the axis. This means the set of
7-
*y* tick labels appear right-aligned. Because the alignment reference point
8-
is on the axis, left-aligned tick labels would overlap the plotting area.
9-
To achieve a good-looking left-alignment, you have to additionally increase
10-
the padding.
7+
y tick labels appear right-aligned.
8+
9+
To obtain left-aligned y tick labels, a solution is to force their
10+
horizontal-alignment to "left". However, because the alignment reference point
11+
is on the axis, such labels would overlap the plotting area, so the label
12+
padding needs to be additionally increased.
13+
14+
An alternate solution is to use the mathtext commands ``\rlap`` and
15+
``\phantom`` to manipulate the widths of the tick labels as seen by Matplotlib.
16+
See https://www.tug.org/TUGboat/tb22-4/tb72perlS.pdf for a detailed description
17+
of this approach.
18+
19+
See also :doc:`/gallery/axisartist/demo_ticklabel_alignment`.
1120
"""
21+
1222
import matplotlib.pyplot as plt
1323

1424
population = {
@@ -20,13 +30,34 @@
2030
"Shanghai": 21.9,
2131
}
2232

23-
fig, ax = plt.subplots(layout="constrained")
33+
fig, axs = plt.subplots(1, 2, layout="constrained")
34+
35+
# First solution: Force the horizontal-alignment of y tick labels to "left",
36+
# and increase the padding (to a manually chosen value).
37+
38+
ax = axs[0]
2439
ax.barh(population.keys(), population.values())
2540
ax.set_xlabel('Population (in millions)')
26-
27-
# left-align all ticklabels
2841
for ticklabel in ax.get_yticklabels():
2942
ticklabel.set_horizontalalignment("left")
30-
31-
# increase padding
3243
ax.tick_params("y", pad=70)
44+
45+
46+
# Second solution: Use mathtext to manipulate the width of ylabels as seen
47+
# by Matplotlib. Here, \rlap means "draw this text, but don't advance the
48+
# cursor", whereas \phantom means "advance the cursor by the width of the
49+
# enclosed text, but without actually drawing the text". The end result is
50+
# that the labels get aligned as if "Mexico City" was written every time, but
51+
# the real labels are actually drawn.
52+
53+
# Note that the widest label ("Mexico City") is still hard-coded here (it is
54+
# the widest *rendered* label, which is not necessarily the longest label in
55+
# characters).
56+
def left_aligned_label(s):
57+
return r"$\rlap{\text{%s}}\phantom{\text{Mexico City}}$" % s
58+
59+
ax = axs[1]
60+
ax.barh([*map(left_aligned_label, population)], population.values())
61+
ax.set_xlabel('Population (in millions)')
62+
63+
plt.show()

0 commit comments

Comments
 (0)