数据分析 第一周 折线图笔记

1.尝试用matplotlib模块生成图像

from matplotlib import pyplot as plt

#设定 x 轴 和 y 轴
x = range(2 , 20 , 2)
y = [13 , 24 , 132 ,123, 12, 11 , 1, 2, 5]


# figsize 设定整个矩形框的 长 和 宽    
# dpi     设定分辨率
plt.figure(figsize = (10 , 5) , dpi = 100 )

#生成图像
plt.plot(x,y)

#图像输出
plt.show()

在这里插入图片描述

2.用 range 精确 x , y 刻度

#在 matplotlib 库里导入 pyplot 模块
from matplotlib import pyplot as plt

#设定 x 轴 和 y 轴
x = range(2 , 20 , 2)
y = [13 , 24 , 132 ,123, 12, 11 , 1, 2, 5]


# figsize 设定整个矩形框的 长 和 宽    
# dpi     设定分辨率
plt.figure(figsize = (10 , 5) , dpi = 100 )

#生成图像
plt.plot(x,y)

#设置刻度
plt.xticks(range(2 , 19 , 1))
plt.yticks(range(min(y) , max(y) + 1 , 10))

#图像输出
plt.show()

在这里插入图片描述

3.用 列表 和 切片精确 x , y 的刻度

from matplotlib import pyplot as plt

#设定 x 轴 和 y 轴
x = range(2 , 20 , 2)
y = [13 , 24 , 132 ,123, 12, 11 , 1, 2, 5]


# figsize 设定整个矩形框的 长 和 宽    
# dpi     设定分辨率
plt.figure(figsize = (10 , 5) , dpi = 100 )

#生成图像
plt.plot(x,y)

#设置刻度
xticks_tables = [i/2 for i in range(4 , 37)]

plt.xticks(xticks_tables[::3])

#图像输出
plt.show()

在这里插入图片描述

4.绘制 10 - 12点的气温变化图

from matplotlib import pyplot as plt
import random


x = range(1 , 121)
y = [random.randint(20 , 35) for i in range(1 , 121)]

#设置真个图片大小 和 分辨率
plt.figure(figsize=(15 , 5) , dpi = 100) 

plt.plot(x,y)

#调整横纵坐标刻度
plt.xticks(range(1,122,5))
plt.yticks(y[::1])

plt.show()

在这里插入图片描述

5.改进图像 , 使横坐标变为中文坐标 并 添加描述信息

# ctal 1 全部注释 /  全部取消注释
# ctal B 查看函数源码   

from matplotlib import pyplot as plt
import random
import matplotlib as mtb

#实现图片下标中文
mtb.rcParams['font.sans-serif'] = ["SimHei"]
mtb.rcParams["axes.unicode_minus"] = False

x = range(1 , 121)
y = [random.randint(20 , 35) for i in range(1 , 121)]

plt.figure(figsize=(15 , 5) , dpi = 100)

plt.plot(x,y)


#设置刻度 间隔 和 文字角度
_xticks_lables = ["{}点{}分".format(10 + i // 60, i % 60)  for i in range(1 , 121)]
_x = list(x)
plt.xticks(_x[::3] , _xticks_lables[::3] , rotation = 45 ) 

#添加描述信息
plt.xlabel("时间")
plt.ylabel("温度 单位(℃)")
plt.title("十点到十二点每分钟的气温变化情况图")

plt.show()

在这里插入图片描述

6.动手作业 11 - 30 岁每年交的男(女)朋友数量走势

from matplotlib import pyplot as plt
import matplotlib as mtb

#实现图片下标中文
mtb.rcParams['font.sans-serif'] = ["SimHei"]
mtb.rcParams["axes.unicode_minus"] = False

#设置范围
x = [i for i in range(11 , 31)]
y = [1,0,1,1,2,4,3,2,3,4,4,5,6,5,4,3,3,1,1,1]

#设置大小 和 分辨率
plt.figure(figsize = (15,5) , dpi = 80)


#设置横坐标  
xticks_lables = ["{}岁".format(i) for i in x]
plt.xticks(x , xticks_lables)

#设置标签
plt.xlabel("年龄")
plt.ylabel("人数(单位:个)")
plt.title("11 - 30 岁每年交的男(女)朋友数量走势")

#设置网格
#设置透明度 
plt.grid(alpha = 0.5)

plt.plot(x , y)

plt.show()

在这里插入图片描述

7.动手作业 自己与同桌 11 - 30 岁每年交的男(女)朋友数量走势

#一定注意设置顺序 , 不要乱改顺序

from matplotlib import pyplot as plt
import matplotlib as mtb

#实现中文输出
mtb.rcParams['font.sans-serif'] = ["SimHei"]
mtb.rcParams["axes.unicode_minus"] = False

#设置大小 和 分辨率
plt.figure(figsize = (15,5) , dpi = 80)

#设置范围
x = [i for i in range(11 , 31)]
y1 = [1,0,1,1,2,4,3,2,3,4,4,5,6,5,4,3,3,1,1,1]
y2 = [1,0,3,1,2,2,3,3,2,2,1,2,1,1,1,1,1,1,1,1]

#设置折线
plt.plot(x , y1 , label = "自己" , color = "#4B0082"    , linestyle = ':')
plt.plot(x , y2 , label = "同桌" , color = "#006400"    , linestyle = '-')

#设置横纵坐标刻度  
#两种用法 直接放范围 或者 把范围对应到字符串
xticks_lables = ["{}岁".format(i) for i in x]
plt.xticks(x , xticks_lables)
plt.yticks(range(0,10))

#设置标签
plt.xlabel("年龄")
plt.ylabel("人数(单位:个)")
plt.title("自己与同桌 11 - 30 岁每年交的男(女)朋友数量走势")


#设置网格 alpha 是清晰度
plt.grid(alpha = 0.3 , color = "#006400")

#设置图例
#两步 , 先在折线中设置标签 , 再用legend函数显示
plt.legend()

#显示图像
plt.show()

在这里插入图片描述

8.对比常用统计图区别

1.折线图:反应事物的变化情况
2.直方图:反应一段连续性的数据分布情况
3.条形图:反应一组离散数(没关系)数据的分布情况
4.散点图:反应一组数据的变化趋势,展示数据的分布规律

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

每天都想发疯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值