使用对代码进行的必要更改
matplotlib.dates
格式化
x-axis
对于
hh:mm
:
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import datetime as dt
import numpy as np
now = dt.datetime.now()
x = [now]
y = [0.5]
fig, ax = plt.subplots()
ax.scatter(x, y, marker="|", s=100000)
# Configure x-axis major and minor tick locators and formatter
ax.xaxis.set_major_locator(mdates.HourLocator(interval=1)) # Set major ticks every hour
ax.xaxis.set_minor_locator(mdates.MinuteLocator(interval=30)) # Set minor ticks every 30 minutes
ax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M')) # Format x-axis labels as 'hour:minute'
# Set x-axis limits from 00:00 to 23:59 of the current day
ax.set_xlim(now.replace(hour=0, minute=0), now.replace(hour=23, minute=59))
plt.ylim(0, 1)
plt.xticks(rotation=45)
plt.show()