|
1 | 1 | """
|
2 |
| -=================== |
3 |
| -Centered Ticklabels |
4 |
| -=================== |
| 2 | +============================== |
| 3 | +Centering labels between ticks |
| 4 | +============================== |
5 | 5 |
|
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:: |
9 | 9 |
|
10 | 10 | for label in ax.xaxis.get_xticklabels():
|
11 | 11 | label.set_horizontalalignment('right')
|
12 | 12 |
|
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. |
17 | 18 | """
|
18 | 19 |
|
19 | 20 | import numpy as np
|
|
34 | 35 | ax.plot(date, r.adj_close)
|
35 | 36 |
|
36 | 37 | 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)) |
38 | 40 |
|
39 | 41 | ax.xaxis.set_major_formatter(ticker.NullFormatter())
|
40 | 42 | ax.xaxis.set_minor_formatter(dates.DateFormatter('%b'))
|
|
0 commit comments