练习一:
为了对某一产品进行合理定价,我们对此类商品进行了试销实验,价格与需求量数据如下。利用图表分析规律。
价格 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()