8000 Backport PR #13610 on branch v3.1.x (Update centered ticklabels example) by meeseeksmachine · Pull Request #13624 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Backport PR #13610 on branch v3.1.x (Update centered ticklabels example) #13624

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
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
24 changes: 13 additions & 11 deletions examples/ticks_and_spines/centered_ticklabels.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
"""
===================
Centered Ticklabels
===================
==============================
Centering labels between ticks
==============================

sometimes it is nice to have ticklabels centered. Matplotlib currently
associates a label with a tick, and the label can be aligned
'center', 'left', or 'right' using the horizontal alignment property::
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.xaxis.get_xticklabels():
label.set_horizontalalignment('right')

but this doesn't help center the label between ticks. One solution
is to "fake it". Use the minor ticks to place a tick centered
between the major ticks. Here is an example that labels the months,
centered between the ticks
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.

Here is an example that labels the months, centered between the ticks.
"""

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

ax.xaxis.set_major_locator(dates.MonthLocator())
ax.xaxis.set_minor_locator(dates.MonthLocator(bymonthday=15))
# 16 is a slight approximation since months differ in number of days.
ax.xaxis.set_minor_locator(dates.MonthLocator(bymonthday=16))

ax.xaxis.set_major_formatter(ticker.NullFormatter())
ax.xaxis.set_minor_formatter(dates.DateFormatter('%b'))
Expand Down
0