这是一个计算数据凸壳的示例(我不知道应该如何计算矩形)并在 PolyCollection 的帮助下显示凸包 . 由于我没有你的数据,我生成了一些随机数据点,但它应该很容易适应 .
from matplotlib import pyplot as plt
import numpy as np
from scipy.spatial import ConvexHull
from matplotlib.collections import PolyCollection
fig = plt.figure()
ax1 = fig.add_subplot(111, projection='polar')
#generating some data:
C = np.random.rand(30)*15+40
h = np.random.rand(30)*15+290
h = np.radians(h) # convert values of the angle from degrees to radians
#following scipy example
points = np.concatenate([h,C]).reshape((2,30)).T
hull = ConvexHull(points)
ax1.scatter(h,C, s = 5, marker = 'o', color='b')
#adding the convex hull to ax1 as a PolyCollection:
ax1.add_collection(PolyCollection(
[points[hull.vertices,:]],
edgecolors='r',
facecolors='w',
linewidths=2,
zorder=-1,
))
ax1.set_rmax(60)
plt.show()
如果已经计算了矩形,则可以省略凸包计算并从那里生成 PolyCollection . 注意我用来创建 PolyCollection 的 **kwargs . 特别是 zorder 很重要,否则多边形将被绘制在点的顶部 . 结果图看起来像这样:
希望这可以帮助 .