20210523 第2节课 Matplotlib图形作业

练习一:
为了对某一产品进行合理定价,我们对此类商品进行了试销实验,价格与需求量数据如下。利用图表分析规律。
价格 60 80 40 30 70 90 95
需求量 100 50 120 135 65 45 40
price = [60,80,40,30,70,90,95]
sales = [100,50,120,135,65,45,40]

"""
为了对某一产品进行合理定价,我们对此类商品进行了试销实验,价格与需求量数据如下。利用图表分析规律。
价格	60	80	40	30	70	90	95
需求量	100	50	120	135	65	45	40
price = [60,80,40,30,70,90,95]
sales = [100,50,120,135,65,45,40]
"""

import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['Heiti TC']      # 字体设置为黑体
plt.figure(figsize=(12, 5), dpi=100)

price = [60,80,40,30,70,90,95]
sales = [100,50,120,135,65,45,40]

plt.scatter(price, sales, marker='o', color='r')        # 图片显示参数信息设定

# 刻度显示
x = list(range(20, 100))
y = list(range(30, 150))
x_t = x
x_label = ["售价{}元".format(i) for i in x]
y_t = y
y_label = ["销售{}个".format(i) for i in y]
plt.xticks(x_t[::5], x_label[::5], rotation=45)       # rotation字体旋转度数
plt.yticks(y_t[::10], y_label[::10], rotation=0)

# 坐标轴标示
plt.xlabel("价格变化")
plt.ylabel("销售数量", rotation=45)

# 注释函数
def auto_label(x_po, y_po):
    for x_i, y_i in list(zip(x_po, y_po)):
        plt.annotate(f"{x_i, y_i}", xy=(x_i, y_i))

auto_label(price, sales)

# 图形注释
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]
# 组距
b = 1

# 组数
groups = int(max(height) - min(height) / b)
plt.hist(height, groups)

# 指定x轴刻度范围
plt.xticks(list(range(min(height), max(height)))[::2])
# 增加网格显示
plt.grid(True, linestyle='--', alpha=1)       # alpha透明度0-1
plt.title("班级的身高分布")
plt.savefig("直方图3.jpg")
plt.show()

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值