将y轴datetime.time值格式化为“%H:%M:%S.%f”

时间:2018-11-28 07:55:32

标签: python datetime matplotlib

我正在尝试将Y轴datetime.time值格式化为%H:%M:%S.%f

此代码:

fig, axes = plt.subplots(nrows=1, ncols=1, figsize=(20, 5), dpi=150)
axes.xaxis.set_major_formatter(mdates.DateFormatter('%d-%m-%Y'))

#axes.yaxis.set_major_formatter(mdates.DateFormatter('%H:%M:%S.%f'))

axes.plot_date(test_df["date"],test_df["time"],ydate=True)

plt.gcf().autofmt_xdate()

给予

output1

当我取消注释axes.yaxis.set_major_formatter时,该图在Y轴上显示零:

output2

你知道怎么了吗?

1 个答案:

答案 0 :(得分:0)

根据要求,我发布了用于其输出的df和代码。

df = pd.DataFrame({'time': ['7:30', '8:30', '7:30','10:30','11:30'], 'date': ['2018-10-01', '2018-10-02', '2018-10-03', '2018-10-04','2018-10-05']})

import matplotlib.dates as mdates
df['time'] = pd.to_datetime(df['time'], format='%H:%M') 
df['date'] = pd.to_datetime(df['date'], format='%Y-%m-%d')
fig, axes = plt.subplots(nrows=1, ncols=1)
axes.xaxis.set_major_formatter(mdates.DateFormatter('%d-%m-%Y'))
axes.yaxis.set_major_formatter(mdates.DateFormatter('%H:%M:%S'))
axes.plot_date(df["date"],df["time"],ydate=True)
plt.gcf().autofmt_xdate()
plt.show()

enter image description here

相关问题