简介
数据分析:是把隐藏在一些看似杂乱五章的数据背后的信息提炼出来,总结出研究对象的内在规律
数据分析三大工具:Numpy,Pandas,Matplotlib
数据分析基本流程:
一、matplotlib
matplotlib最流星的Python的底层绘图库, 主要做数据可视化图表,名字取材去MATLAB,是模仿MATLAB构建的。
示例
eg1.
假设 一天中每个两个小时(range(2,26,2))的气温分别是
[15,13,14,15,17,20,25,26,27,22,18,15]
from matplotlib import pyplot as plt # 导入pyplot
fit = plt.figure(figsize=(20, 8), dpi = 80) # dpi是在图像模糊时,传入的参数,让图像更加清晰
x = range(2, 26, 2) #数据在x轴的位置,是一个可迭代对象
y = [15,13,14,15,17,20,25,26,27,22,18,15] # 数据在y轴的位置,是一个可迭代对象
plt.plot(x, y) # 传入x, y 通过plot 绘制折线图
# 设置x轴的刻度
xtick_lables = [i/2 for i in range(4, 48)]
plt.xticks(xtick_lables[::3]) # 太密集或者稀疏可以调整步长。
plt.yticks(range(min(y), max(y)+1))
plt.savefig('./sig_size.png') # 保存图片,并且放大时,不会失真。
plt.show() # 展示图形
eg2.
10点到12点每分钟气温变化情况
from matplotlib import pyplot as plt
import random
x = range(0, 120)
y = [random.randint(20, 35) for i in range(120)]
plt.figure(figsize=(20, 8), dpi=80)
plt.plot(x, y)
_x = list(x)
# 调整x轴的刻度
_xticks_labels = ["10点{}分".format(i) for i in range(60)]
_xticks_labels += ["11点{}分".format(i) for i in range(60)]
# 取步长,数字和字符串一一对应,数据的长度一样
plt.xticks(x[::3], _xticks_labels[::3], rotation=45) # 旋转45度。逆时针
# 添加描述信息
plt.xlabel('时间')
plt.ylabel('温度')
plt.title('10点到12点每分钟气温变化情况')
plt.show()
eg3.
假设,在30岁的时候,根据自己实际情况,统计出来从11岁到30岁每年交的男女朋友数据如列表a ,请绘制出,该数据的折线图,以便分析走势。
a = [1, 0, 1, 1,2 ,4, 3, 2, 3, 4, 4, 5 ,6 ,5 ,4 ,3 ,3 ,1 ,1]
y轴表示个数
x轴表示岁数
from matplotlib import pyplot as plt
x = range(11, 31)
y = [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, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1]
plt.figure(figsize=(20, 8), dpi=80)
plt.plot(x, y, label='自己', color='orange', linestyle=':', linewidth=5) # 颜色 形状 粗细
plt.plot(x, y2, label='同桌', color='cyan', linestyle='-.')
plt.xlabel('年龄')
plt.ylabel('数量')
xticks_labels = ["{}岁".format(i) for i in range(11, 31)]
plt.xticks(x, xticks_labels, rotation=45)
plt.yticks(range(9))
# 绘制网格
plt.grid(alpha=0.4) # 透明度
# 添加图例, 调整位置
plt.legend(loc='upper left')
plt.show()
2.总结
绘制折线图(plt.plot)
设置图片大小(plt.figure)
实现图片保存(plt.savefig)
设置xy轴上的刻度和字符串(xticks/yticks)
设置标题,xy轴label(title,xlabel,ylabel)
解决字体
绘制多个图形(plt多次plot即可)
添加图例(plt.legend)
3.对比常用统计图
折线图:以折现的上升或者下降表示统计数量的增减变化。
直方图:由一系列高度不同的纵向条纹或者线段表示数据分布情况。
一般横轴表示数据范围,纵轴表示分布情况。
条形图:排列在工作表的列或者行中的数据。
散点图:用两组数据构成多个坐标点,考察坐标点的分布,判断两变量之间是否存在某种关联或者总结坐标点的分布模式。
3.1绘制散点图
from matplotlib import pyplot as plt
import random
y_3 = [random.randint(0, 30) for i in range(0, 31)]
y_10 = [random.randint(0, 20) for i in range(0, 31)]
x_3 = range(1, 32)
x_10 = range(51, 82)
# 设置图形大小
plt.figure(figsize=(20, 8), dpi=80)
# 绘制散点图
plt.scatter(x_3, y_3, label='3月份')
plt.scatter(x_10, y_10, label='10月份')
# 调整x轴
_x = list(x_3) + list(x_10)
_xticks_label = ["3月{}日".format(i) for i in x_3]
_xticks_label += ['10月{}日'.format(i-50) for i in x_10]
plt.xticks(_x[::3], _xticks_label, rotation=45)
# 添加图例
plt.legend(loc='upper left')
plt.xlabel('时间')
plt.ylabel('温度')
plt.title('标题')
plt.show()
3.2绘制条形图
获取2017年内地电影票房前20 的电影和电影票房数据,直观的展示数据。
from matplotlib import pyplot as plt
import random
a = ['猩球崛起:终极之战', '敦克尔克', '蜘蛛侠:英雄归来', '战狼2']
b_16 = [random.randint(100, 1000) for i in range(len(a))]
b_15 = [random.randint(200, 1000) for j in range(len(a))]
b_14 = [random.randint(300, 1000) for k in range(len(a))]
bar_width = 0.2
x_14 = list(range(len(a)))
x_15 = [i+bar_width for i in x_14] # # 右移为了不让条子图形重叠
x_16 = [i+bar_width*2 for i in x_14]
# 设置图形大小
plt.figure(figsize=(20, 8), dpi=80)
plt.bar(x_14, b_14, width=bar_width, label='9月14日')
plt.bar(x_15, b_15, width=bar_width, label='9月15日')
plt.bar(x_16, b_16, width=bar_width, label='9月16日')
# 设置x轴刻度
plt.xticks(x_15, a) # 字符串显示在中间
# 添加图例
plt.legend(loc='upper left')
plt.show()
3.3绘制直方图
直方图一般是处理没有经过统计的数据。
数据分为多少组比较合理,一般数据在100以内时,大多分为5-12组
组距:两个端点距离
组数 =
级
差
组
距
\frac{级差}{组距}
组距级差 =
m
a
x
(
a
)
−
m
i
n
(
a
)
b
i
n
w
i
d
t
h
\frac{max(a)-min(a)}{bin_width}
binwidthmax(a)−min(a)
示例
eg1.
假设获取了250部电影的时长,在列表a中,统计出这些电影时长分布,比如(100到120分钟的电影数量)
import random
from matplotlib import pyplot as plt
a = [random.randint(80,150) for i in range(0,250)]
# 计算组数
d = 5 # 组距
num_bins = int(max(a)-min(a))//d
plt.figure(figsize=(20, 8), dpi=80)
plt.hist(a, num_bins)
# 设置x轴刻度
plt.xticks(range(min(a), max(a)+d, d))
plt.grid()
plt.show()
eg2.
统计从家到上班地点所需要时间。
import random
from matplotlib import pyplot as plt
a = [random.randint(80,150) for i in range(0,250)]
# 计算组数
d = 5 # 组距
num_bins = int(max(a)-min(a))//d
plt.figure(figsize=(20, 8), dpi=80)
plt.hist(a, num_bins)
# 设置x轴刻度
plt.xticks(range(min(a), max(a)+d, d))
plt.grid()
plt.show()
直方图应用场景
用户年龄分布
一段时间用户点击次数分布
用户活跃时间分布状态
总结:
plt.plot() 折线图
plt.bar() 柱状图
plt.scatter() 散点图
plt.hist() 直方图
xticks /yticks x/y轴刻度
label 和title,grid 网格
legend 图例
savfig 保存
figure 设置大小