python雷达图详解_python如何绘制简单的雷达图

要在Excel工作表上绘制简单的雷达图,请使用add_chart()方法

# import xlsxwriter module

import xlsxwriter

# Workbook() takes one, non-optional, argument

# which is the filename that we want to create.

workbook = xlsxwriter.Workbook('chart_radar1.xlsx')

# The workbook object is then used to add new

# worksheet via the add_worksheet() method.

worksheet = workbook.add_worksheet()

# Create a new Format object to formats cells

# in worksheets using add_format() method .

# here we create bold format object .

bold = workbook.add_format({'bold': 1})

# create a data list .

headings = ['Number', 'Batch 1', 'Batch 2']

data = [

[2, 3, 4, 5, 6, 7],

[80, 80, 100, 60, 50, 100],

[60, 50, 60, 20, 10, 20],

]

# Write a row of data starting from 'A1'

# with bold format .

worksheet.write_row('A1', headings, bold)

# Write a column of data starting from

worksheet.write_column('A2', data[0])

worksheet.write_column('B2', data[1])

worksheet.write_column('C2', data[2])

# Create a chart object that can be added

# to a worksheet using add_chart() method.

# here we create a radar chart object .

chart1 = workbook.add_chart({'type': 'radar'})

# Add a data series to a chart

# using add_series method.

# Configure the first series.

# = Sheet1 !$A$1 is equivalent to ['Sheet1', 0, 0].

chart1.add_series({

'name': '= Sheet1 !$B$1',

'categories': '= Sheet1 !$A$2:$A$7',

'values': '= Sheet1 !$B$2:$B$7',

})

# Configure a second series.

# Note use of alternative syntax to define ranges.

# [sheetname, first_row, first_col, last_row, last_col].

chart1.add_series({

'name': ['Sheet1', 0, 2],

'categories': ['Sheet1', 1, 0, 6, 0],

'values': ['Sheet1', 1, 2, 6, 2],

})

# Add a chart title

chart1.set_title ({'name': 'Results of data analysis'})

# Add x-axis label

chart1.set_x_axis({'name': 'Test number'})

# Add y-axis label

chart1.set_y_axis({'name': 'Data length (mm)'})

# Set an Excel chart style.

chart1.set_style(11)

# add chart to the worksheet

# the top-left corner of a chart

# is anchored to cell E2 .

worksheet.insert_chart('E2', chart1)

# Finally, close the Excel file

# via the close() method.

workbook.close()

upload_003eaa060aa29cb62cec3aba57b0837b

  • 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、付费专栏及课程。

余额充值