完成上机练习相关题目,将编程代码和结果整理到实验报告并提交
题目一:绘制展示各班男生、女生英语平均成绩的柱形图
编写程序。根据实例4的要求,绘制展示各班男生、女生英语平均成绩的柱形图,并将全体高二年级的英语平均成绩绘制成参考线。
实验代码:
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
men_means = (90.5, 89.5, 88.7, 88.5, 85.2, 86.6)
women_means = (92.7, 87.0, 90.5, 85.0, 89.5, 89.8)
ind = np.arange(len(men_means))
width = 0.2
fig = plt.figure()
ax = fig.add_subplot(111)
ax.bar(ind - width/2, men_means, width, label = '男生平均成绩')
ax.bar(ind + 0.2, women_means, width, label = '女生平均成绩')
ax.set_title('高二各班男生,女生英语平均成绩')
ax.set_ylabel('分数')
ax.set_xticks(ind)
ax.set_xticklabels(['高二1班', '高二2班', '高二3班', '高二4班',
'高二5班', '高二6班'])
ax.axhline(88.5, ls = '--', linewidth = 1.0, label = '全体平均成绩')
ax.legend(loc = "lower right")
plt.show()
实验结果:
题目二:绘制一个展示2013—2019财年阿里巴巴淘宝和天猫平台GMV的柱形图
编写程序。根据实例5的要求,绘制一个展示2013—2019财年阿里巴巴淘宝和天猫平台GMV的柱形图。
实验代码:
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
x = np.arange(1, 8)
y = np.array([10770, 16780, 24440, 30920, 37670, 48200, 57270])
bar_rects = plt.bar(x, y, tick_label = ['FY2013', 'FY2014', 'FY2015','FY2016',
'FY2017','FY2018','FY2019'], width=0.5)
def autolabel(rects):
for rect in rects:
height = rect.get_height()
plt.text(rect.get_x()+rect.get_width()/2, height+300, s='{}'.format(height),
ha='center', va = 'bottom')
autolabel(bar_rects)
plt.ylabel('GMV(亿元)')
plt.show()
实验结果:
题目三:绘制一个展示果酱面包配料比例的饼图
编写程序。根据实例6的要求,绘制一个展示果酱面包配料比例的饼图。
实验代码:
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
kinds = ['面粉', '全麦粉', '酵母', '苹果酱', '鸡蛋', '黄油', '盐', '白糖']
weight = [250, 150, 4, 250, 50, 30, 4, 20]
plt.pie(weight, autopct='%3.1f%%')
plt.legend(kinds, loc='upper right', bbox_to_anchor=[2.0, 0.8], ncol=4)
colors = ['pink','pink','pink','pink','pink','pink','pink','pink']
plt.table(cellText=[weight], cellLoc='center', rowLabels=['重量(g)'],
bbox=[1.2, 0.3, 0.8, 0.2], colLabels=kinds, loc='bottom', rowColours=['pink'], colColours=colors, cellColours= [colors])
plt.show()
实验结果:
题目四:全体男生、女生各科的平均成绩绘制柱形图,绘制堆积柱形图
编写程序。已知实验中学举行了高二期中模拟考试,考试后分别计算了全体男生、女生各科的平均成绩,统计结果如下表。
注:(1)绘制柱形图。柱形图的x轴为学科,y轴为平均成绩。
(2)绘制堆积柱形图。堆积柱形图的x轴为学科,y轴为平均成绩。
(3)设置y轴的标签为“平均成绩(分)”;
(4)设置x轴的刻度标签位于两组柱形中间;
(5)添加标题为“高二男生、女生的平均成绩”;
(6)添加图例;
(7)向每个柱形的顶部添加注释文本,标注平均成绩。
表2-10 全校高二男生、女生的平均成绩
学科 | 平均成绩(男) | 平均成绩(女) |
语文 | 85.5 | 94 |
数学 | 91 | 82 |
英语 | 72 | 89.5 |
物理 | 59 | 62 |
化学 | 66 | 49 |
生物 | 55 | 53 |
柱形图:
实验代码:
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams['font.sans-serif'] = [u'SimHei']
# 避免图例中无法显示中文
bar_wight = 0.3
x_title = ["语文", "数学", "英语", "物理", "化学", "生物"]
y_boy = np.array([85.5, 91, 72, 59, 66, 55])
y_girl = np.array([94, 82, 89.5, 62, 49, 53])
x_value = np.arange(len(x_title))
plt.bar(x_value, y_boy, tick_label=x_title, width=bar_wight, label="男生")
plt.bar(x_value + bar_wight, y_girl, width=bar_wight, label="女生")
# 向每个柱形的顶部添加注释文本,标注平均成绩
for a, b, c in zip(x_value, y_boy, y_girl):
# zip函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的列表。
plt.text(a - bar_wight / 2, b, y_boy[a], fontsize=9)
plt.text(a + bar_wight / 2, c, y_girl[a], fontsize=9)
# text(x, y, s, **kwargs)
# x,y定位
# s要显示的内容
plt.xticks(x_value + bar_wight / 2, x_title)
# 将x轴刻度标签放在两组柱形中间
plt.axhline(y=(85.5 + 91 + 72 + 59 + 66 + 55 + 94 + 82 + 89.5 + 62 + 49 + 53) / 12, linestyle='--', label='平均成绩')
# 在轴上添加一条直线
plt.ylabel("平均成绩(分)")
plt.title("高二男生、女生的平均成绩")
plt.legend()
plt.show()
实验结果:
堆积柱形图:
实验代码:
#堆积柱形图
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = [u'SimHei']
girl = plt.bar([1, 2, 3, 4, 5, 6], [94, 82, 89.5, 62, 49, 53], label="女生")
boy = plt.bar([1, 2, 3, 4, 5, 6], [85.5, 91, 72, 59, 66, 55], bottom=[94, 82, 89.5, 62, 49, 53], label="男生")
plt.xticks([1, 2, 3, 4, 5, 6], ["语文", "数学", "英语", "物理", "化学", "生物"])
plt.xlabel("学科")
plt.ylabel("平均成绩")
plt.legend()
plt.show()
实验结果:
题目五:物流公司物流费用统计
编写程序。根据2.4.2 实例4:物流公司物流费用统计。将月份列的数据作为x轴的刻度标签,将A公司、B公司、C公司这三列数据作为y轴的数据,使用stackplot()函数绘制堆积面积图。
实验代码:
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(1, 13)
y1 = np.array([198, 215, 245, 222, 200, 236, 201, 253, 236, 200, 266, 290])
y2 = np.array([203, 236, 200, 236, 269, 216, 298, 333, 301, 349, 360, 368])
y3 = np.array([185, 205, 226, 199, 238, 200, 250, 209, 246, 219, 253, 288])
plt.stackplot(x, y1, y2, y3)
plt.show()