Python数据分析 #001 Matplotlib

数据分析的前提就是数据可视化,数据可视化旨在直观展示信息的分析结果和构思,令某些抽象数据具象化

1. Matplotlib 介绍

  • Matplotlib 官网:https://matplotlib.org/

  • Matplotlib 是 Python 2D 绘图领域使用最广泛的库。它能让使用者很轻松地将数据图形化,并且提供多样化的输出格式。

  • 下载:pip install matplotlib



2. Matplotlib 三层结构

  • 容器层

    • Canvas:是位于最底层的系统层,在绘图的过程中充当画板的角色,即放置画布的工具

    • Figure: 是Canvas上方的第一层,在绘图的过程中充当画布的角色。

    • Axes:在绘图的过程中相当于画布上的绘图区的角色。

      一个figure(画布)可以包含多个axes(坐标系/绘图区),但是一个axes只能属于一个figure。

      一个axes(坐标系/绘图区)可以包含多个axis(坐标轴),包含两个即为2d坐标系,3个即为3d坐标系。

  • 辅助显示层

    • 辅助显示层为Axes(绘图区)内的除了根据数据绘制出的图像以外的内容,主要包括Axes外观(facecolor)、边框线(spines)、坐标轴(axis)、坐标轴名称(axis label)、坐标轴刻度(tick)、坐标轴刻度标签(tick label)、网格线(grid)、图例(legend)、标题(title)等内容。

    • 该层的设置可使图像显示更加直观更加容易被用户理解,但又不会对图像产生实质的影响。

  • 图像层

    • 图像层指Axes内通过plot、scatter、bar、histogram、pie等函数根据数据绘制出的图像。


3. 基本参数

  • 绘图流程:创建画布——创建子图——绘制图形——美化图形——保存图形——显示图形

  • 创建画布

    plt.figure(num=None,figsize=None,dpi=None,facecolor=None,edgecolor=None,frameon=True)
    
    num:图像编号或名称,数字为编号,字符串为名称
    figsize:指定 figure 的宽和高,单位为英寸
    dpi:参数指定绘图对象的分辨率,即每英寸多少个像素,缺省值为80,1英寸等于2.5cm
    facecolor:背景颜色
    edgecolor:边框颜色
    frameon:是否显示边框
    
  • 创建子图

    plt.subplot(nrows,ncols,plot_number)
    
    nrows:行
    ncols:列
    plot_number:当前子图区
    



4. 常见图形应用场景

  • 折线图:能够显示数据的变化趋势,反映事物的变化情况【变化】

  • 散点图:判断变量之间是否存在数量关联趋势,展示离群点 【分布规律】

  • 柱形图:主要用于离散数据展示的图形,直观的显示数据之间的差别 【统计/对比】

  • 直方图:体现数据频数分布的一种图形,适用于原始数据。【统计】

    • 组数:在统计数据时,把数据按照不同的范围分成几个组,分成的组的个数称为组数
    • 组距:每一组两个端点的差,组距 = 极差/组数
  • 饼图:分类数据的占比情况 【占比】



5. Matplotlib 基本使用

5.1 折线图 (plot)

from matplotlib import pyplot as plt
# 有的电脑需要写,不然图像不显示
%matplotlib inline

# 1.创建画布
plt.figure()
# 2.绘图,第一个列表为x轴的值,第二个为y的值,且一一对应
plt.plot([1, 3, 5], [2, 4, 6])
# 3.显示图像
plt.show()

添加属性

from matplotlib import pyplot as plt

# 设置尺寸,清晰度
plt.figure(figsize=(20, 6), dpi=80)
plt.plot([1, 3, 5], [2, 4, 6])

# 设置保存路径
# 注意:plt.show()会释放figure资源,如果在显示图像后再保存图片,则保存为空内容
plt.savefig(r"C:\Users\ASUS PC\Desktop\test.png")
plt.show()


示例一:

