我正在尝试创建一个也应该显示标准差的图。这是我的数据
y20_6 = np.array([18351.6,17976.6,16101.6])
y20_12 = np.array([15664.1,16984.4,18304.7])
y20_18 = np.array([13031.3,13218.8,15468.8])
y80_6 = np.array([17109.4,16890.65,16671.9])
y80_12 = np.array([15867.2,18265.6,16132.8])
y80_18 = np.array([18304.2,17070.3,15539.1])
i20_6 = np.array([11375,11070.3,10398.4])
i20_12 = np.array([11273.4,10929.7,11304.7])
i20_18 = np.array([11789.1,10507.8,12507.8])
i80_6 = np.array([11906.3,9421.88,10218.8])
i80_12 = np.array([9750,10335.95,10921.9])
i80_18 = np.array([10109.4,10660.15,11210.9])
我采用了每组的平均值来使用以下代码创建绘图
x = np.array([0,1,2])
y20 = [sum(sub_list) / len(sub_list) for sub_list in np.array([y20_6, y20_12, y20_18])]
y80 = [sum(sub_list) / len(sub_list) for sub_list in np.array([y80_6, y80_12, y80_18])]
i20 = [sum(sub_list) / len(sub_list) for sub_list in np.array([i20_6, i20_12, i20_18])]
i80 = [sum(sub_list) / len(sub_list) for sub_list in np.array([i80_6, i80_12, i80_18])]
z = np.array([10518.2, 10518.2, 10518.2])
my_xticks = ['6 hrs','12 hrs','18 hrs']
plt.xticks(x, my_xticks)
plt.plot(x, y20, linestyle='-', marker='o', label='20 $^\circ$C, 175 $^\circ$C')
plt.plot(x, y80, linestyle='-', marker='v',label='80 $^\circ$C, 175 $^\circ$C')
plt.plot(x, i20, linestyle='-', marker='s',label='20 $^\circ$C, 275 $^\circ$C')
plt.plot(x, i80, linestyle='-', marker='x',color='black', label='80 $^\circ$C, 275 $^\circ$C')
plt.plot(x, z,linestyle='--', label='Referrence')
plt.legend(bbox_to_anchor=(1.35, 1.005), loc='upper right', borderaxespad=0)
plt.ylim([10000, 18000])
#plt.show()
plt.savefig('AL380', dpi=600, bbox_inches='tight')
示例图如下所示。我想创建一个类似的。
但是,我无法在图中添加标准差线条。请问有人可以帮我吗?
一种解决方案是您可以使用
plt.fill_between
:
# Fill the region between the upper and lower bounds of the standard deviation
plt.fill_between(x, np.subtract(mean_values, std_values), np.add(mean_values, std_values), alpha=0.2)
您只需要计算每个数据点的平均值和标准值。
seaborn.lineplot
:
示例:
import seaborn as sns
import maptlotlib.pyplot as plt
fmri = fmri = sns.load_dataset("fmri")
sns.lineplot(data=fmri, x="timepoint", y="signal", hue="event", errorbar=('sd', 1))
plt.show()
输出: