简体   繁体   English

与Pandas和Seaborn的约会日期

[英]Plot dates with Pandas and Seaborn

I have a DataFrame where each row is an event and it has a column of datetime values specifying the date and time of the event. 我有一个DataFrame,其中的每一行都是一个事件,它有一列datetime值,用于指定事件的日期和时间。

I just want to plot the amount of events for each day and be able to specify the start and end date of the x axis. 我只想绘制每天的事件量,并能够指定x轴的开始和结束日期。 How can I do that? 我怎样才能做到这一点?

Consider a DF containing a single column having datetime values as shown: 考虑包含一个具有日期时间值的单列的DF ,如下所示:

df = pd.DataFrame(pd.date_range('1/1/2016', periods=10, freq='D'), columns=['Date'])

在此处输入图片说明

Concatenate a sample of the original DF with itself to create duplicated values(say, 5) 将原始DF的样本与其自身连接以创建重复值(例如5)

df_dups = pd.concat([df, df.sample(n=5, random_state=42)], ignore_index=True)

Compute it's unique counts by stacking it into a series object. 通过将其堆叠到一系列对象中来计算其唯一计数。

plotting_df = df_dups.stack().value_counts().reset_index(name='counts')

Scatter Plot: 散点图:

As only numerical values are supported for both x and y axis as args for the built-in scatter plot method, we must call the plot_date function of matplotlib axes object to retain the dates as it is. 由于内置散点图方法的x和y轴均仅支持数值作为args,因此必须调用matplotlib轴对象的plot_date函数将日期保持plot_date

fig, ax = plt.subplots()
ax.plot_date(plotting_df['index'], plotting_df['counts'], fmt='.', color='k')
ax.set_ylim(0, plotting_df['counts'].values.max()+1)
fig.autofmt_xdate()
plt.xlabel('Date')
plt.ylabel('Counts')
plt.show()

图片

The amount/count of events is essentially a histogram where date is your datetime column: 事件的数量/数量实质上是一个直方图,其中date是您的datetime列:

df.date = df.date.astype("datetime64")
df.groupby(df.date.dt.day).count().plot(kind="scatter")

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

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