|
|
1
ImportanceOfBeingErnest
8 年前
注意matplotlib不支持
datetime.time
values。不可否认,事实上,它似乎起到了一定的作用。
因此,您首先需要使用
datetime.datetime
导入日期时间
将matplotlib.pyplot导入为plt
图,ax=plt.子批次()。
ax.axhline(datetime.datetime(2018,7,24,12,0,0,0),color='blue',ls=--',lw=3)
ax.axhline(datetime.datetime(2018,7,24,18,0,0,0),color='red',ls=--',lw=3)
AxSCALE()
显示()
< /代码>
现在这已经给了你每小时的滴答声(巧合)。但是现在您当然可以使用自定义位置和格式,添加
from matplotlib.dates import hourlocator,dateformater
plt.gca().yaxis.set_major_locator(hourlocator())
plt.gca().yaxis.set_major_格式化程序(日期格式化程序(“%h:%m”))
< /代码>
正如问题中所述,将为您提供所需的输出。

首先需要使用datetime.datetime相反。
import datetime
import matplotlib.pyplot as plt
fig,ax=plt.subplots()
ax.axhline(datetime.datetime(2018,7,24,12,0,0,0),color='blue',ls='--',lw=3)
ax.axhline(datetime.datetime(2018,7,24,18,0,0,0),color='red',ls='--',lw=3)
ax.autoscale()
plt.show()

现在这已经给了你每小时的滴答声(巧合)。但是现在您当然可以使用自定义位置和格式,添加
from matplotlib.dates import HourLocator, DateFormatter
plt.gca().yaxis.set_major_locator(HourLocator())
plt.gca().yaxis.set_major_formatter(DateFormatter('%H:%M'))
正如问题中所述,将为您提供所需的输出。

|