openbrain/语音情感/Open-CT/radarChart.py

66 lines
3.3 KiB
Python

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = 'SimSun' # 设置中文字体为宋体,可以根据系统中安装的字体来选择其他字体
# 时间片段数
datas = np.array([[10741, 284, 880, 492, 35, 194, 41],
[10141, 875, 1212, 331, 43, 558, 14],
[11774, 1016, 1213, 342, 38, 619, 16],
[9750, 1458, 907, 266, 48, 698, 9]])
# 频率
counts = np.array([[4391, 217, 597, 416, 29, 138, 31], [2635, 703, 525, 323, 28, 402, 7], [4433, 1171, 1097, 428, 53, 737, 24], [2277, 582, 760, 177, 40, 401, 15]])
# 平均持续时长
times = datas * 10 / counts
# 平均切换时间
switch = np.array([[61.14458998261721, 632.2479017581059, 547.6350413944383, 615.7969170851824, 177.65460729746445, 296.090922083119, 383.96258503401356], [51.27205013468541, 636.9488114264699, 524.3238970832911, 621.3669463807968, 345.9795321637427, 451.49136253673464, 278.94736842105266], [45.0405611983205, 396.79175015434987, 482.40695921154395, 508.47649889419984, 434.1753623188406, 503.01220812246856, 215.06280193236717], [59.312898420324544, 597.6178646532704, 396.5266118829706, 1049.9983278733278, 819.579794079794,
698.9298476374611, 329.31531531531533]])
# 归一化数据
datas = (datas - np.min(datas, axis=0)) / \
(np.max(datas, axis=0) - np.min(datas, axis=0))
counts = (counts - np.min(counts, axis=0)) / \
(np.max(counts, axis=0) - np.min(counts, axis=0))
times = (times - np.min(times, axis=0)) / \
(np.max(times, axis=0) - np.min(times, axis=0))
switch = (switch - np.min(switch, axis=0)) / \
(np.max(switch, axis=0) - np.min(switch, axis=0))
labels = ['中性', '喜悦', '厌恶蔑视', '愤怒', '恐惧担忧', '惊讶', '悲伤']
# 设置雷达图的角度
angles = np.linspace(0, 2*np.pi, len(labels), endpoint=False)
# 将角度进行闭合
angles = np.concatenate((angles, [angles[0]]))
# 创建一个包含四个子图的图表
fig, axes = plt.subplots(2, 2, subplot_kw=dict(polar=True))
new_datas = np.c_[datas, datas[:, 0]]
new_counts = np.c_[counts, counts[:, 0]]
new_times = np.c_[times, times[:, 0]]
new_switch = np.c_[switch, switch[:, 0]]
for i in range(2):
for j in range(2):
axes[i, j].plot(angles, new_datas[i*2+j], label='次数', linewidth=2)
axes[i, j].plot(angles, new_counts[i*2+j], label='频率', linewidth=2)
axes[i, j].plot(angles, new_times[i*2+j], label='平均持续时长', linewidth=2)
axes[i, j].plot(angles, new_switch[i*2+j], label='平均切换时长', linewidth=2)
# 设置雷达图的标签
for ax in axes.flat:
ax.set_thetagrids(angles[:-1] * 180/np.pi, labels)
# 在图表的中心位置添加图例
fig.legend(['次数', '频率', '平均持续时长', '平均切换时长'],
loc='center', bbox_to_anchor=(0.5, 0.5))
# 调整子图之间的间距
plt.tight_layout()
# 显示图形
plt.show()