将数据帧(具有日期索引)绘制为条形图

2024-04-20 03:32:25 发布

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

我在这个领域是全新的,几乎所有的事情对我来说还是个谜。在

我有以下数据帧:

            usersPerWeek date
      date      
2018-03-07  127          2018-03-07
2018-03-14  3177         2018-03-14
2018-03-21  8758         2018-03-21
2018-03-28  16770        2018-03-28
2018-04-04  17964        2018-04-04

我试着把它画成一个简单的条形图:

^{pr2}$

这将导致错误消息Axis must havefreqset to convert to Periods

当我添加时(就在调用axis.bar之前)

axis.xaxis_date()

然后我得到错误消息'Period' object has no attribute 'toordinal'。在

我希望我错过了一些非常简单的东西,但到目前为止,谷歌并没有提供多少帮助。在

干杯


Tags: to数据消息convertdate错误事情领域
1条回答
网友
1楼 · 发布于 2024-04-20 03:32:25

它在下面的例子中似乎起作用了。在

from numpy.random import randint
import pandas as pd
import matplotlib.pyplot as plt

# Create data frames for the example
rng = pd.date_range('3/7/2018 00:00', periods=10, freq='1w')
df = pd.DataFrame({'usersPerWeek': randint(10, 200, 10)}, index=rng)

figPerWeek = plt.figure(dpi=80, figsize=(8,1))
axis = figPerWeek.add_subplot(1,1,1)
axis.bar(x=df.index, height=df.usersPerWeek)
plt.ylabel('usersPerWeek')
plt.show()

enter image description here

相关问题 更多 >