Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fill between standard deviations on Matplotlib lineplot

I have a line plot which graphs 2 columns of data loaded from an excel file. See Plot: Matplotlib line plot

I want to display the standard deviation for each line (i.e., column of data) in my plot using the fill between function or something similar (like image 2), however I cannot figure out how to include this. Any help would be great. Please note that I am very new to this... How I would like my plot to look

Here is the code used for my lineplot:

import pandas as pd
import scipy as sp
from scipy import stats
import numpy as np
import matplotlib.pyplot as plt

Probability_fast_gamma_on_theta = pd.read_excel(r'C:\Users\RL\Excel work\Probability_of_a_theta_cycle_containing_fast_gamma.xlsx')

fig5, ax5=plt.subplots()
plt.plot(Probability_fast_gamma_on_theta.MIA)
plt.plot(Probability_fast_gamma_on_theta.CTL)

plt.grid(False)
plt.ylim(0.0, 0.6)
plt.xlim(0.0, 25)
plt.legend(['MIA', 'CTL'], loc='lower right')

plt.show()
like image 399
Rex Reginald Avatar asked Oct 17 '25 11:10

Rex Reginald


1 Answers

This is straightforwardly done using the plt.fill_between() function. A minimal example would be as follows:

import numpy as np
import matplotlib.pyplot as plt

fig, ax = plt.subplots()

x = np.linspace(0, 2 * np.pi, 50)
y = np.sin(x) + np.random.randn(len(x)) * 0.03

yerr0 = y - (0.1 + np.random.randn(len(x)) * 0.03)
yerr1 = y + (0.1 + np.random.randn(len(x)) * 0.03)

ax.plot(x, y, color='C0')
plt.fill_between(x, yerr0, yerr1, color='C0', alpha=0.5)
like image 169
t.wood Avatar answered Oct 20 '25 00:10

t.wood



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!