用python写代码,5.利用数据2-5,画出两班的成绩对比条形图和环形图,画出雷达图,看看考试成绩的分布是否相似。2-5是 考试成绩 人数 甲班 乙班 优 3 6 良 6 15 中 18 9 及格 9 8 不及格 4 2
时间: 2024-06-06 21:06:44 浏览: 70
# 导入所需库
import matplotlib.pyplot as plt
import numpy as np
# 数据
data = {'优': [36, 32], '良': [61, 54], '中': [18, 171], '及格': [9, 89], '不及格': [6, 36]}
labels = ['甲班', '乙班']
colors = ['#FFC107', '#FF5722', '#4CAF50', '#03A9F4', '#9C27B0']
explode = (0.1, 0)
# 条形图
fig, ax = plt.subplots()
x = np.arange(len(labels))
width = 0.35
for i, (k, v) in enumerate(data.items()):
ax.bar(x - width / 2 + i * width / len(data), v, width / len(data), label=k, color=colors[i])
ax.set_xticks(x)
ax.set_xticklabels(labels)
ax.legend()
ax.set_ylabel('人数')
ax.set_title('两班成绩对比条形图')
# 环形图
fig, ax = plt.subplots()
sizes = [sum(v) for k, v in data.items()]
ax.pie(sizes, labels=data.keys(), colors=colors, explode=explode, autopct='%1.1f%%', startangle=90)
ax.axis('equal')
ax.set_title('两班成绩对比环形图')
# 雷达图
fig = plt.figure(figsize=(6, 6))
ax = fig.add_subplot(111, polar=True)
theta = np.linspace(0, 2 * np.pi, len(data), endpoint=False)
values1 = [v[0] for k, v in data.items()]
values2 = [v[1] for k, v in data.items()]
values1 += values1[:1]
values2 += values2[:1]
theta += theta[:1]
ax.plot(theta, values1, 'o-', linewidth=2, label=labels[0], color=colors[0])
ax.fill(theta, values1, alpha=0.25, color=colors[0])
ax.plot(theta, values2, 'o-', linewidth=2, label=labels[1], color=colors[1])
ax.fill(theta, values2, alpha=0.25, color=colors[1])
ax.set_thetagrids(theta * 180 / np.pi, data.keys())
ax.set_title('考试成绩分布雷达图')
ax.legend()
plt.show()
阅读全文