gpt4 book ai didi

python - 在 y = 0 和 y 的正值之间填充

转载 作者:行者123 更新时间:2023-11-30 23:10:17 25 4
gpt4 key购买 nike

我正在练习 fill_ Between matplotlib 函数。

我想要做的是填充 y = 0 和 y 的正值之间的区域,并让 y = 0 和 y 的负值之间的区域不填充。

类似于:

import matplotlib.pyplot as plt

x = [1,2,3,4,5]
y = [0,2,-3,4,-5]

ypos = [i for i in y if i>0]

plt.plot(x,y)
plt.fill_between(x,0,ypos)
plt.show()

它给出的错误是ValueError:参数维度不兼容。我已经检查了一些可能的解决方案,但无法使其正常工作。

最佳答案

你可以使用

where=np.array(y)>0

限制填充区域的绘制位置并使用

interpolate=True

fill_between找到交点:

<小时/>
import numpy as np
import matplotlib.pyplot as plt

x = np.array([1,2,3,4,5])
y = np.array([0,2,-3,4,-5])
plt.plot(x,y)
plt.fill_between(x, 0, y, where=y>0, interpolate=True)
plt.show()

产量

enter image description here

关于python - 在 y = 0 和 y 的正值之间填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30765932/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com