画出某城市11:00-12:00每分钟气温变化,温度范围15~18

from matplotlib import pyplot as plt
import random

# 1.准备数据 x ,y
x = range(60)
# uniform随机均匀生成两个数之间的随机数
y = [random.uniform(15,18) for i in  x] # 列表生成式

# 2.创建画布
plt.figure(figsize=(20, 8), dpi=80)
# 3.绘制图像
plt.plot(x, y)
# 4.显示图像
plt.show()


示例一:优化

添加配置:调整x,y的刻度,添加网格,添加标题及x,y描述, 添加图例

from matplotlib import pyplot as plt
import random

# 1.准备数据 x ,y
x = range(60)
# uniform随机均匀生成两个数之间的随机数
y_shanghai = [random.uniform(15,18) for i in  x]
y_beijing = [random.uniform(1, 3) for i in x]

# 2.创建画布
plt.figure(figsize=(20, 8), dpi=80)

# 3.绘制图像,指定线条的颜色(颜色首字母),样式,添加图例
plt.plot(x, y_shanghai, color="r", linestyle="--", label="上海")
plt.plot(x, y_beijing, color="b",label="北京")
# 显示图例
plt.legend()

# 修改坐标轴刻度,range(0,40,5) 可写成range(40)[::5] 
# 准备描述信息
x_lamba = ["11点{}分".format(i) for i in x] 
plt.xticks(x[::5], x_lamba[::5])  # 数据长度要对应 
plt.yticks(range(0,40,5))

# 添加网格,设置线的风格,透明度
plt.grid(True, linestyle="--", alpha=0.5)

# 添加标题,x,y描述
plt.title("上海,北京中午11.00-12.00气温变化")
plt.xlabel("时间")
plt.ylabel("温度")
# 4.显示图像
plt.show()


多个坐标系

plt.subplots()面向对象的画图

from matplotlib import pyplot as plt
import random

# 1.准备数据 x ,y
x = range(60)
y_shanghai = [random.uniform(15,18) for i in  x]
y_beijing = [random.uniform(1, 3) for i in x]

# 2.创建画布,subplots方法返回两个对象,画布和绘图区,画布可有多个绘图区
# nrows为行, ncols为列,一行两列即一行两个图
figure,axes = plt.subplots(nrows=1, ncols=2, figsize=(20, 8), dpi=80)

# 3.绘制图像,axes[0]为第一个要绘制的图,axes[1]为第二个
axes[0].plot(x, y_shanghai, color="r", linestyle="--", label="上海")
axes[1].plot(x, y_beijing, color="b",label="北京")

# 显示图例
axes[0].legend()
axes[1].legend()

# 修改坐标轴刻度,range(0,40,5) 可写成range(40)[::5] 
x_lamba = ["11点{}分".format(i) for i in x] 
axes[0].set_xticks(x[::5])
axes[0].set_xticklabels(x_lamba[::5])
axes[0].set_yticks(range(0,40,5))
axes[1].set_xticks(x[::5])
axes[1].set_xticklabels(x_lamba[::5])
axes[1].set_yticks(range(0,40,5))

# 添加网格,设置线的风格,透明度
axes[0].grid(True, linestyle="--", alpha=0.5)
axes[1].grid(True, linestyle="--", alpha=0.5)

# 添加标题,x,y描述
axes[0].set_title("上海中午11.00-12.00气温变化")
axes[0].set_xlabel("时间")
axes[0].set_ylabel("温度")
axes[1].set_title("北京中午11.00-12.00气温变化")
axes[1].set_xlabel("时间")
axes[1].set_ylabel("温度")
# 4.显示图像
plt.show()


数学图像

from matplotlib import pyplot as plt
import numpy as np

# 1. 准备数据
# [-1,1]生成1000个数
x = np.linspace(-1, 1, 1000)
y = 2 * x * x

# 2.创建画布
plt.figure(figsize=(20, 8), dpi=80)

