Python绘制雷达图

发布时间 2023-05-08 17:53:01作者: 小鱼圆又圆

基本步骤

创建一个axe对象,创建时设置参数polar为True以使用极坐标系

绘制图像



示例代码

from matplotlib import pyplot as plt


ax = plt.subplot(111, polar=True)  # 设置绘制极坐标

xs = ['a', 'b', 'c', 'd', 'e', 'a']
ys = [1, 2, 4, 3, 1, 1]

ax.plot(xs, ys)  # 绘制
ax.set_thetagrids([i*180/5*2 for i in range(len(xs) - 1)], xs[:-1])  # 设置坐标轴

plt.show()
# plt.savefig('test.png')  # 如果要保存,记得注释上一行

要点

要点
通过设置plt.subplot函数的polar参数为True让坐标变为极坐标
通过在坐标列表最后额外加上第一个坐标让雷达图封闭
通过axe对象的set_thetagrids函数设置坐标轴,第一个参数是坐标值列表,第二个参数是坐标名列表