练习1 为了对某一产品进行合理定价,我们对此类商品进行了试销实验,价格与需求量数据如下。利用图表分析规律。
- price = [60,80,40,30,70,90,95]
- sales = [100,50,120,135,65,45,40]
import matplotlib.pyplot as plt
# 解决中文乱码问题
import matplotlib
font = {
'family':'SimHei',
'weight':'bold',
'size':12
}
matplotlib.rc("font", **font)
# 使用散点图分析
price = [60,80,40,30,70,90,95]
sales = [100,50,120,135,65,45,40]
plt.scatter(price,sales)
# 设置横纵坐标标签
plt.ylabel("销量")
plt.xlabel("价格")
plt.show()
结果如下:
则该产品的销量随着价格的升高而降低
练习2 直观体现出不同电影近三天的票房的对比情况¶
- 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]
# 设置画布大小
plt.figure(figsize=(10,10))
# 设置柱形图宽度为0.25
width = 0.25
# 获取day_12\13\14横坐标位置
po_day_12 = [i-width for i in list(range(len(movies_name))) ]
po_day_13 = [i for i in list(range(len(movies_name))) ]
po_day_14 = [i+width for i in list(range(len(movies_name)))]
# 绘图
plt.bar(po_day_12,day_12,width,label = "12日")
plt.bar(po_day_13,day_13,width,label = "13日")
plt.bar(po_day_14,day_14,width,label = "14日")
# 设置x轴刻度标签
plt.xticks(list(range(len(movies_name))),movies_name)
# 标注票房情况
def auto_lable(x, y):
for x_i,y_i in zip(x,y):
plt.annotate(y_i, (x_i, y_i), (x_i-0.1, y_i+100))
auto_lable(po_day_12,day_12)
auto_lable(po_day_13,day_13)
auto_lable(po_day_14,day_14)
# 设置x轴标签
plt.xlabel("电影")
# 设置u轴标签
plt.ylabel("票房")
# 设置标题
plt.title("12-14日电影票房")
# 设置图例
plt.legend()
# 展示图片
plt.show()
结果如下: