Issue
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:
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()
Solution
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 -5 to 4.9 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:
Answered By - Rishabh Kumar
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.