Matplotlib熊猫情节日期时间

时间:2018-12-13 12:46:30

标签: python pandas matplotlib

我有一个问题,如何正确绘制我的熊猫数据文件的日期时间:

[{"city"=>"Atlanta"}, {"state"=>"IL"}, {"zip"=>"61723"}]

当我打印date_time列时,输出是正确的,但是在matplotlib图中,它整天都不正确。

print(df):

headers = ['date', 'time', 'level']
dtypes = {'date': 'str', 'time': 'str','level': 'float'}

filen = 'level-11-12-2018.lvm'
df = pd.read_csv(filen, delimiter=' ', header=None, names=headers, dtype= dtypes, parse_dates=[['date', 'time']])
fig, ax = plt.subplots()
ax.plot_date(df['date_time'], df['level'],'o', label='level')

您能帮我如何在绘图中获取正确的date_time格式: Plot with matplotlib date_time axis not like the print output

所以基本上应该是2018年12月11日,然后是一天中的时间序列。

1 个答案:

答案 0 :(得分:0)

问题是法国日期格式,如:08/12/2018 对于这样的法国日期,您应该使用:

df = pd.read_csv(filen, delimiter=' ', header=None, names=headers, dtype= dtypes, parse_dates=[['date', 'time']], dayfirst=True)

具有选项dayfirst = True,例如在此处编写的示例: Specifying date format when converting with pandas.to_datetime