0

是否有任何 Python 函数(matplotlib 或 mayavi)可以执行与 Matlab 中的“填充”相同的任务?我需要的是,给定由一组点 x, y 和每个点的颜色向量 cfill(x, y, c)定义的多边形,将绘制由 (x, y) 定义的多边形,每个 (x[ i], y[i])。

4

1 回答 1

2

matplotlib它是否比 更直接matlab,您需要添加polygon到轴上。

from matplotlib.patches import Polygon

fig, ax = plt.subplots()
N = 5
polygon = Polygon(np.random.rand(N, 2), True, facecolor='r')
ax.add_patch(polygon)

注意:facecolor控制多边形的颜色,并接受字符串、RGBA 或 html 颜色代码作为值。

在此处输入图像描述

如果您有一组多边形并且每个多边形都需要具有不同的颜色,您可以使用路径集合:

from matplotlib.patches import Polygon
from matplotlib.collections import PatchCollection

fig, ax = plt.subplots()
N = 5
val = np.random.rand(N, 2, 3)
patches = [Polygon(val[:, :, i], True) for i in range(val.shape[-1])]
p = PatchCollection(patches, alpha=0.4)
p.set_array(np.random.rand(3))  # assign colors
ax.add_collection(p)
fig.colorbar(p)

在此处输入图像描述

于 2019-08-19T23:54:32.613 回答