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

pic*_*olo 2 python matplotlib

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

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()
Run Code Online (Sandbox Code Playgroud)

这使 三角形

gtl*_*ert 5

您可以使用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()
Run Code Online (Sandbox Code Playgroud)

产量

在此输入图像描述