本文介绍了在python中绘制填充的多边形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
对于要绘制的多边形的面(Nx3)和顶点(Mx3),我有两个矩阵Tri
和V
.是否有任何matplotlib(或其他替代方法)做到这一点?类似于Matlab命令
I have two matrices Tri
and V
for 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
patch('faces',Tri,'vertices',V,'facecolor',
'flat','edgecolor','none','facealpha',1)
推荐答案
我不确定Matlab可以做什么,但是您可以使用 matplotlib.patches.Polygon
.改编自文档中的示例:
I'm not exactly sure what matlab does, but you can draw a polygon using matplotlib.patches.Polygon
. Adapted from an example in the docs:
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()
这篇关于在python中绘制填充的多边形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!