【发布时间】:2014-06-09 20:10:48
【问题描述】:
我有这样的数据:
col1 ;col2
2001-01-01;1
2001-01-01;2
2001-01-02;3
2001-01-03;4
2001-01-03;2
2001-01-04;2
我正在 Python/Pandas 中使用 pd.read_csv(...) 将其读取到 DataFrame 中。
现在我想每天在 y 轴上绘制 col2 和在 x 轴上绘制 col1 。我进行了很多搜索,但找不到太多非常有用的页面来详细描述这一点。我发现 matplotlib 目前不支持存储日期的数据格式(datetime64)。
我尝试像这样转换它:
fig, ax = plt.subplots()
X = np.asarray(df['col1']).astype(DT.datetime)
xfmt = mdates.DateFormatter('%b %d')
ax.xaxis.set_major_formatter(xfmt)
ax.plot(X, df['col2'])
plt.show()
但这不起作用。 什么是最好的方法? 我只能在那里找到一些位和位,但没有真正完整的工作,更重要的是,与最新版本的 pandas/numpy/matplotlib 的此功能相关的最新资源。
我也有兴趣将此绝对日期转换为连续的日索引,即: 开始日期 2001-01-01 是第 1 天,因此数据如下所示:
col1 ;col2 ; col3
2001-01-01;1;1
2001-01-01;2;1
2001-01-02;3;2
2001-01-03;4;3
2001-01-03;2;3
2001-01-04;2;4
.....
2001-02-01;2;32
非常感谢您。
【问题讨论】:
标签: python numpy matplotlib plot pandas