matplotlib set_minor_locator 不工作

mathplotlib set_minor_locator not working

我一直在尝试在 x 轴上设置一个次要的 DateTime 间隔,但它根本不显示。我在 google collabnote 书上这样做。我能够在重新 运行 之前让它工作,它似乎产生了与正确情节相去甚远的不同结果。任何建议都会很好,提前谢谢你。

import matplotlib.pyplot as plt
import matplotlib.dates as mdates


plt.style.use("dark_background")
eurusd['Close'] = pd.to_numeric(eurusd['Close'])
plt.figure(figsize=(40,10))
plt.xticks(rotation= 90)

ax = plt.gca()
xfmt = mdates.DateFormatter('%Y-%m-%d %H:%M:%S')
hours= mdates.HourLocator(interval=180)

ax.xaxis.set_major_formatter(xfmt)
ax.xaxis.set_major_locator(mdates.HourLocator(interval=720))

#issues with the minor locator not reflecting on plot
ax.xaxis.set_minor_locator(hours)
ax.spines['bottom'].set_linewidth(0.5)


plt.plot(eurusd['Local time'],eurusd['Close'],color='yellow', linewidth=1.5)

小音阶的设置方式必须与大音阶相同。

import matplotlib.pyplot as plt
import matplotlib.dates as mdates

plt.style.use("dark_background")
eurusd['Close'] = pd.to_numeric(eurusd['Close'])
plt.figure(figsize=(20,10))
# plt.xticks(rotation= 90)

ax = plt.gca()
xfmt = mdates.DateFormatter('%Y-%m-%d %H:%M:%S')
hours= mdates.HourLocator(interval=180)

ax.xaxis.set_major_formatter(xfmt)
ax.xaxis.set_major_locator(mdates.HourLocator(interval=720))

#issues with the minor locator not reflecting on plot
ax.xaxis.set_minor_formatter(xfmt)
ax.xaxis.set_minor_locator(hours)
ax.spines['bottom'].set_linewidth(0.5)

plt.plot(eurusd['Local_time'], eurusd['Close'], color='yellow', linewidth=1.5)

plt.setp(ax.xaxis.get_majorticklabels(), rotation=45)
plt.setp(ax.xaxis.get_minorticklabels(), rotation=45)
plt.show()