用python中的matplotlib库,自己设定四组数据,根据数据画出画出第一组数据(五次考试学生成绩变化波动)的折线图,第二组数据(一次考试五科的具体成绩)的柱状图,第三组数据(五科成绩的各自比重)的饼图以及第四组数据(五科平均成绩和实际成绩)的雷达图,给出相应的代码
时间: 2024-03-05 19:51:52 浏览: 48
好的,以下是使用matplotlib库绘制这四种图形的代码:
1. 折线图
```python
import matplotlib.pyplot as plt
# 定义数据
exam_times = [1, 2, 3, 4, 5] # 考试次数
grades = [80, 85, 90, 87, 92] # 成绩
# 绘制折线图
plt.plot(exam_times, grades, color='blue', linewidth=2, marker='o', markersize=8)
plt.title('Five Exams Scores Changes', fontsize=16)
plt.xlabel('Exam Times', fontsize=12)
plt.ylabel('Grades', fontsize=12)
plt.tick_params(axis='both', which='major', labelsize=10)
plt.show()
```
2. 柱状图
```python
import matplotlib.pyplot as plt
# 定义数据
subjects = ['Math', 'Chinese', 'English', 'Physics', 'Chemistry'] # 科目
scores = [85, 92, 78, 90, 88] # 成绩
# 绘制柱状图
plt.bar(subjects, scores, color='green')
plt.title('Scores of Five Subjects', fontsize=16)
plt.xlabel('Subjects', fontsize=12)
plt.ylabel('Scores', fontsize=12)
plt.tick_params(axis='both', which='major', labelsize=10)
plt.show()
```
3. 饼图
```python
import matplotlib.pyplot as plt
# 定义数据
subjects = ['Math', 'Chinese', 'English', 'Physics', 'Chemistry'] # 科目
weights = [0.2, 0.3, 0.15, 0.2, 0.15] # 各科比重
# 绘制饼图
plt.pie(weights, labels=subjects, autopct='%1.1f%%', startangle=90)
plt.title('Weights of Five Subjects', fontsize=16)
plt.show()
```
4. 雷达图
```python
import matplotlib.pyplot as plt
import numpy as np
# 定义数据
subjects = ['Math', 'Chinese', 'English', 'Physics', 'Chemistry'] # 科目
mean_scores = [85, 92, 78, 90, 88] # 平均成绩
real_scores = [80, 90, 85, 92, 86] # 实际成绩
# 绘制雷达图
plt.figure(figsize=(6, 6))
angles = np.linspace(0, 2*np.pi, len(subjects), endpoint=False)
mean_scores = np.concatenate((mean_scores, [mean_scores[0]]))
real_scores = np.concatenate((real_scores, [real_scores[0]]))
angles = np.concatenate((angles, [angles[0]]))
plt.polar(angles, mean_scores, 'o-', linewidth=1, label='Mean Scores')
plt.fill(angles, mean_scores, alpha=0.25)
plt.polar(angles, real_scores, 'o-', linewidth=1, label='Real Scores')
plt.fill(angles, real_scores, alpha=0.25)
plt.thetagrids(angles * 180/np.pi, subjects, fontsize=12)
plt.legend(loc='upper right', bbox_to_anchor=(1.3, 1.0))
plt.title('Mean Scores and Real Scores of Five Subjects', fontsize=16, y=1.1)
plt.show()
```
阅读全文