python雷达图的相似度_python画雷达图

用雷达图做两个类别的特征对比,对比两个类别在各特征下的差异

类别

资产负债率

营业收入趋势

销售毛利率

现金比率

资产减值损失/营业总收入

营业收入波动比率

风险企业

0.9

0.14

0.27

0.92

0.94

0.68

正常企业

0.48

-0.12

0.13

0.27

0.05

0.46

雷达图对比

由于7个特征的量程都在0-1之间,因此不需要在做特征标准化,直接用原始值绘制雷达图

97d5e653712e

风险企业特征对比.png

python代码实现

import numpy as np

import matplotlib.pyplot as plt

matplotlib.rcParams["font.sans-serif"] = ["SimHei"] # 正常显示中文

matplotlib.rcParams["axes.unicode_minus"] = False # 正常显示负号

def radar_plot(a_array, b_array, a_label, b_label, labels, title, tick=True, axis_tick=False):

assert len(a_array) == len(b_array) == len(labels), "data array length must match labels length"

data_length = len(a_array)

angles = np.linspace(0, 2 * np.pi, data_length, endpoint=False)

angles = np.concatenate((angles, [angles[0]]))

a_array = np.concatenate((a_array, [a_array[0]]))

b_array = np.concatenate((b_array, [b_array[0]]))

plt.plot(figsize=(12, 12))

plt.polar()

plt.plot(angles, a_array, "go-", linewidth=1, label=a_label, ms=4, mfc="white", mec="g")

plt.plot(angles, b_array, "ro-", linewidth=1, label=b_label, ms=4, mfc="white", mec="r")

plt.fill(angles, a_array, facecolor="g", alpha=0.25)

plt.fill(angles, b_array, facecolor="r", alpha=0.25)

labels = np.array(labels)

plt.thetagrids(angles * 180 / np.pi, labels, fontproperties="SimHei", fontsize=20)

if tick: # 是否能显示雷达图上点的值

for i, j in enumerate(a_array):

plt.text(i - 0.4, j + 0.1, round(j, 2), ha="center", va="center", size=9)

for i, j in enumerate(b_array):

plt.text(i - 0.4, j + 0.1, round(j, 2), ha="center", va="center", size=9)

if not axis_tick: # 是否显示雷达图的纵轴刻度值

plt.tick_params(labelleft=False)

plt.legend(loc=0)

plt.title(title, fontsize=20)

plt.show()

if __name__ == "__main__":

a_array = [0.48, -0.12, 0.13, 0.27, 0.05, 0.46, 0.12 ]

b_array = [0.9, 0.14, 0.27, 0.92, 0.94, 0.68, 0.74]

radar_list_zh = ["资产负债率", "营业收入趋势" , "销售毛利率" , "现金比率", "资产减值损失/营业总收入", "营业收入波动比率" ]

radar_plot(a_array, b_array, a_label="正常企业", b_label="风险企业", labels=radar_list_zh, title="风险企业和正常企业比率指标对比雷达图", tick=True, axis_tick=False)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
雷达(Radar Chart),也称为蜘蛛网(Spider Chart)或星形(Star Chart),是一种多变量数据可视化方式,通常用于比较多个变量或维度的相对关系。 Python中使用`matplotlib`库可以绘制雷达。下面我将为你详细讲解如何使用Python绘制雷达。 首先,我们需要导入相关的库: ```python import numpy as np import matplotlib.pyplot as plt ``` 接下来,我们需要准备数据。假设我们要绘制一个学生的五项能力评估雷达,其中包括语文、数学、英语、体育和艺术五个维度的得分: ```python labels = np.array(['语文', '数学', '英语', '体育', '艺术']) data = np.array([90, 80, 85, 70, 60]) ``` 然后,我们需要计算出每个维度在雷达中的角度。因为雷达是一个圆形,所以每个维度的角度应该是均分360度,即每个角度应该是`360 / 数据维度个数`。代码如下: ```python angles = np.linspace(0, 2*np.pi, len(labels), endpoint=False) angles = np.concatenate((angles, [angles[0]])) ``` 接下来,我们需要将数据和角度转换成极坐标系下的坐标。这里我们可以使用`np.vstack()`函数将数据和第一个数据点组合起来,再使用`np.cos()`和`np.sin()`函数计算出每个数据点的坐标。代码如下: ```python data = np.concatenate((data, [data[0]])) coords = np.vstack((angles, data)).T coords = np.concatenate((coords, [coords[0]])) ``` 最后,我们可以使用`matplotlib`的`plot()`函数绘制出雷达。代码如下: ```python fig, ax = plt.subplots(figsize=(6, 6), subplot_kw=dict(polar=True)) ax.plot(angles, data, 'o-', linewidth=2) ax.fill(coords[:, 0], coords[:, 1], alpha=0.25) ax.set_thetagrids(angles * 180/np.pi, labels) ax.set_title('学生五项能力评估') ax.grid(True) ``` 完整的代码如下: ```python import numpy as np import matplotlib.pyplot as plt labels = np.array(['语文', '数学', '英语', '体育', '艺术']) data = np.array([90, 80, 85, 70, 60]) angles = np.linspace(0, 2*np.pi, len(labels), endpoint=False) angles = np.concatenate((angles, [angles[0]])) data = np.concatenate((data, [data[0]])) coords = np.vstack((angles, data)).T coords = np.concatenate((coords, [coords[0]])) fig, ax = plt.subplots(figsize=(6, 6), subplot_kw=dict(polar=True)) ax.plot(angles, data, 'o-', linewidth=2) ax.fill(coords[:, 0], coords[:, 1], alpha=0.25) ax.set_thetagrids(angles * 180/np.pi, labels) ax.set_title('学生五项能力评估') ax.grid(True) plt.show() ``` 运行代码,我们可以看到绘制出来的雷达: ![雷达](https://img-blog.csdnimg.cn/20211104121534521.png) 这个雷达表示该学生在语文、数学、英语、体育和艺术五个维度上的得分情况,可以用于对比不同学生在这五个维度上的能力。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值