I have a line graph that I'm making using matplotlib. I'm trying to add a fill behind the line such that the filled area is from current_val - standard_deviation to current_val + standard deviation. So far I've read the documentation and I was originally getting a fill line only at the y-intercept of the first coordinate.
My code is currently as follows:
data_field = 'Mean'
row_index = 'Rvolt'
x_axis = buckets
y_axis = df_result.loc[df_result.index.isin([row_index]) & df_result['Bucket'].between(1, 144), data_field].tolist()
plt.plot(x_axis, y_axis)
plt.xlabel("Bucket Spans")
plt.ylabel(data_field + " values for " + row_index)
#plt.fill(x_axis, y_axis)
plt.show()
How do I get it such that my graph eventually looks a little bit like 
For further disclosure, my x & y axes are lists.
Assuming your y_axis value is a numpy array. Otherwise you'll have to manually add / subtract the standard deviation from the y_axis variable using a for loop.
plt.fill_between(x, y_axis - standard_deviation, y_axis + standard_deviation)
I used @GWW's answer to implement fill_between, but I passed it a lower and upper bound list!
Here's my solution:
data_field = 'Mean'
row_index = 'Rvolt'
x_axis = buckets
y_axis = df_result.loc[df_result.index.isin([row_index]) & df_result['Bucket'].between(1, 144), data_field].tolist()
standevs = df_result.loc[df_result.index.isin([row_index]) & df_result['Bucket'].between(1, 144), 'StanDev'].tolist()
lower_bound = np.array(y_axis) - np.array(standevs)
upper_bound = np.array(y_axis) + np.array(standevs)
plt.plot(x_axis, y_axis)
plt.xlabel("Bucket Spans")
plt.ylabel(data_field + " values for " + row_index)
plt.fill_between(x_axis, lower_bound, upper_bound, facecolor='lightblue')
plt.show()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With