Note
Click here to download the full example code
Centering Day Labels with MidBusdayLocator¶
MidBusdayLocator places one tick at the midpoint of each business session.
This is useful for day labels on the minor axis: the label sits visually
centered inside the session rather than at 12:00.
Core code:
ax.xaxis.set_minor_locator(busdayaxis.MidBusdayLocator())
ax.xaxis.set_minor_formatter(mdates.DateFormatter("%a"))

import matplotlib.dates as mdates
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import busdayaxis
busdayaxis.register_scale()
# define dummy data
OPEN = 9
CLOSE = 17
num_days = 5
dates = pd.date_range("2025-01-06", periods=num_days * 24, freq="h")
returns = np.random.normal(0, 0.002, len(dates))
returns[(dates.hour <= OPEN) | (dates.hour > CLOSE)] = 0.0
returns[~np.is_busday(np.array(dates, dtype="datetime64[D]"))] = 0.0
prices = (1 + pd.Series(returns, index=dates)).cumprod()
fig, ax = plt.subplots(figsize=(10, 4))
ax.plot(dates, prices.values, linewidth=1.3)
ax.set_xscale("busday", bushours=(OPEN, CLOSE))
ax.xaxis.set_major_locator(busdayaxis.DayLocator())
ax.xaxis.set_major_formatter(mdates.DateFormatter(""))
ax.xaxis.set_minor_locator(busdayaxis.MidBusdayLocator())
ax.xaxis.set_minor_formatter(mdates.DateFormatter("%A"))
ax.xaxis.grid(True, which="major")
ax.set_title("Day labels centered in session using MidBusdayLocator")
_ = plt.tight_layout()
Total running time of the script: ( 0 minutes 0.497 seconds)
Download Python source code: plot_4_mid_busday_locator.py