绘制datetime.date大 pandas [英] Plot datetime.date pandas

查看:77
本文介绍了绘制datetime.date大 pandas 的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我根据特定日历日期的一些值创建了一个熊猫数据框。这是我的操作方式:

I created a pandas dataframe from some value counts on particular calendar dates. Here is how I did it:

time_series = pd.DataFrame(df['Operation Date'].value_counts().reset_index())
time_series.columns = ['date', 'count']

,它是两列,第一列日期是具有 datetime.date 对象的列,第二列 count只是整数值。

Basically, it is two columns, the first "date" is a column with datetime.date objects and the second column, "count" are simply integer values.

现在,我想绘制散点图或KDE来表示日历日内的价值变化,但是如何?

Now, I'd like to plot a scatter or a KDE to represent the value changes over the calendar days, but how?

我查看了数据框,所有内容都按顺序排列,完全按照我上面的描述。但是当我尝试:

I looked at the dataframe and it all looks in order and exactly as I describe above. But when I try:

time_series.plot(kind='kde')
plt.show()

我得到一个绘图,其中x轴从-50到150,就好像它在解析 datetime.date 对象以某种方式作为整数。而且,它产生的是两个相同的图,而不仅仅是一个。

I get a plot where the x-axis is from -50 to 150 as if it is parsing the datetime.date objects as integers somehow. Also, it is yielding two identical plots rather than just one.

任何想法我都可以绘制它们并沿x轴查看日历日吗?

Any idea how I can plot them and see the calendars day along the x-axis?

推荐答案

您确定日期时间?我只是尝试了一下,而且效果很好:

you sure you got datetime? i just tried this and it worked fine:

df =    date    count
7   2012-06-11 16:51:32 1.0
3   2012-09-28 08:05:14 12.0
19  2012-10-01 18:01:47 4.0
2   2012-10-03 15:18:23 29.0
6   2012-12-22 19:50:43 4.0
1   2013-02-19 19:54:03 28.0
9   2013-02-28 16:08:40 17.0
12  2013-03-12 08:42:55 6.0
4   2013-04-04 05:27:27 6.0
17  2013-04-18 09:40:37 29.0
11  2013-05-17 16:34:51 22.0
5   2013-07-07 14:32:59 16.0
14  2013-10-22 06:56:29 13.0
13  2014-01-16 23:08:46 20.0
15  2014-02-25 00:49:26 10.0
18  2014-03-19 15:58:38 25.0
0   2014-03-31 05:53:28 16.0
16  2014-04-01 09:59:32 27.0
8   2014-04-27 12:07:41 17.0
10  2014-09-20 04:42:39 21.0

df = df.sort_values('date', ascending=True)
plt.plot(df['date'], df['count'])
plt.xticks(rotation='vertical')

编辑:

如果要散点图,可以:

plt.plot(df['date'], df['count'], '*')
plt.xticks(rotation='vertical')

这篇关于绘制datetime.date大 pandas 的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