Вы можете установить соответствующий форматер в зависимости от границ графика.Это может выглядеть следующим образом.
import numpy as np
import datetime as dt
import matplotlib.pyplot as plt
from matplotlib.dates import DateFormatter, HourLocator, DayLocator
def plot_something(h, ax=None):
td = np.arange(0,h, np.timedelta64(1, "h"))
y = np.sin(np.linspace(0,h,len(td)))
t = np.datetime64("2018-11-27") + td
(ax or plt.gca()).plot(t, y)
fig, axes = plt.subplots(nrows=4)
plot_something(16, ax=axes[0])
plot_something(24, ax=axes[1])
plot_something(40, ax=axes[2])
plot_something(72, ax=axes[3])
def ticking(ax):
d = np.diff(ax.get_xlim())
if d <= 1:
ax.xaxis.set_major_formatter(DateFormatter("%H:%M:%S"))
elif d <= 2:
ax.xaxis.set_major_locator(HourLocator(byhour=(0,6,12,18)))
ax.xaxis.set_major_formatter(DateFormatter("%H:%M:%S"))
else:
ax.xaxis.set_major_locator(DayLocator())
ax.xaxis.set_major_formatter(DateFormatter("%Y.%m.%d"))
for ax in axes.flat:
ticking(ax)
fig.tight_layout()
plt.show()
