import numpy as np
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
# 设置中文字体
plt.rcParams['font.sans-serif'] = ['SimHei'] # 使用黑体
plt.rcParams['axes.unicode_minus'] = False # 解决负号显示问题
# 1. 数据定义
dimensions = ['效率提升', '资源利用', '相对表现']
theta = np.array([0, 2*np.pi/3, 4*np.pi/3, 0]) # 闭合路径
data_non = [0.55, 0.51, 0.45, 0.55] # 非双一流数据
data_double = [0.58, 0.53, 0.48, 0.58] # 双一流数据
# 2. 创建极坐标轴
fig = plt.figure(figsize=(8, 6), facecolor='w')
ax = fig.add_subplot(111, polar=True)
plt.subplots_adjust(left=0.1, right=0.9, top=0.9, bottom=0.1)
# 3. 创建半透明填充效果
# 非双一流填充(蓝色)
ax.fill(theta, data_non, color=[0.2, 0.4, 0.8], alpha=0.1, zorder=1)
# 双一流填充(红色)
ax.fill(theta, data_double, color=[0.8, 0.2, 0.2], alpha=0.1, zorder=1)
# 4. 绘制主数据线
p1, = ax.plot(theta, data_non, 'b-o',
linewidth=2.5, markersize=10,
markerfacecolor=[0.2, 0.4, 0.8],
label='非双一流', zorder=3)
p2, = ax.plot(theta, data_double, 'r--s',
linewidth=2.5, markersize=10,
markerfacecolor=[0.8, 0.2, 0.2],
label='双一流', zorder=3)
# 5. 极坐标轴配置
ax.set_theta_offset(np.pi/2) # 0度位置在顶部
ax.set_theta_direction(-1) # 顺时针方向
ax.set_xticks(theta[:-1]) # 设置维度标签位置
ax.set_xticklabels(dimensions, fontsize=11)
ax.set_ylim(0.4, 0.6) # 径向范围
ax.set_yticks(np.arange(0.4, 0.61, 0.05)) # 径向刻度
ax.tick_params(axis='both', colors=[0.3, 0.3, 0.3], labelsize=11)
ax.grid(color=[0.5, 0.5, 0.5], alpha=0.3, linestyle='-', linewidth=1.2)
# 6. 添加标注
# 使用FontProperties确保中文显示
font_prop = FontProperties(fname='simhei.ttf', size=10, weight='bold')
ax.text(0, 0.57, '西部非双一流最低(0.52)',
color=[0.1, 0.3, 0.7],
fontproperties=font_prop,
ha='center', va='center',
bbox=dict(facecolor='white', alpha=0.7, edgecolor='none'))
ax.text(2*np.pi/3, 0.54, '全体普遍偏低',
color='k',
fontproperties=font_prop,
ha='center', va='center',
bbox=dict(facecolor='white', alpha=0.7, edgecolor='none'))
ax.text(4*np.pi/3, 0.465, '东部非双一流差距显著(0.42)',
color=[0.7, 0.1, 0.1],
fontproperties=font_prop,
ha='center', va='center',
bbox=dict(facecolor='white', alpha=0.7, edgecolor='none'))
# 7. 添加标题和图例
plt.title('高校表现三维度雷达图',
fontsize=14, fontweight='bold', color=[0.2, 0.2, 0.2],
pad=20)
# 创建图例并设置字体
legend_font = FontProperties(fname='simhei.ttf', size=11)
lgd = ax.legend(handles=[p1, p2], loc='upper right',
prop=legend_font,
frameon=True, edgecolor=[0.8, 0.8, 0.8])
# 设置图例文本颜色
for text in lgd.get_texts():
text.set_color([0.3, 0.3, 0.3])
# 8. 添加背景径向线
for t in np.linspace(0, 2*np.pi, 72):
ax.plot([t, t], [0.4, 0.6],
color=[0.9, 0.9, 0.9], alpha=0.2,
linewidth=0.5, zorder=0)
plt.tight_layout()
plt.show() 无法运行,修改一下
最新发布