使用 matplotlib 绘制雷达图无法正常工作

Plotting a Radar drawing with matplotlib not working correctly

我找到了这个资源:

https://towardsdatascience.com/how-to-make-stunning-radar-charts-with-python-implemented-in-matplotlib-and-plotly-91e21801d8ca

我正在尝试创建自己的版本,我将在其中分析情绪以进行免费文本调查。我将有 7 种情绪,而不是 5 个类别:

import numpy as np
import matplotlib.pyplot as plt


categories = ['Joy', 'Fear', 'Anger', 'Sadness', 'Disgust', 'Shame','Guilt']

q1 = [4, 4, 5, 4, 3, 7, 10]
q2 = [5, 5, 4, 5, 2,9,2]
q3 = [3, 4, 5, 3, 5,10,2]
q4 = [3, 4, 5, 3, 5,8,8]

label_loc = np.linspace(start=0, stop=2 * np.pi, num=len(q1))

plt.figure(figsize=(8, 8))
plt.subplot(polar=True)
plt.plot(label_loc, q1, label='q 1')
plt.plot(label_loc, q2, label='q 2')
plt.plot(label_loc, q3, label='q 3')
plt.plot(label_loc, q4, label='q 4')
plt.title('Answer to Question 1 - Emotion Analysis', size=20)
lines, labels = plt.thetagrids(np.degrees(label_loc), labels=categories)
plt.legend()
plt.show()

然而,快乐和内疚的情绪在这幅画中是重叠的。

我在这里错过了什么?

我认为原始源代码不正确。这会产生所需的输出:

import numpy as np
import matplotlib.pyplot as plt

categories = ['Joy', 'Fear', 'Anger', 'Sadness', 'Disgust', 'Shame','Guilt']

q1 = [4, 4, 5, 4, 3, 7, 10]
q2 = [5, 5, 4, 5, 2, 9, 2]
q3 = [3, 4, 5, 3, 5, 10, 2]
q4 = [3, 4, 5, 3, 5, 8, 8]

label_loc = np.linspace(start=0, stop=2*np.pi, num=len(q1)+1)

plt.figure(figsize=(8, 8))
plt.subplot(polar=True)
for q in [q1, q2, q3, q4]:
    plt.plot(label_loc, np.r_[q, q[0]], label='q 1')
plt.title('Answer to Question 1 - Emotion Analysis', size=20)
lines, labels = plt.thetagrids(np.degrees(label_loc), labels=categories)
plt.legend()
plt.show()