参考链接
random.rand的函数说明
PatchCollection的实例化对象说明
set_array的函数说明
Circles, Wedges and Polygons
用Polygon和PatchCollection画一个五边形的代码如下,可修改r的具体值画出不同的
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
from matplotlib.collections import PatchCollection
fig, ax = plt.subplots()
N=5
patches = []
r = [[0.5,0.5],
[0.6,0.6],
[0.7,0.8],
[0.5,0.8],
[0.4,0.9]
]
polygon = Polygon(r, True)
patches.append(polygon)
colors = 100*np.random.rand(len(patches))
p = PatchCollection(patches, alpha=0.4)
p.set_array(np.array(colors))
ax.add_collection(p)
fig.colorbar(p, ax=ax)
plt.show()