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

Skip to content

Commit 2c54229

Browse files
authored
Merge pull request #13610 from timhoffm/example-centered_ticklabels
Update centered ticklabels example
2 parents 0f927c1 + 4dd06fb commit 2c54229

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

examples/ticks_and_spines/centered_ticklabels.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
"""
2-
===================
3-
Centered Ticklabels
4-
===================
2+
==============================
3+
Centering labels between ticks
4+
==============================
55
6-
sometimes it is nice to have ticklabels centered. Matplotlib currently
7-
associates a label with a tick, and the label can be aligned
8-
'center', 'left', or 'right' using the horizontal alignment property::
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::
99
1010
for label in ax.xaxis.get_xticklabels():
1111
label.set_horizontalalignment('right')
1212
13-
but this doesn't help center the label between ticks. One solution
14-
is to "fake it". Use the minor ticks to place a tick centered
15-
between the major ticks. Here is an example that labels the months,
16-
centered between the ticks
13+
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.
16+
17+
Here is an example that labels the months, centered between the ticks.
1718
"""
1819

1920
import numpy as np
@@ -34,7 +35,8 @@
3435
ax.plot(date, r.adj_close)
3536

3637
ax.xaxis.set_major_locator(dates.MonthLocator())
37-
ax.xaxis.set_minor_locator(dates.MonthLocator(bymonthday=15))
38+
# 16 is a slight approximation since months differ in number of days.
39+
ax.xaxis.set_minor_locator(dates.MonthLocator(bymonthday=16))
3840

3941
ax.xaxis.set_major_formatter(ticker.NullFormatter())
4042
ax.xaxis.set_minor_formatter(dates.DateFormatter('%b'))

0 commit comments

Comments
 (0)