python数据雷达图
时间: 2023-08-11 19:28:52 浏览: 103
在 Python 中可以使用 matplotlib 库来绘制雷达图。以下是一个简单的例子:
```python
import numpy as np
import matplotlib.pyplot as plt
# 数据
categories = ['A', 'B', 'C', 'D', 'E']
values = [4, 3, 2, 5, 1]
# 计算角度
angles = np.linspace(0, 2*np.pi, len(categories), endpoint=False)
# 将第一个数据点重复以闭合图形
values = np.concatenate((values,[values[0]]))
angles = np.concatenate((angles,[angles[0]]))
# 绘图
fig = plt.figure()
ax = fig.add_subplot(111, polar=True)
ax.plot(angles, values, 'o-', linewidth=2)
ax.fill(angles, values, alpha=0.25)
ax.set_thetagrids(angles * 180/np.pi, categories)
ax.grid(True)
plt.show()
```
这个例子中,我们使用了 numpy 库来计算各个数据点的角度,然后使用 matplotlib 库来绘制雷达图。你可以根据自己的需求修改数据和参数,来绘制不同风格的雷达图。
相关问题
python雷达图详解_python绘制雷达图实例讲解
雷达图(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()
```
运行代码,我们可以看到绘制出来的雷达图:

这个雷达图表示该学生在语文、数学、英语、体育和艺术五个维度上的得分情况,可以用于对比不同学生在这五个维度上的能力。
python 数据分析雷达图
### 使用 Python 进行数据分析并创建雷达图
#### 推荐使用的库
对于绘制雷达图,`matplotlib` 是最常用的选择之一[^1]。然而,在追求更高级的功能如交互性和美观度时,可以考虑 `plotly` 库[^2]。
#### 创建静态雷达图示例 (基于 Matplotlib)
下面是一个简单的例子来展示如何利用 `matplotlib` 绘制基本的雷达图:
```python
import numpy as np
import matplotlib.pyplot as plt
from math import pi
# 设置数据集
categories=['A','B','C','D']
values=[3,7,6,4]
N = len(categories)
angles=[n / float(N) * 2 * pi for n in range(N)]
values+=values[:1]
angles+=angles[:1]
ax = plt.subplot(polar=True)
plt.xticks(angles[:-1], categories)
ax.plot(angles, values)
ax.fill(angles, values, 'b', alpha=0.1)
plt.show()
```
这段代码展示了如何通过给定的一组类别及其对应的数值构建一个基础版本的雷达图,并填充颜色以增强视觉效果。
#### 创建动态雷达图示例 (基于 Plotly)
如果希望得到更加互动性的图表,则可以选择 `plotly` 来实现这一目标:
```python
import plotly.graph_objects as go
import pandas as pd
df = pd.DataFrame(dict(
r=[1, 5, 2, 2, 3],
theta=['processing cost','mechanical properties',
'chemical stability', 'thermal stability',
'device integration']))
fig = go.Figure()
fig.add_trace(go.Scatterpolar(
r=df['r'],
theta=df['theta'],
fill='toself'
))
fig.update_layout(
polar=dict(
radialaxis=dict(
visible=True,
range=[0, 5]
)),
showlegend=False
)
fig.show()
```
此段脚本说明了怎样借助于 `plotly` 的强大功能制作带有动画过渡效果以及良好用户体验界面下的雷达图。
阅读全文
相关推荐















