在python中绘制填充多边形

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26935701/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:11:36  来源:igfitidea点击:

ploting filled polygons in python

pythonmatplotlibplotpolygon

提问by ahmethungari

I have two matrices Triand Vfor faces (Nx3) and vertices (Mx3) of polygons that I want to plot. Is there any matplotlib (or any alternative) way to do that? Something similar to Matlab command

我有两个矩阵TriV要绘制的多边形的面 (Nx3) 和顶点 (Mx3)。是否有任何 matplotlib(或任何替代)方法来做到这一点?类似于 Matlab 命令的东西

patch('faces',Tri,'vertices',V,'facecolor', 
      'flat','edgecolor','none','facealpha',1)

回答by Hooked

I'm not exactly sure what matlab does, but you can draw a polygon using matplotlib.patches.Polygon. Adapted from an examplein the docs:

我不确定 matlab 是做什么的,但是您可以使用matplotlib.patches.Polygon. 改编自文档中的示例

import numpy as np

import matplotlib.pyplot as plt
import matplotlib
from matplotlib.patches import Polygon
from matplotlib.collections import PatchCollection

fig, ax = plt.subplots()
patches = []
num_polygons = 5
num_sides = 5

for i in range(num_polygons):
    polygon = Polygon(np.random.rand(num_sides ,2), True)
    patches.append(polygon)

p = PatchCollection(patches, cmap=matplotlib.cm.jet, alpha=0.4)

colors = 100*np.random.rand(len(patches))
p.set_array(np.array(colors))

ax.add_collection(p)

plt.show()

enter image description here

在此处输入图片说明