I want to plot two time-series lines with mean and CI each for low and high diversity firms (variable div_ind
) throughout 8 calendar years (variable calyr
). May I know how to do it? Online sources mostly plot for one line, but I want two lines. Below are my codes which create errors.
import pandas as pd
import numpy as np
import math
import seaborn as sns
import os
import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib.pyplot import imread
df = pd.read_csv(r"data.csv")
lst = ['q','ret','lsale']
for i in lst:
stats = df.groupby(['calyr','div_ind'])[i].agg(['mean', 'count', 'sem'])
stats.columns = ['clayr','div_ind','mean', 'count', 'sem']
stats['95_ci'] = 1.96 * stats['sem']
print(stats)
# Plot error bars
plt.errorbar(x=stats['calyr'], y=stats['mean'], yerr=stats['95_ci'], fmt='o')
plt.title('95% Confidence Interval Error Bars for High and Low Diversity Firms')
plt.show()