使用 x 和 y 的数组调用绘图函数会做什么?查看第 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)
它是否填充了两条曲线之间的区域?我们可以控制这种填充物或设计其样式吗?我想填充具有透明度的平滑颜色。
谢谢!
最佳答案
来自the docs :
If x and/or y is 2-dimensional, then the corresponding columns will be plotted.
也就是说,如果我们有 a, b, c, d
所有 np.array
的大小都相同 s
,则命令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)
<小时/>
如果您想填充两条曲线之间的区域,请尝试 fill_between
。如果我没记错的话,你应该可以将它用作
plt.fill_between([x, y], [x, yhalf])
关于python - Matplotlib:用数组调用绘图函数有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43015690/