“fill_between”未达到指定的X位置。

huangapple go评论69阅读模式
英文:

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'))

  1. 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

  2. Changing the order in which the areas get painted but it just won't reach the position.

  3. 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'))

“fill_between”未达到指定的X位置。

或者,如果需要精确匹配目标数字,您可以向 NumPy 数组添加一个虚拟变量,对其进行排序,然后绘制您的 y 值。

x = np.append(np.linspace(mean - (3 * std), mean + (3 * std)), [especificacion])
x = np.sort(x)
y = gaussian_curve(x, mean, std)
(代码的其余部分相同)

“fill_between”未达到指定的X位置。

英文:

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=&#39;blue&#39;)
fcr2 = mean-(std*1.2812)
ax.set_xlim([mean-(3*std),mean+(3*std)])
ax.fill_between(x,0,y,where=(x&lt;=fcr2),hatch=&#39;///&#39;,color=&#39;blue&#39;,alpha=0.2)
ax.fill_between(x,0,y,where=(x&lt;=especificacion),color=&#39;red&#39;,alpha=0.4)

valor_y = norm.pdf(especificacion,loc=mean,scale=std)

plt.annotate(f&#39;fcr:{especificacion}&#39;,xy=(especificacion,valor_y),xytext=(especificacion,valor_y+0.02),
arrowprops=dict(facecolor=&#39;black&#39;))
plt.annotate(f&#39;fc10:{round(fcr2,2)}&#39;,xy=(round(fcr2,2),0),xytext=(round(fcr2,2),0.01),
arrowprops=dict(facecolor=&#39;black&#39;))

“fill_between”未达到指定的X位置。

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)

“fill_between”未达到指定的X位置。

huangapple
  • 本文由 发表于 2023年5月22日 11:26:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76302866.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定