matplotlib:使用 fill_between 制作彩色三角形 [英] matplotlib: use fill_between to make coloured triangles

查看:35
本文介绍了matplotlib:使用 fill_between 制作彩色三角形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've plotted random triangles in space with code below but I want to fill the triangles with colour. I'm aware of the fill_between() function in matplotlib, however I am not sure how to implement it in the example below

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()

这给

推荐答案

你可以使用 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()

输出

这篇关于matplotlib:使用 fill_between 制作彩色三角形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