有没有办法在这个正态分布的区域上色?
Is there a way to color in a region of this normal distribution?
我有一个分布图,但我想用一些颜色填充蓝色垂直虚线和分布图开始之间的区域。所以基本上,只需用颜色填充,箭头所在的区域:
我试过plt.fill_between...但它覆盖了整个地块而不是一个特定区域。怎么只填写一个区域?
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()
使用where
参数:
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()
输出:
我有一个分布图,但我想用一些颜色填充蓝色垂直虚线和分布图开始之间的区域。所以基本上,只需用颜色填充,箭头所在的区域:
我试过plt.fill_between...但它覆盖了整个地块而不是一个特定区域。怎么只填写一个区域?
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()
使用where
参数:
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()
输出: