I'm fairly new to the coding realm and have been trying to familiarize myself with code for data analysis. I'm trying to figure out how to insert an average of the dataset in this line graph. It contains data from multiple subjects across multiple days, and I'm curious to know if there's any way that I can take an "average" of the data, or the lines, and insert that into this code so that it's displayed on the graph. I've tried searching stack overflow and the matplotlib.org but have fallen short. Any help on this would be appreciated!
**为进一步说明:共有9个主题,每个主题的准确度范围从〜50%-100%。数据是在excel中编译的,该行具有“天”(1-22)和“主题”行(在给定的日期具有相应的准确度,例如,第1天为50%,第2天为65%,依此类推)。
这是我的代码:
importpandasaspdimportmatplotlib.pyplotaspltimportnumpyasnp
df=pd.read_excel('data.xlsx')plt.figure(figsize=(10,7))Day=df['Day']Accuracy=df[['Subject 1','Subject 2','Subject 3','Subject 4','Subject 5','Subject 6','Subject 7','Subject 8','Subject 9']]plt.plot(Day,Accuracy,alpha=0.3)plt.axis([1,22,0.55,1])plt.axhline(y=0.8,color='black',linestyle='--',alpha=0.3)plt.xlabel('Day')plt.ylabel('Accuracy')plt.title("Days to Acquisition by Subject")ax=plt.subplot()ax.set_xticks(Day)plt.show()
这就是我得到的:结果图
解决方案
importpandasaspdimportmatplotlib.pyplotaspltimportnumpyasnp
df=pd.read_excel('data.xlsx')plt.figure(figsize=(10,7))Day=df['Day']Accuracy=df[['Subject 1','Subject 2','Subject 3','Subject 4','Subject 5','Subject 6','Subject 7','Subject 8','Subject 9']]Accuracy_mean=df[['Subject 1','Subject 2','Subject 3','Subject 4','Subject 5','Subject 6','Subject 7','Subject 8','Subject 9']].mean(axis=1)plt.plot(Day,Accuracy,alpha=0.3)plt.plot(Day,Accuracy_mean)plt.axis([1,22,0.55,1])plt.axhline(y=0.8,color='black',linestyle='--',alpha=0.3)plt.xlabel('Day')plt.ylabel('Average Accuracy')plt.title("Days to Acquisition by Subject")ax=plt.subplot()ax.set_xticks(Day)plt.show()