用 Pandas 和 Seaborn 绘制日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40308365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 02:18:13  来源:igfitidea点击:

Plot dates with Pandas and Seaborn

pythonpandasmatplotlibseaborn

提问by Marcelo Lazaroni

I have a DataFrame where each row is an event and it has a column of datetimevalues 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. How can I do that?

我只想绘制每天的事件数量,并能够指定 x 轴的开始和结束日期。我怎样才能做到这一点?

回答by Nickil Maveli

Consider a DFcontaining a single column having datetime values as shown:

考虑一个DF包含具有日期时间值的单列,如下所示:

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

enter image description here

在此处输入图片说明

Concatenate a sample of the original DFwith 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_datefunction of matplotlib axes object to retain the dates as it is.

由于 x 和 y 轴仅支持数值作为内置散点图方法的 args,因此我们必须调用plot_datematplotlib 轴对象的函数以保持日期原样。

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

Image

图片

回答by A.Kot

The amount/count of events is essentially a histogram where date is your datetime column:

事件的数量/计数本质上是一个直方图,其中日期是您的日期时间列:

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