python日期打印不正确

2024-04-20 05:10:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图绘制我的数据,从日期'2015-05-01'到'2015-05-08'(5月1日至5月8日),这是我的代码

df = pd.read_csv(myfile.csv, parse_dates=[0], dayfirst=True)
df = df.set_index('timestamp')
df.index = df.index.to_datetime()
df.sort_index(inplace=True)

但是当我绘制并限制x轴时

axs.set_xlim('2015-05-01','2015-05-08')

我得到下面的数字

enter image description here

例如,如果运行代码

df[3000:3005]

我明白了

                          A
     timestamp  
2015-03-05 13:51:00    71.000000
2015-03-05 13:52:00    71.600000
2015-03-05 13:53:00    72.500000
2015-03-05 13:54:00    73.142857
2015-03-05 13:55:00    77.625000

我想是因为它认为2015-03-05是3月5日,但实际上是5月3日。你知道吗

那么我怎样才能得到显示YYYY-DD-MM而不是YYYY-MM-DD数据的图呢?你知道吗

编辑:我的原始数据

05/03/2015 07:37    60.6
05/03/2015 07:38    59.33333333
05/03/2015 07:39    65.625
05/03/2015 07:40    68.33333333
05/03/2015 07:41    61

我用下面的代码

fig, axs = plt.subplots(1,1, figsize=(15,4))
axs.plot( df.index, df.A , '*')
axs.legend(loc='lower right', prop={'size':19})
axs.set_xlim('2015-05-01','2015-05-08')
axs.set_ylim(0,200)
fig.show()

以及使用

axs.set_xlim('01-05-2015','08-05-2015')

给予

enter image description here


Tags: csv数据代码truedfindexfig绘制