Python:填充雷达图中的区域

标签 python matplotlib

这里我制作了这个雷达图:

enter image description here

我期待将多边形下方的灰色区域填充为其他颜色。我没有找到任何方法来做到这一点。

生成此雷达图的代码如下所示:

import matplotlib.pyplot as plt
from matplotlib.patches import Polygon

fig, ax = plt.subplots(figsize=(20, 10))
ax.axis('equal')         ## setting the axis so that we can draw circles

ax.set(xlim=(-10, 23), ylim = (-15, 15))

# drawing the circles
circle_1 = plt.Circle((0, 0), 1.1, fc='none', ec='#D6D6D6', lw=28, zorder=3)
circle_2 = plt.Circle((0, 0), 2.57, fc='none', ec='#D6D6D6', lw=27, zorder=3)
circle_3 = plt.Circle((0, 0), 4.05, fc='none', ec='#D6D6D6', lw=27, zorder=3)
circle_4 = plt.Circle((0, 0), 5.5, fc='none', ec='#D6D6D6', lw=27, zorder=3)
circle_5 = plt.Circle((0, 0), 7.0, fc='none', ec='#D6D6D6', lw=27, zorder=3)

ax.add_artist(circle_1)
ax.add_artist(circle_2)
ax.add_artist(circle_3)
ax.add_artist(circle_4)
ax.add_artist(circle_5)


radar_1 = Polygon([[0, 6.45], [4.3, 4.3], [5.46, 0.02], [6.5, -3.55], [-0.04, -2.25], [-5.5, -3.5], [-6.55, 0.0], [-0.33, 4.7]],
                   fc='#246864', lw=1.2, zorder=2)
radar_2 = Polygon([[0, 6.45], [4.3, 4.3], [5.46, 0.02], [6.5, -3.55], [-0.04, -2.25], [-5.5, -3.5], [-6.55, 0.0], [-0.33, 4.7]],
                  fc='none', ec='#000000', zorder=3)

ax.add_artist(radar_1)
ax.add_artist(radar_2)


ax.axis('off')
plt.show()

最佳答案

使用set_clip_path(),圆可以被多边形剪切:

import matplotlib.pyplot as plt
from matplotlib.patches import Polygon

fig, ax = plt.subplots(figsize=(20, 10))
ax.axis('equal')
ax.set(xlim=(-10, 23), ylim=(-15, 15))

vertices = [[0, 6.45], [4.3, 4.3], [5.46, 0.02], [6.5, -3.55], [-0.04, -2.25], [-5.5, -3.5], [-6.55, 0.0], [-0.33, 4.7]]
radar_1 = Polygon(vertices, fc='#246864', lw=1.2, zorder=1)
radar_2 = Polygon(vertices, fc='none', ec='#000000', zorder=4)
ax.add_patch(radar_1)
ax.add_patch(radar_2)

for rad in [1.1, 2.57, 4.05, 5.5, 7.0]:
    circle1 = plt.Circle((0, 0), rad, fc='none', ec='#D6D6D6', lw=27, zorder=2)
    ax.add_patch(circle1)
    circle2 = plt.Circle((0, 0), rad, fc='none', ec='turquoise', lw=27, zorder=3)
    circle2.set_clip_path(radar_2)
    ax.add_patch(circle2)

ax.axis('off')
plt.tight_layout()

enter image description here

关于Python:填充雷达图中的区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63139624/

相关文章:

python - yaxis.set_major_formatter 中的 f 字符串

python - 需要将所有文本转换为纯文本/ASCII(我认为?)

python - 在 Python 类型中声明元组的长度

python - 如何使用 python 在文件中的搜索模式的下一行插入字符串?

python - 背景中的matplotlib垂直空间

python - 两个不同的 tk Canvas 中的两个 matplotlib 图形

python - 如何在 Pandas 的时间序列图上绘制垂直线?

python - 为什么图像 block 在提取时会像素化,但在连接时会变得正常?

python - 如何在 Ubuntu 中为 python 安装 Oauth2.0

python - 在多个列上应用函数来创建多个新列