简体   繁体   中英

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.

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?

Consider a DF containing a single column having datetime values as shown:

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_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.

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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