import matplotlib.pyplot as plt
import pandas as pd
from docx import Document
from docx.shared import Inches
plt.rcParams['font.sans-serif'] = ['SimHei'] # 选择黑体
plt.rcParams['axes.unicode_minus'] = False # 解决负号显示问题
data={
'学生':['张三','李四','王五','赵六'],
'数学':[100,87,60,99]
}
df=pd.DataFrame(data)
plt.figure(figsize=(8, 4))
plt.bar(df['学生'], df['数学'], color='skyblue')
plt.xlabel('学生')
plt.ylabel('数学')
plt.title('学生成绩柱状图')
plt.savefig('stuscorebar.png')
plt.show()
plt.close()
doc = Document()
doc.add_heading('学生分数分析', 0)
# 添加一个段落
p = doc.add_paragraph('柱状图')
doc.add_picture('stuscorebar.png', width=Inches(6))
doc.add_paragraph('表格')
table = doc.add_table(rows=1, cols=3,style="Light List Accent 1")
hdr_cells = table.rows[0].cells
hdr_cells[0].text = '编号'
hdr_cells[1].text = '姓名'
hdr_cells[2].text = '分数'
i=0
for name in data['学生']:
row_cells = table.add_row().cells
row_cells[0].text = str(i+1)
row_cells[1].text = name
row_cells[2].text = str(data['数学'][i])
i+=1
doc.save("学生分数分析.docx")