| [ Web Proxy ] |
| Viewing: https://matplotlib.org/stable/api/_as_gen/../../gallery/animation/../ticks/centered_ticklabels.html | [Back] [Original] |
Section Navigation
fill_between with transparencyhisttype settingsplt.subplotsfloating_axes featuresNote
Go to the end to download the full example code.
Tick labels are aligned relative to their associated tick, and are by default centered.
However, there is no direct way to center the labels between ticks. To fake this behavior, one can place a minor tick in between the major ticks. Then label the minor tick, and hide the minor tick lines and the major tick labels.
Here is an example that labels the months, centered between the ticks.
[centered ticklabels]import matplotlib.pyplot as plt
import matplotlib.cbook as cbook
import matplotlib.dates as dates
import matplotlib.ticker as ticker
# Load some financial data; Google's stock price
r = cbook.get_sample_data('goog.npz')['price_data']
r = r[-250:] # get the last 250 days
fig, ax = plt.subplots()
ax.plot(r["date"], r["adj_close"])
ax.xaxis.set_major_locator(dates.MonthLocator())
# 16 is a slight approximation since months differ in number of days.
ax.xaxis.set_minor_locator(dates.MonthLocator(bymonthday=16))
# The NullFormatter removes the major tick labels
ax.xaxis.set_major_formatter(ticker.NullFormatter())
ax.xaxis.set_minor_formatter(dates.DateFormatter('%b'))
# Remove the minor tick lines
ax.tick_params(axis='x', which='minor', tick1On=False, tick2On=False)
imid = len(r) // 2
ax.set_xlabel(str(r["date"][imid].item().year))
plt.show()
Copyright 20022012 John Hunter, Darren Dale, Eric Firing, Michael Droettboom and the Matplotlib development team; 20122026 The Matplotlib development team.
Created using Sphinx 9.1.0.
Built from v3.11.2-1-g879ad8ebba.
Built with the PyData Sphinx Theme 0.16.1.
| Web Proxy Viewer | New URL | Original Page |