Is there a way to color in a region of this normal distribution?

Viewed 195

I have a distribution plot, but I want to fill in with some color the region between the blue vertical dashed line and the beginning of the distribution plot. So basically, just fill in with color, this area where the arrow is:

enter image description here

I tried doing plt.fill_between... but it's covering the entire plot rather than one specific area. How would I only fill in one region?

import numpy as np
from scipy import stats
import matplotlib.pyplot as plt

x = np.arange(-5, 5, 0.1) # x from -6 to 6 in steps of 0.1
y = 1 / np.sqrt(2 * np.pi) * np.exp(-x ** 2 / 4.)

std = np.std(y)

plt.plot(x,y, 'k')

plt.axvline(x=-20*std,color='blue', linestyle='--')
plt.fill_between(x,-4,10)
plt.ylim(0,.42)
plt.show()
1 Answers

Using where parameter:

import numpy as np
from scipy import stats
import matplotlib.pyplot as plt

x = np.arange(-5, 5, 0.1) # x from -6 to 6 in steps of 0.1
y = 1 / np.sqrt(2 * np.pi) * np.exp(-x ** 2 / 4.)

std = np.std(y)

plt.plot(x,y, 'k')

plt.fill_between(x,y,where = x<=-20*std)
plt.ylim(0,.42)
plt.show()

Output:

Related