Matplotlib:用数组调用plot函数做什么?

2024-09-29 22:29:39 发布

您现在位置:Python中文网/ 问答频道 /正文

对x和y调用带有数组的plot函数做什么?看第15行。你知道吗

x1 = stats.norm.ppf(0.001)
x2 = stats.norm.ppf(0.999)

x = np.linspace(x1, x2, 100)

y = stats.norm.pdf(x)

yhalf = []
for i in range(len(x)):
    if i > len(x)/2:
        yhalf.append(y[i])
    else:
        yhalf.append(0)

plt.plot([x, x], [y, yhalf], 'y-')

plt.axes().set_xlim(x1, x2)

Plot

它是否填满了两条曲线之间的区域?我们能控制这种填充物吗?我想用透明的平滑颜色填充。你知道吗

谢谢!你知道吗


Tags: 函数normlenpdfplotstatsnpplt
1条回答
网友
1楼 · 发布于 2024-09-29 22:29:39

the docs

If x and/or y is 2-dimensional, then the corresponding columns will be plotted.

也就是说,如果我们有大小相同的a, b, c, d所有np.array,命令plt.plot([a, b], [c, d])将绘制s行数(a[0], c[0]) -> (b[0], d[0])。你知道吗

在您的例子中,yhalf[0:len(x)/2]是0,其他值与y相同。根据我之前所说的,你从(x[i], y[i]) -> (x[i], yhalf[i])中画线,这是

case 1: i <  len(x)/2: (x[i], y[i]) -> (x[i], 0)    <  the columns you see
case 2: i >= len(x)/2: (x[i], y[i]) -> (x[i], y[i]) <  lines with 0 length (== invisible)

如果要填充两条曲线之间的区域,请尝试^{}。如果我没记错的话,你应该可以把它当作

plt.fill_between([x, y], [x, yhalf])

相关问题 更多 >

    热门问题