gpt4 book ai didi

python - matplotlib:使用 fill_between 制作彩色三角形

转载 作者:行者123 更新时间:2023-11-28 21:48:39 24 4
gpt4 key购买 nike

我用下面的代码在空间中绘制了随机三角形,但我想用颜色填充三角形。我知道 matplotlib 中的 fill_between() 函数,但是我不确定如何在下面的示例中实现它

import matplotlib.pyplot as plt

trianglex = [ 1, 10, 7, 1 ] #repeated last coordinates so that the last coordinate joins the first coordinate to form the outline of the triangle
triangley = [ 2, 8, 4, 2 ]

triangle2x = [ 13, 25, 21, 13]
triangle2y = [ 5, 7 , 14, 5 ]


plt.figure('Triangles')
for i in range(3):
plt.plot( trianglex, triangley, 'o-')

for i in range(3):
plt.plot( triangle2x, triangle2y, 'o-')

plt.show()

给出 Triangles

最佳答案

您可以按如下方式使用 plt.fill():

import matplotlib.pyplot as plt

trianglex = [ 1, 10, 7, 1 ]
triangley = [ 2, 8, 4, 2 ]
triangle2x = [ 13, 25, 21, 13]
triangle2y = [ 5, 7 , 14, 5 ]

plt.figure('Triangles')
for i in range(3):
plt.plot(trianglex, triangley, 'o-')
plt.fill(trianglex, triangley)

for i in range(3):
plt.plot( triangle2x, triangle2y, 'o-')
plt.fill(triangle2x, triangle2y)

plt.show()

输出

enter image description here

关于python - matplotlib:使用 fill_between 制作彩色三角形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34931864/

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