Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit3323161

Browse files
authored
Merge pull request#30763 from timhoffm/doc-align-ticklabels
DOC: Add example how to align tick labels
2 parents7bf7e47 +596bc2c commit3323161

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""
2+
=================
3+
Align tick labels
4+
=================
5+
6+
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.
11+
"""
12+
importmatplotlib.pyplotasplt
13+
14+
population= {
15+
"Sydney":5.2,
16+
"Mexico City":8.8,
17+
"São Paulo":12.2,
18+
"Istanbul":15.9,
19+
"Lagos":15.9,
20+
"Shanghai":21.9,
21+
}
22+
23+
fig,ax=plt.subplots(layout="constrained")
24+
ax.barh(population.keys(),population.values())
25+
ax.set_xlabel('Population (in millions)')
26+
27+
# left-align all ticklabels
28+
forticklabelinax.get_yticklabels():
29+
ticklabel.set_horizontalalignment("left")
30+
31+
# increase padding
32+
ax.tick_params("y",pad=70)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp