gpt4 book ai didi

python - 曲线和对角线之间的填充区域

转载 作者:行者123 更新时间:2023-12-04 14:02:43 26 4
gpt4 key购买 nike

你能帮我解决这个问题吗?我想知道如何填充曲线和对角线(连接 (X min, Y min) 和 (X max, Y max) 的线)之间的区域。例如,在下图中我们如何填充上面的区域红色对角线和蓝色下方的区域。提前感谢您的时间和考虑。

import numpy as np
import matplotlib.pyplot as plt

fig, ax = plt.subplots(figsize = (6,4))
x=np.array([0,1,2,3,4,5,6,7,8,9,10])
y=np.array([0,4,5,5,4,5,6,8,9,9,15])

ax.plot(x,y, color = "red", lw=1.2)
ax.plot([x.min(),x.max()],[y.min(),y.max()], color = "black", lw=1.2)

plt.show()

最佳答案

fill_between()方法是专门为此设计的:

# Create empty figure 
_,ax = plt.subplots()
# Plot the two lines
ax.plot(x,x,color='C0')
ax.plot(x,y,color='C3')
# Plot the area with two conditions (x>y) and (x<=y)
ax.fill_between(x, x, y, where=(x > y), color='C0', alpha=0.2,
interpolate=True)
ax.fill_between(x, x, y, where=(x <= y), color='C3', alpha=0.2,
interpolate=True)

我们得到:

enter image description here

关于python - 曲线和对角线之间的填充区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69461334/

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