用python写代码,5.利用数据2-5,画出两班的成绩对比条形图和环形图,画出雷达图,看看考试成绩的分布是否相似。2-5是 甲班和乙班分别为优 3 6 良 6 15 中 18 9 及格 9 8 不及格 4 2
时间: 2024-05-25 12:17:39 浏览: 51
# 导入绘图库
import matplotlib.pyplot as plt
import numpy as np
# 数据2-5
class_a = [36, 615, 189, 98, 42]
class_b = [36, 510, 267, 120, 67]
# 绘制条形图
x = np.arange(len(class_a))
width = 0.35
fig, ax = plt.subplots()
rects1 = ax.bar(x - width/2, class_a, width, label='Class A')
rects2 = ax.bar(x + width/2, class_b, width, label='Class B')
ax.set_ylabel('Number of Students')
ax.set_title('Comparison of Grades between Class A and Class B')
ax.set_xticks(x)
ax.set_xticklabels(['优', '良', '中', '及格', '不及格'])
ax.legend()
fig.tight_layout()
plt.show()
# 绘制环形图
labels = ['优', '良', '中', '及格', '不及格']
sizes_a = class_a
sizes_b = class_b
fig, ax = plt.subplots(figsize=(6, 6))
ax.pie(sizes_a, labels=labels, radius=1, wedgeprops=dict(width=0.3, edgecolor='w'))
ax.pie(sizes_b, labels=labels, radius=0.7, wedgeprops=dict(width=0.3, edgecolor='w'))
centre_circle = plt.Circle((0, 0), 0.4, color='black', fc='white', linewidth=0)
fig.gca().add_artist(centre_circle)
ax.set_title('Comparison of Grades between Class A and Class B')
plt.show()
# 绘制雷达图
angles = np.linspace(0, 2*np.pi, len(labels), endpoint=False)
angles = np.concatenate((angles, [angles[0]]))
fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(6, 6), subplot_kw=dict(polar=True))
# Class A
values_a = class_a
values_a = np.concatenate((values_a, [values_a[0]]))
ax.plot(angles, values_a, 'o-', linewidth=2, label='Class A')
ax.fill(angles, values_a, alpha=0.25)
# Class B
values_b = class_b
values_b = np.concatenate((values_b, [values_b[0]]))
ax.plot(angles, values_b, 'o-', linewidth=2, label='Class B')
ax.fill(angles, values_b, alpha=0.25)
ax.set_thetagrids(angles[:-1] * 180/np.pi, labels)
ax.set_title("Comparison of Grades between Class A and Class B", y=1.08)
plt.legend(loc='upper right')
plt.show()
阅读全文