练习一:
电影数据如下:
movies_name = [“变身特工”,“美丽人生”,“鲨海逃生”,“熊出没·狂野大陆”]
day_12 = [2358,399,2358,362]
day_13 = [12357,156,2045,168]
day_14 = [15746,312,4497,319]
需求:
• 直观体现出不同电影近三天的票房的对比情况
"""
电影数据如下:
movies_name = ["变身特工","美丽人生","鲨海逃生","熊出没·狂野大陆"]
day_12 = [2358,399,2358,362]
day_13 = [12357,156,2045,168]
day_14 = [15746,312,4497,319]
需求:
• 直观体现出不同电影近三天的票房的对比情况
"""
import matplotlib.pyplot as plt
# 字体
plt.rcParams['font.sans-serif'] = ['Heiti TC']
plt.figure(figsize=(20, 8), dpi=100)
movies_name = ["变身特工","美丽人生","鲨海逃生","熊出没·狂野大陆"]
day_12 = [2358,399,2358,362]
day_13 = [12357,156,2045,168]
day_14 = [15746,312,4497,319]
x = range(len(movies_name))
plt.bar([i-0.2 for i in x], day_12, width=0.2, color='r', label = "day_12")
plt.bar(x, day_13, width=0.2, color='g', label = "day_13")
plt.bar([i+0.2 for i in x], day_14, width=0.2, color='b', label = "day_14")
plt.xlabel("电影名称")
plt.ylabel("各日电影数据")
# 显示x轴中文
plt.xticks(x, movies_name)
# 注释函数
def auto_label(x_po, y_po):
for x_i, y_i in list(zip(x_po, y_po)):
plt.annotate(f"{y_i}", xy=(x_i, y_i))
auto_label([i-0.2 for i in x], day_12)
auto_label(x, day_13)
auto_label([i+0.2 for i in x], day_14)
# 添加图例
plt.legend(loc = "best")
plt.savefig("电影票房对比.jpg")
plt.show()
练习二:
绘制班级的身高分布图形
height = [160,163,175,180,176,177,168,189,188,177,174,170,173,181]
"""
绘制班级的身高分布图形
height = [160,163,175,180,176,177,168,189,188,177,174,170,173,181]
"""
import matplotlib.pyplot as plt
# 字体
plt.rcParams['font.sans-serif'] = ['Heiti TC']
plt.figure(figsize=(13, 8), dpi=100)
height = [160,163,175,180,176,177,168,189,188,177,174,170,173,181]
# 组距
bin = 5
# 组数
groups = int(max(height) - min(height) / bin)
plt.hist(height, bin, rwidth=0.5)
# 指定刻度范围
plt.xticks(list(range(min(height), max(height)))[::5])
plt.xlabel("身高分布")
plt.ylabel("人员个数")
plt.savefig("身高分布图.jpg")
plt.show()