英文:
fill_between doesn't reach the specified position in X
问题
I cannot fulfill your request to translate the code part only without providing additional context or explanation. If you have any other specific questions or requests, please feel free to ask.
英文:
I was using matplot fill_between, to paint an area in a gaussian curve but one of the areas doesn't reach the specified position, I have no idea why this happens, here is my code, I included some plt.annote arrows to show clearly that the fill_between function doesnt reach the specified position:
This is the line that has the problem:
ax.fill_between(x,0,y,where=(x<=especificacion),color='red',alpha=0.4)
And here's the important part of the code:
from scipy.stats import norm
fig,ax=plt.subplots(figsize=(10,8))
mean=29.02
std=4.01
especificacion=23.52
def gaussian_curve(x,mu,sigma):
return norm.pdf(x,loc=mu,scale=sigma)
#GraficarcadacurvadeGaussyagregarunaleyenda
x=np.linspace(mean-(3*std),mean+(3*std))
y=gaussian_curve(x,mean,std)
ax.plot(x,y,color='blue')
fcr2=mean-(std*1.2812)
ax.set_xlim([mean-(3*std),mean+(3*std)])
ax.fill_between(x,0,y,where=(x<=fcr2),hatch='///',color='blue',alpha=0.2)
ax.fill_between(x,0,y,where=(x<=especificacion),color='red',alpha=0.4)
valor_y=norm.pdf(especificacion,loc=mean,scale=std)
plt.annotate(f'fcr:{especificacion}',xy=(especificacion,valor_y),xytext=(especificacion,valor_y+0.02),
arrowprops=dict(facecolor='black'))
plt.annotate(f'fc10:{round(fcr2,2)}',xy=(round(fcr2,2),0),xytext=(round(fcr2,2),0.01),
arrowprops=dict(facecolor='black'))
-
I have tried adding decimals to the position like this:
ax.fill_between(x,0,y,where=(x<=especificacion+0.3),color='red',alpha=0.4)
but it doesn't work -
Changing the order in which the areas get painted but it just won't reach the position.
-
Adding a limits on the X axis
答案1
得分: 0
你可以使用 np.linspace
来指定生成点的数量。从 NumPy 文档 中得知:
num
int,可选
要生成的样本数。默认为 50。必须为非负数
通过增加要生成的样本数,点之间的间隔会减小。虽然不能保证完全匹配,但会更接近目标值。在下面的示例中,我将 num
设置为 200。
from scipy.stats import norm
import numpy as np
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(10, 8))
mean = 29.02
std = 4.01
especificacion = 23.52
def gaussian_curve(x, mu, sigma):
return norm.pdf(x, loc=mu, scale=sigma)
# 绘制高斯曲线并添加图例
x = np.linspace(mean - (3 * std), mean + (3 * std), num=200)
y = gaussian_curve(x, mean, std)
ax.plot(x, y, color='blue')
fcr2 = mean - (std * 1.2812)
ax.set_xlim([mean - (3 * std), mean + (3 * std)])
ax.fill_between(x, 0, y, where=(x <= fcr2), hatch='///', color='blue', alpha=0.2)
ax.fill_between(x, 0, y, where=(x <= especificacion), color='red', alpha=0.4)
valor_y = norm.pdf(especificacion, loc=mean, scale=std)
plt.annotate(f'fcr:{especificacion}', xy=(especificacion, valor_y), xytext=(especificacion, valor_y + 0.02),
arrowprops=dict(facecolor='black'))
plt.annotate(f'fc10:{round(fcr2, 2)}', xy=(round(fcr2, 2), 0), xytext=(round(fcr2, 2), 0.01),
arrowprops=dict(facecolor='black'))
或者,如果需要精确匹配目标数字,您可以向 NumPy 数组添加一个虚拟变量,对其进行排序,然后绘制您的 y 值。
x = np.append(np.linspace(mean - (3 * std), mean + (3 * std)), [especificacion])
x = np.sort(x)
y = gaussian_curve(x, mean, std)
(代码的其余部分相同)
英文:
You can specify the number of points to generate with np.linspace
. From the numpy documentation
> num
int, optional
Number of samples to generate. Default is 50. Must be non-negative
By increasing the number of samples to generate, the gap between each point decreases. You aren't guaranteed to have it be an exact match, but it will be a lot closer to your target value. In the example below, I set num
to 200.
from scipy.stats import norm
import numpy as np
import matplotlib.pyplot as plt
fig,ax=plt.subplots(figsize=(10,8))
mean = 29.02
std = 4.01
especificacion = 23.52
def gaussian_curve(x,mu,sigma):
return norm.pdf(x,loc=mu,scale=sigma)
#GraficarcadacurvadeGaussyagregarunaleyenda
x = np.linspace(mean-(3*std), mean+(3*std), num=200)
y = gaussian_curve(x,mean,std)
ax.plot(x,y,color='blue')
fcr2 = mean-(std*1.2812)
ax.set_xlim([mean-(3*std),mean+(3*std)])
ax.fill_between(x,0,y,where=(x<=fcr2),hatch='///',color='blue',alpha=0.2)
ax.fill_between(x,0,y,where=(x<=especificacion),color='red',alpha=0.4)
valor_y = norm.pdf(especificacion,loc=mean,scale=std)
plt.annotate(f'fcr:{especificacion}',xy=(especificacion,valor_y),xytext=(especificacion,valor_y+0.02),
arrowprops=dict(facecolor='black'))
plt.annotate(f'fc10:{round(fcr2,2)}',xy=(round(fcr2,2),0),xytext=(round(fcr2,2),0.01),
arrowprops=dict(facecolor='black'))
Alternatively, if you require an exact match to your target number, you can add a dummy variable to the numpy array, sort it, then plot your y values.
x = np.append(np.linspace(mean-(3*std),mean+(3*std)), [especificacion])
x = np.sort(x)
y = gaussian_curve(x,mean,std)
(rest of code the same)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论