# 3.绘制图像
plt.plot(x, y)

# # 添加网格
plt.grid(linestyle="--", alpha=0.5)

# # 4.显示图像
plt.show()



5.2 散点图 (scatter)

from matplotlib import pyplot as plt

# 1.准备数据
x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
y = [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

# 2.创建画布
plt.figure(figsize=(20,8), dpi=80)

# 3.绘制图像
plt.scatter(x, y)

# 4.显示图像
plt.show()



5.3 柱形图 (bar)

案例一

from matplotlib import pyplot as plt 

# 1.准备数据
movie_names = ["雷神3", "诸神黄昏", "正义联盟", "寻梦环游记", "降魔传"]
tickets = [73853, 57767, 22354, 14839, 8725]

# 2.创建画布
plt.figure(figsize=(20, 8), dpi=80)

# 3.绘图
# x一般不把数据直接传入,而是在修改刻度的时候添加描述
# 比如可写成:plt.bar(, tickets, color=["r", "b", "y", "m", "k"], width=0.3)
x = range(len(movie_names))
plt.bar(x, tickets, color=["r", "b", "y", "m", "k"], width=0.3)

# 修改刻度
plt.xticks(x, movie_names)

# 添加标题
plt.title("票房统计")

# 4.显示图像
plt.show()


案例二

# 统计相同天数的票房
from matplotlib import pyplot as plt 

# 1.准备数据
movie_names = ["雷神3", "诸神黄昏", "正义联盟", "寻梦环游记", "降魔传"]
fist_day = [4556, 7785, 4456, 1598, 2365]
fist_week = [73853, 57767, 22354, 14839, 8725]

# 2.创建画布
plt.figure(figsize=(20, 8), dpi=80)

# 3.绘图
x = range(len(movie_names))
plt.bar(x, fist_day,  width=0.2, label="第一天")
# 另一个绘图区与原来的偏移一个宽度,不然会重叠,覆盖
plt.bar([0.2, 1.2, 2.2, 3.2, 4.2], fist_week, width=0.2, label="第一周")
# 或写成
# plt.bar([i+0.2 for i in x], fist_week, width=0.2, label="第一周")

# 显示图例
plt.legend()

# 修改刻度
# 让刻度在两个柱形的中间
plt.xticks([0.1, 1.1, 2.1, 3.1, 4.1], movie_names)

# 添加标题
plt.title("票房统计")

# 4.显示图像
plt.show()



5.4 直方图 (histogram)

# 产生一些随机数,用于分析
import random
lis =[]
for i in range(250):
    x = random.randint(100,150)
    lis.append(x)
print(lis)
[146, 126, 108, 145, 133, 132, 102, 137, 150, 111, 103, 101, 139, 105, 118, 139, 128, 138, 107, 109, 103, 116, 110, 103, 142, 115, 122, 150, 150, 130, 127, 111, 123, 120, 146, 121, 106, 105, 125, 150, 128, 116, 136, 122, 101, 149, 102, 143, 119, 100, 116, 133, 100, 137, 112, 146, 102, 125, 137, 149, 144, 148, 139, 126, 120, 125, 146, 107, 132, 142, 131, 132, 150, 123, 127, 125, 136, 121, 102, 106, 133, 129, 144, 118, 110, 108, 147, 109, 128, 100, 105, 132, 125, 141, 150, 137, 150, 134, 138, 144, 149, 149, 107, 117, 109, 148, 133, 100, 143, 102, 133, 109, 106, 105, 119, 144, 104, 126, 126, 134, 149, 121, 111, 118, 124, 148, 111, 104, 111, 107, 128, 135, 149, 136, 104, 147, 104, 115, 139, 127, 112, 147, 131, 143, 119, 134, 111, 104, 115, 108, 141, 135, 106, 120, 143, 105, 144, 101, 122, 110, 119, 148, 104, 101, 124, 137, 106, 146, 114, 129, 103, 100, 105, 132, 111, 137, 118, 115, 114, 104, 120, 100, 110, 101, 124, 145, 125, 118, 104, 106, 105, 141, 115, 105, 141, 145, 104, 117, 114, 108, 111, 143, 117, 144, 109, 106, 113, 102, 134, 104, 102, 121, 129, 127, 124, 130, 143, 123, 114, 148, 107, 100, 106, 100, 103, 124, 109, 130, 149, 118, 123, 122, 104, 109, 123, 129, 125, 130, 117, 125, 134, 107, 134, 103, 109, 136, 128, 119, 122, 105]
from matplotlib import pyplot as plt 

# 1.准备数据
x = [146, 126, 108, 145, 133, 132, 102, 137, 150, 111, 103, 101, 139, 105, 118, 139, 128, 138, 107, 109, 103, 116, 110, 103, 142, 115, 122, 150, 150, 130, 127, 111, 123, 120, 146, 121, 106, 105, 125, 150, 128, 116, 136, 122, 101, 149, 102, 143, 119, 100, 116, 133, 100, 137, 112, 146, 102, 125, 137, 149, 144, 148, 139, 126, 120, 125, 146, 107, 132, 142, 131, 132, 150, 123, 127, 125, 136, 121, 102, 106, 133, 129, 144, 118, 110, 108, 147, 109, 128, 100, 105, 132, 125, 141, 150, 137, 150, 134, 138, 144, 149, 149, 107, 117, 109, 148, 133, 100, 143, 102, 133, 109, 106, 105, 119, 144, 104, 126, 126, 134, 149, 121, 111, 118, 124, 148, 111, 104, 111, 107, 128, 135, 149, 136, 104, 147, 104, 115, 139, 127, 112, 147, 131, 143, 119, 134, 111, 104, 115, 108, 141, 135, 106, 120, 143, 105, 144, 101, 122, 110, 119, 148, 104, 101, 124, 137, 106, 146, 114, 129, 103, 100, 105, 132, 111, 137, 118, 115, 114, 104, 120, 100, 110, 101, 124, 145, 125, 118, 104, 106, 105, 141, 115, 105, 141, 145, 104, 117, 114, 108, 111, 143, 117, 144, 109, 106, 113, 102, 134, 104, 102, 121, 129, 127, 124, 130, 143, 123, 114, 148, 107, 100, 106, 100, 103, 124, 109, 130, 149, 118, 123, 122, 104, 109, 123, 129, 125, 130, 117, 125, 134, 107, 134, 103, 109, 136, 128, 119, 122, 105]
# 2.创建画布
plt.figure(figsize=(20, 8), dpi=80)

# 3.绘制图像
# 设置组距为2
distance = 2
# 分组:组数 =(最大值-最小值)/ 组距
group_num = int((max(x) - min(x)) / distance)
# density=True 把y轴设为频率(频率=频数/总数),否则显示频数
plt.hist(x, bins=group_num, density=True)

# 修改x刻度
# 前闭后开,150没显示,所以+2
plt.xticks(range(min(x), max(x)+2, distance))

# 添加网格
plt.grid(linestyle="--", alpha=0.5)

# 4.显示图像
plt.show()



5.5 饼图 (pie)

from matplotlib import pyplot as plt

# 1.准备数据
movie_names = ["雷神3", "诸神黄昏", "正义联盟", "寻梦环游记", "降魔传"]
tickets = [4556, 7785, 4456, 1598, 2365]

# 2.创建画布
plt.figure(figsize=(20, 8), dpi=80)

# 3.绘制图像
plt.pie(tickets, labels=movie_names, colors=["r", "b", "m", "k", "c"], autopct="%1.2f%%")

# 显示图例
plt.legend()

# 保持横纵坐标比例相同
plt.axis("equal")

# 4.显示图像
plt.show()

posted @ 2023-06-28 22:52  枫_Null  阅读(26)  评论(0)    收藏  举报