数据分析——第15节课matplotlib作业

练习1

为了对某一产品进行合理定价,我们对此类商品进行了试销实验,价格与需求量数据如下。利用图表分析规律。
价格 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 numpy as np
import pandas as pd
import matplotlib.pyplot as plt

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

plt.scatter(price,sales)
plt.xlabel('price')
plt.ylabel('sales')
plt.title('sales vs price')
plt.show()
  • 运行结果
    可见需求量随着价格的增加而呈现下降趋势
    在这里插入图片描述

作业2

电影数据如下:
movies_name = [“变身特工”,“美丽人生”,“鲨海逃生”,“熊出没·狂野大陆”]
day_12 = [2358,399,2358,362]
day_13 = [12357,156,2045,168]
day_14 = [15746,312,4497,319]
需求:
直观体现出不同电影近三天的票房的对比情况
示例图如下(基本需求都需实现):
在这里插入图片描述

  • 代码
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei'] # 步骤一(替换sans-serif字体)
plt.rcParams['axes.unicode_minus'] = False   # 步骤二(解决坐标轴负数的负号显示问题)

movies_name = ["变身特工","美丽人生","鲨海逃生","熊出没·狂野大陆"] 
day_12 = [2358,399,2358,362] 
day_13 = [12357,156,2045,168] 
day_14 = [15746,312,4497,319]

def auto_label(x_po,y_po):
    for x_i,y_i in zip(x_po,y_po):
        plt.text(x_i,y_i+100,f"{y_i}",ha='center',fontsize=10)

movies_name = ["变身特工","美丽人生","鲨海逃生","熊出没·狂野大陆"] 
width = 0.2

plt.figure(figsize=(10,6))
po_l = [i-width for i in list(range(len(movies_name)))]
plt.bar(po_l,day_12,width,label="12号")

po_m = [i for i in list(range(len(movies_name)))]
plt.bar(po_m,day_13,width,label="13号")

po_r = [i+width for i in list(range(len(movies_name)))]
plt.bar(po_r,day_14,width,label="14号")

auto_label(po_l,day_12)
auto_label(po_m,day_13)
auto_label(po_r,day_14)
plt.xlabel('电影')
plt.ylabel('票房')
plt.title('2020年12日至14日电影票房')
plt.legend()
plt.xticks(list(range(len(movies_name))),movies_name)
plt.show()
  • 运行结果
    在这里插入图片描述

作业3

绘制班级的身高分布图形
height = [160,163,175,180,176,177,168,189,188,177,174,170,173,181]

  • 代码
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei'] # 步骤一(替换sans-serif字体)
plt.rcParams['axes.unicode_minus'] = False   # 步骤二(解决坐标轴负数的负号显示问题)

nums,bins,patches = plt.hist(height,bins=4)
plt.xticks(bins,bins)
for num,bin in zip(nums,bins):
    plt.annotate(num,xy=(bin,num),xytext=(bin+2.5,num+0.1))

plt.xlabel("身高区间")
plt.ylabel("频数")
plt.title("身高分布直方图")
plt.ylim((0,7))
plt.show()
  • 运行结果
    在这里插入图片描述

作业4

实现以下子图布局:
在这里插入图片描述

  • 代码
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei'] # 步骤一(替换sans-serif字体)
plt.rcParams['axes.unicode_minus'] = False   # 步骤二(解决坐标轴负数的负号显示问题)

fig = plt.figure()
width = (8,2)     # 宽为2:1 0列:1列 的宽度 2:1
height = (2,8)    # 高为2:1 0行:1行 的宽度 2:1
gs = fig.add_gridspec(2,2,width_ratios=width,height_ratios=height)

fig.add_subplot(gs[0,0])
plt.plot(range(5),range(5))
fig.add_subplot(gs[1,0])
plt.scatter(range(10,30),np.random.randint(10,30,size=20))
plt.xlim((10,30))
fig.add_subplot(gs[1,1])
plt.plot([i*0.6 for i in range(5)],range(5))
plt.show()
  • 运行结果
    在这里插入图片描述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值