1

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?

4
  • 3
    Can you post your sample data Commented Oct 28, 2016 at 15:42
  • Not sure how to do that, but imagine a dataframe with just one column, date. I want the X axis to be the date, and the Y axis to be the amount of times this date occurs. I'm having trouble with the dates in the x axis part. Commented Oct 28, 2016 at 15:49
  • Can you give us more details about: I'm having trouble with the dates in the x axis part.? Commented Oct 28, 2016 at 16:12
  • I do not know how to create a scatter plot where the x axis is a range of dates and the y axis is the amount of times each of these dates occurs in my DataFrame. How would I do that if I have, for example, a DataFrame with just one column, the column called dates which has one datetime value in each row? Commented Oct 28, 2016 at 16:18

2 Answers 2

5

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

enter image description here

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

Image

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.