首页 > 解决方案 > 如何在 matplolib 框架中以日期格式绘制数据框?

问题描述

我有以下数据框

date;A;B;C;D
2017-01-01 00:00:00;    7.45;   52.78;   11.46;  502.44
2017-01-01 01:00:00;   23.95;   47.21;   11.45;  502.42
2017-01-01 02:00:00;    0.96;   44.72;   11.44;  502.44
2017-01-01 03:00:00;   26.67;   42.48;   11.42;  502.42
2017-01-01 04:00:00;   22.42;   38.86;   11.41;  502.40
2017-01-01 05:00:00;   17.50;   39.47;   11.40;  502.39
2017-01-01 06:00:00;   16.78;   42.47;   11.38;  502.38

我想使用日期和列 A 绘制它。特别是我想使用日期自定义 x-ax,但使用固定间隔和格式,仅使用年、月和日。一切都在 matplotlib 框架中以便使用它的功能。

这就是我所做的:

1)读取数据框:

DATAF = pd.read_csv(fnamed+'.dat',sep=';',index_col=0, header = 0) 

2)然后我将其绘制为

ax.plot_date(DATAF.index.values,DATAF['A'].values)

结果似乎不可读:图不可读

一种方法是将数据索引转换为数据值,但之后我不知道如何以我想要的格式绘制日期。

提前致谢,

迭戈

标签: pythonpandasdatetimematplotlibformat

解决方案


@ImportanceOfBeingErnest 是对的。这就是我所做的,它似乎有效:

# Converting the index as date
DATAF.index = pd.to_datetime(DATAF.index)
ax.plot(DATAF.index.values,DATAF['A'].values)

非常感谢


推荐阅读