在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
ploting filled polygons in python
提问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
我有两个矩阵Tri和V要绘制的多边形的面 (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()



