X轴表示2024学年-2027学年,每个学年分上学期期中和期末考试,下学期期中和期末考试,Y轴2024学年-2027学年的语文,数学,英语的分数;2024上学期期中成绩:语文103,数学103,英语113.5;2024上学期期末成绩:语文104,数学121,英语126.7;2024年下学期期中成绩:语文102,数学119,英语115.2;2024年下学期期末成绩:语文104,数学91,英语122.1
时间: 2025-09-02 17:22:10 AIGC 浏览: 3
可以使用 Python 的`matplotlib`库和`pandas`库来构建成绩图表并进行相关数据处理。以下是具体的代码实现:
```python
import pandas as pd
import matplotlib.pyplot as plt
# 定义 2024 学年的考试名称和成绩
exams_2024 = ['2024 上学期期中', '2024 上学期期末', '2024 下学期期中', '2024 下学期期末']
chinese_scores_2024 = [103, 104, 102, 104]
math_scores_2024 = [103, 121, 119, 91]
english_scores_2024 = [113.5, 126.7, 115.2, 122.1]
# 创建 2024 学年的 DataFrame
data_2024 = {
'Exam': exams_2024,
'Chinese': chinese_scores_2024,
'Math': math_scores_2024,
'English': english_scores_2024
}
df_2024 = pd.DataFrame(data_2024)
# 生成 2025 - 2027 学年的考试名称
exams_2025_2027 = []
for year in range(2025, 2028):
exams_2025_2027.extend([f'{year} 上学期期中', f'{year} 上学期期末', f'{year} 下学期期中', f'{year} 下学期期末'])
# 为 2025 - 2027 学年的成绩填充 NaN
data_2025_2027 = {
'Exam': exams_2025_2027,
'Chinese': [None] * len(exams_2025_2027),
'Math': [None] * len(exams_2025_2027),
'English': [None] * len(exams_2025_2027)
}
df_2025_2027 = pd.DataFrame(data_2025_2027)
# 合并 2024 学年和 2025 - 2027 学年的数据
df = pd.concat([df_2024, df_2025_2027], ignore_index=True)
# 设置图片清晰度
plt.rcParams['figure.dpi'] = 300
# 创建图表
plt.figure(figsize=(12, 6))
plt.plot(df['Exam'], df['Chinese'], marker='o', label='Chinese')
plt.plot(df['Exam'], df['Math'], marker='s', label='Math')
plt.plot(df['Exam'], df['English'], marker='^', label='English')
# 设置图表标题和坐标轴标签
plt.title('2024 - 2027 Academic Year Scores')
plt.xlabel('Exams')
plt.ylabel('Scores')
# 显示图例
plt.legend()
# 旋转 x 轴标签以便更好显示
plt.xticks(rotation=45)
# 显示网格线
plt.grid(True)
# 显示图表
plt.tight_layout()
plt.show()
```
### 代码解释
1. **数据准备**:首先定义 2024 学年的考试名称和对应的语文、数学、英语成绩,然后创建 2024 学年的`DataFrame`。接着生成 2025 - 2027 学年的考试名称,并为这些学年的成绩填充`NaN`值,最后将 2024 学年和 2025 - 2027 学年的数据合并成一个`DataFrame`。
2. **图表绘制**:使用`matplotlib`库绘制折线图,分别绘制语文、数学、英语的成绩曲线,并设置图表的标题、坐标轴标签、图例等。
3. **图表显示**:旋转 x 轴标签以便更好显示,显示网格线,并最终显示图表。
阅读全文
相关推荐




















