Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib: plot number of observations per minute across all Mondays in a year

I have a pandas DataFrame where one of the columns is a bunch of dates (datetime type). I am trying to plot the number of observations per minute across all Mondays in a year against the minutes in the day.

For example, suppose I have two Mondays in my data and there are 3 observations at 09:01 on the first Monday and 4 observations at 09:01 on the second Monday. I would want to plot 7 (3+4) against 9*60+1=541 (That is, 09:01 is the 541st minute since the start of the day). Here is how I started:

def minutes_in_day(arg):
    #returns minute number in day  
    return arg.hour*60+arg.minute

def get_day(arg):
    return arg.isocalendar()[2]

# df is my pandas dataframe
df['day']=df['my_datetime_variable'].apply(get_day)
df['minute']=df['my_datetime_variable'].apply(minutes_in_day)
group=df.groupby(['day','minute'])
my_data=group['whatever_variable'].count()

my_data has two indices: a day index going from 1(Monday) to 7(Sunday) and a minute index going from potentially 0 to potentially 24*60-1=1439. How could I use matplotlib(pyplot) to plot the observation count against the minute index only when day index is 1?

like image 774
gjy Avatar asked Dec 05 '25 03:12

gjy


1 Answers

I think this is more or less what you want:

#import modules
import random as randy 
import numpy as np
import pandas as pd

#create sample dataset
idx=randy.sample(pd.date_range(start='1/1/2015',end='5/5/2015',freq='T'),2000)
idx.sort()
dfm=pd.DataFrame({'data':np.random.randint(0,2,len(idx))},index=idx)

#resample to fill in the gaps and groupby day of the week (0-6) and time
dfm=dfm.resample('T')
dfm=dfm.groupby([dfm.index.dayofweek,dfm.index.time]).count()

#Select monday (the '0th' day of the week) 
dfm=dfm.loc[0]

#plot
dfm.plot(title="Number of observations on Mondays",figsize=[12,5])

Gives

enter image description here

As you can read in the pandas.DatetimeIndex docs, the dayofweekattribute returns the day of the week with Monday=0 - Sunday=6 and the time attribute returns a numpy array of datetime.time.

like image 156
Pilik Avatar answered Dec 07 '25 19:12

Pilik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!