简体   繁体   English

Plot datetime.date/时间序列中的一个 pandas dataframe

[英]Plot datetime.date / time series in a pandas dataframe

I created a pandas dataframe from some value counts on particular calendar dates.我根据特定日历日期的一些值计数创建了一个 pandas dataframe。 Here is how I did it:这是我的做法:

time_series = pd.DataFrame(df['Operation Date'].value_counts().reset_index())
time_series.columns = ['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.基本上,它是两列,第一个“日期”是一个包含datetime.date对象的列,第二列“计数”只是 integer 个值。 Now, I'd like to plot a scatter or a KDE to represent how the value changes over the calendar days.现在,我想要 plot 一个散点图或一个 KDE 来表示该值在日历日内的变化情况。

But when I try:但是当我尝试时:

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

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.我得到一个 plot,其中 x 轴从 -50 到 150,就好像它以某种方式将datetime.date对象解析为整数一样。 Also, it is yielding two identical plots rather than just one.此外,它产生了两个相同的图,而不仅仅是一个。

Any idea how I can plot them and see the calendars day along the x-axis?知道我如何才能 plot 他们并沿 x 轴查看日历日吗?

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')

在此输入图像描述

EDIT: 编辑:

if you want a scatter plot you can: 如果你想要一个散点图,你可以:

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

在此输入图像描述

If the column is datetime dtype (not object), then you can call plot() directly on the dataframe .如果该列是 datetime dtype(不是对象),那么您可以直接在 dataframe 上调用plot() You don't need to sort by date either, it's done behind the scenes if x-axis is datetime.您也不需要按日期排序,如果 x 轴是日期时间,它会在幕后完成。

df['date'] = pd.to_datetime(df['date'])
df.plot(x='date', y='count', kind='scatter', rot='vertical');

资源

You can also pass many arguments to make the plot nicer (add titles, change figsize and fontsize, rotate ticklabels, set subplots axis etc.) See the docs for full list of possible arguments.您还可以传递许多 arguments 以使 plot 更好(添加标题、更改图形大小和字体大小、旋转刻度标签、设置子图轴等)。请参阅文档以获取可能的 arguments 的完整列表。

df.plot(x='date', y='count', kind='line', rot=45, legend=None, 
        title='Count across time', xlabel='', fontsize=10, figsize=(12,4));

资源2

You can even use another column to color scatter plots.您甚至可以使用另一列为散点图着色。 In the example below, the months are used to assign color.在下面的示例中,月份用于分配颜色。 Tip: To get the full list of possible colormaps, pass any gibberish string to colormap and the error message will show you the full list.提示:要获得可能的颜色图的完整列表,请将任何乱码字符串传递给colormap图,错误消息将显示完整列表。

df.plot(x='date', y='count', kind='scatter', rot=90, c=df['date'].dt.month, colormap='tab20', sharex=False);

资源4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM