1、雷达图
import matplotlib.pyplot as plt import numpy as np # ====================== 全局配置(中文显示) ====================== plt.rcParams['font.sans-serif'] = ['SimHei'] plt.rcParams['axes.unicode_minus'] = False # -------------------------- 1. 准备数据 -------------------------- # 维度标签(比如产品的5个性能维度) categories = ['性能', '易用性', '稳定性', '扩展性', '性价比'] # 系列1数据(产品A) values_a = [85, 90, 80, 75, 95] # 系列2数据(产品B) values_b = [70, 85, 90, 88, 75] # 维度数量 n = len(categories) # 计算每个维度的角度(0到2π),并闭合雷达图(最后补第一个角度) angles = np.linspace(0, 2 * np.pi, n, endpoint=False).tolist() angles += angles[:1] # 闭合角度 # 闭合数据(最后补第一个值) values_a += values_a[:1] values_b += values_b[:1] categories += categories[:1] # 闭合标签 # -------------------------- 2. 创建雷达图 -------------------------- # 设置画布大小 fig, ax = plt.subplots(figsize=(8, 8), subplot_kw=dict(projection='polar')) # 绘制产品A的雷达图(填充半透明颜色) ax.plot(angles, values_a, 'o-', linewidth=2, label='产品A', color='#FF6B6B') ax.fill(angles, values_a, alpha=0.25, color='#FF6B6B') # 绘制产品B的雷达图 ax.plot(angles, values_b, 'o-', linewidth=2, label='产品B', color='#4ECDC4') ax.fill(angles, values_b, alpha=0.25, color='#4ECDC4') # -------------------------- 3. 样式美化 -------------------------- # 设置角度标签(对应维度) ax.set_xticks(angles[:-1]) # 去掉重复的最后一个角度 ax.set_xticklabels(categories[:-1], fontsize=12, fontfamily='SimHei') # 支持中文 # 设置径向刻度(数值范围) ax.set_ylim(0, 100) ax.set_yticks(np.arange(20, 101, 20)) ax.set_yticklabels([f'{x}分' for x in np.arange(20, 101, 20)], fontsize=10) # 隐藏径向标签的重叠 ax.set_rlabel_position(30) # 添加标题和图例 plt.title('产品多维度性能对比雷达图', fontsize=16, fontfamily='SimHei', pad=20) plt.legend(loc='upper right', bbox_to_anchor=(1.2, 1.0), fontsize=12) # 优化布局 plt.tight_layout() # 显示/保存图片 plt.show() # plt.savefig('radar_chart.png', dpi=300, bbox_inches='tight')
![]()
2、玫瑰图
import matplotlib.pyplot as plt import numpy as np # ====================== 全局配置(中文显示) ====================== plt.rcParams['font.sans-serif'] = ['SimHei'] plt.rcParams['axes.unicode_minus'] = False # -------------------------- 1. 准备数据 -------------------------- # 分类标签(比如不同月份的销售额) categories = ['1月', '2月', '3月', '4月', '5月', '6月'] # 对应数值 values = [58, 72, 65, 88, 78, 95] # 维度数量 n = len(categories) # 计算每个分类的角度(0到2π) angles = np.linspace(0, 2 * np.pi, n, endpoint=False) # 柱状图宽度(均匀分配) width = 2 * np.pi / n # -------------------------- 2. 创建玫瑰图 -------------------------- fig, ax = plt.subplots(figsize=(8, 8), subplot_kw=dict(projection='polar')) # 定义渐变颜色(可选) colors = plt.cm.Set3(np.linspace(0, 1, n)) # 绘制玫瑰图(核心:bar函数) bars = ax.bar( angles, # 每个柱子的角度 values, # 柱子高度(数值) width=width, # 柱子宽度 bottom=0, # 柱子底部起始位置 color=colors, # 柱子颜色 alpha=0.8, # 透明度 edgecolor='white',# 边框颜色 linewidth=1 # 边框宽度 ) # -------------------------- 3. 样式美化 -------------------------- # 设置角度标签 ax.set_xticks(angles) ax.set_xticklabels(categories, fontsize=12, fontfamily='SimHei') # 设置径向刻度(数值范围) ax.set_ylim(0, max(values) * 1.2) # 留出顶部空间 ax.set_yticks(np.arange(20, max(values)*1.2+1, 20)) ax.set_yticklabels([f'{x}万' for x in np.arange(20, max(values)*1.2+1, 20)], fontsize=10) # 调整极坐标起始角度(让第一个分类在顶部) ax.set_theta_zero_location('N') # 0度在正北方向 ax.set_theta_direction(-1) # 角度顺时针增加 # 为每个柱子添加数值标签 for bar, value in zip(bars, values): height = bar.get_height() ax.text( bar.get_x() + bar.get_width()/2, # 标签x坐标(柱子中心) height + 2, # 标签y坐标(柱子顶部+偏移) f'{value}万', # 标签文本 ha='center', va='bottom', # 对齐方式 fontsize=10, fontfamily='SimHei' ) # 添加标题 plt.title('上半年各月份销售额玫瑰图', fontsize=16, fontfamily='SimHei', pad=20) # 优化布局 plt.tight_layout() # 显示/保存图片 plt.show() # plt.savefig('rose_chart.png', dpi=300, bbox_inches='tight')
![]()