python - Matplotlib:用数组调用绘图函数有什么作用?

标签 python matplotlib

使用 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)

Plot

它是否填充了两条曲线之间的区域?我们可以控制这种填充物或设计其样式吗?我想填充具有透明度的平滑颜色。

谢谢!

最佳答案

来自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/

相关文章:

python - 如何在 matplotlib 中获取 x 和 y 截距?

Python-删除字典中的嵌套列表

python - 将 Pandas 列转换为 datetime64,包括缺失值

python - matplotlib imshow、ArtistAnimation 和类属性

matplotlib - mplot3d彩色散点数据堆叠显示优先级

javascript - 如何使用 mpld3 创建实时绘图

python - 为什么 stats.rv_continuous 始终返回相同的值?

python - 给定掩码标记边界

python - 在 Plotly 中更改/更新 xtick 标签和 ytick 标签不一致

Python - 绘制具有不均匀误差(高和低)的误差条形图