Reputation: 63
I am trying to create a plot that should also show the standard deviation. Here is my data
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])
I have taken the average of each group to create the plot with the following code
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')
The example graph is shown below. I want to create a similar one.
However, I could not add a standard deviation line bar in the plot. Can anyone help me, please?
Upvotes: 2
Views: 1468
Reputation: 120509
Use seaborn.lineplot
as suggested in comments:
Example:
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()
Output:
Upvotes: 0
Reputation: 417
One solution is that you can use 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)
You just need to calculate the mean and std value at each data point.
Upvotes: 0