1 回答
TA贡献1871条经验 获得超8个赞
您的Time列未被识别为日期时间对象。您可以将列转换为日期时间,如下所示:
dataset.Time = pd.to_datetime(dataset.Time, format='%Y-%m-%d %H:%M:%S')
完整代码:
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import pandas as pd
from io import StringIO
d='''Day_of_the_week,Time
Monday,1899-12-30 08:15:00
Monday,1899-12-30 14:15:00
Tuesday,1899-12-30 16:15:00
Wednesday,1899-12-30 08:15:00
Thursday,1899-12-30 11:30:00
Thursday,1899-12-30 14:15:00
Thursday,1899-12-30 16:15:00
Friday,1899-12-30 11:30:00'''
dataset = pd.read_csv(StringIO(d), header=0)
dataset.Time = pd.to_datetime(dataset.Time, format='%Y-%m-%d %H:%M:%S')
plt.figure()
plt.scatter(dataset.Day_of_the_week,dataset.Time)
date_format = mdates.DateFormatter('%H:%M')
plt.gca().yaxis.set_major_formatter(date_format)
plt.tight_layout()
plt.show()
添加回答
举报