I have panel data with 50 different individuals over 60 months. I've created facet grids using Seaborn to plot the tone for each individual over time and now want to add vertical lines for when an event happens. Ideally, I want to add say a blue line for when event 1 occurs and a red line when event 2 occurs.
Sample of the data:
f_a tone t event1 event2
01_01 -1.9 0 0 0
01_01 -1.1 1 1 0
01_01 -2.5 2 0 0
01_01 -3.0 3 0 1
...
01_01 1.3 40 1 0
01_01 0.7 41 0 0
01_01 -0.6 42 0 0
01_01 -2.3 43 0 1
- 'f_a' is the ID (grouping variable)
- 'tone' is the y-axis
- 't' is the time variable
- 'event1' and 'event2' equal 1 if the event occurs during period t and zero otherwise.
Here's the code I have to create the plots:
# Initialize a grid of plots with an Axes for each pair
grid1 = sns.FacetGrid(df,col='f_a',hue='f_a',col_wrap=5,height=1.5)
#Draw a horizontal line to show the starting point
grid1.map(plt.axhline,y=0,ls=":",c=".5")
#Draw a line plot to show the trajectory of tone for each f-a pair over time
grid1.map(plt.plot,"t","tone",marker='o')
Here's a sample of the plot output: Plots
Here's the code I used to generate the data:
# Create list of observation pairs
fs = np.arange(1,11,1)
ans = np.arange(1,6,1)
f_a=[str(f).zfill(2)+'_'+str(a).zfill(2)
for f in fs for a in ans]
# Create dataframe with ARMA process by f_a pair with 60 months of observations of tone
# per pair
d={}
for f in f_a:
arparams = np.array([.5, .25])
maparams = np.array([.5, .3])
ar = np.r_[1,-arparams]
ma = np.r_[1, maparams]
y = sm.tsa.arma_generate_sample(ar,ma,60)
d[f]=y
df=pd.melt(pd.DataFrame(d)).rename(columns={'variable':'f_a','value':'tone'})
df['t']=df.groupby('f_a').cumcount()
# One occurrence of event 1 and 2 per f_a pair
up = [np.random.choice(np.arange(2,61,1)) for f in f_a]
down = [np.random.choice(np.arange(2,61,1)) for f in f_a]
# Dataframe with event 1 and 2
events=pd.DataFrame(data=[f_a,up,down]).T.rename(columns={0:'f_a',1:'event1_t',2:'event2_t'})
# Merge Datasets
df=df.merge(right=events,how='left',on='f_a')
# Create dummies for event1/event2
df['event1']=(df.t==df.event1_t)*1
df['event2']=(df.t==df.event2_t)*1
# Clean up dataset
df=df.drop(columns=['event1_t','event2_t'])