三、matplotlin

1. 定义

  • 专门用于开发2D图表(包括3D图表)
  • 使用起来及其简单
  • 以渐进、交互式方式实现数据可视化

2. 学习目的

  • 能将数据可视化,更直观的呈现
  • 使数据更加客观、更具说服力

3. 运用

1. 绘制流程

  1. 创建画布
  2. 绘制图像
  3. 显示图像

2. 三层结构

  1. 容器层
    • canvas
    • figure
    • axes
  2. 辅助显示层
    • 添加x轴,y轴描述,标题……
  3. 图像层
    • 绘制什么图像的声明
import matplotlib.pyplot as plt

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

# 2. 绘制图像
x = [1, 2, 3]
y = [4, 5, 6]
plt.plot(x, y)

# 显示图像
plt.show()

在这里插入图片描述

4. 基础绘图功能

1. help介绍

import matplotlib.pyplot as plt

# 显示plt.figure的帮助文档
help(plt.figure)

2. 设置画布属性与图片保存

# 语法
plt.figure(figsize=(), dpi=)
    figsize:指定图的长宽
    dpi:图像的清晰度
    返回fig对象
plt.savefig(path)
import matplotlib.pyplot as plt

# 图像保存
# 创建画布
plt.figure(figsize=(20, 8), dpi=100)

# 绘制图像
x = [1, 2, 3]
y = [4, 5, 6]
plt.plot(x, y)

# 图像保存
plt.savefig("data/text1.png")

# 显示图像
plt.show()
  • 注意:plt.show()会释放figure资源,如果在显示图像之后保存图片将这只能保存空图片。

3. 添加x,y轴刻度

import matplotlib.pyplot as plt
import random

# 生成数据
x = range(60)
y = [random.uniform(10, 15) for i in x]

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

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

# 添加x,y轴刻度
y_ticks = range(40)
x_ticks_labels = ["11h{}m".format(i) for i in x]

plt.yticks(y_ticks[::5])
plt.xticks(x[::5], x_ticks_labels[::5]) # 参数1必须是数字

# 图像显示
plt.show()

在这里插入图片描述

4. 添加网格

import matplotlib.pyplot as plt
import random

# 生成数据
x = range(60)
y = [random.uniform(10, 15) for i in x]

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

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

# 添加x,y轴刻度
y_ticks = range(40)
x_ticks_labels = ["11h{}m".format(i) for i in x]

plt.yticks(y_ticks[::5])
plt.xticks(x[::5], x_ticks_labels[::5]) # 参数1必须是数字

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

# 图像显示
plt.show()

在这里插入图片描述

5. 添加描述信息

import matplotlib.pyplot as plt
import random

# 生成数据
x = range(60)
y = [random.uniform(10, 15) for i in x]

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

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

# 添加x,y轴刻度
y_ticks = range(40)
x_ticks_labels = ["11h{}m".format(i) for i in x]

plt.yticks(y_ticks[::5])
plt.xticks(x[::5], x_ticks_labels[::5]) # 参数1必须是数字

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

# 添加描述信息
plt.xlabel("时间", fontsize=20)
plt.ylabel("温度", fontsize=20)
plt.title("一小时温度变化图", fontsize=30)

# 图像显示
plt.show()

在这里插入图片描述

6. 多条数据线、显示图例

import matplotlib.pyplot as plt
import random

# 生成数据
x = range(60)
y = [random.uniform(10, 15) for i in x]
y_avg = [random.uniform(15, 25) for i in x]

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

# 图像绘制 - label:图例名
plt.plot(x, y, label="最低")
plt.plot(x, y_avg, label="平均")

# 添加x,y轴刻度
y_ticks = range(40)
x_ticks_labels = ["11h{}m".format(i) for i in x]

plt.yticks(y_ticks[::5])
plt.xticks(x[::5], x_ticks_labels[::5]) # 参数1必须是数字

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

# 添加描述信息
plt.xlabel("时间", fontsize=20)
plt.ylabel("温度", fontsize=20)
plt.title("一小时温度变化图", fontsize=30)

# 显示图例 - loc:显示位置
plt.legend(loc="best")

# 图像显示
plt.show()

在这里插入图片描述

7. 设置图形风格

颜色字符风格字符
r 红色- 实线
g 绿色– 虚线
b 蓝色-. 点划线
w 白色: 点虚线
c 青色‘’ 留空、空格
m 洋红
y 黄色
k 黑色
import matplotlib.pyplot as plt
import random

# 生成数据
x = range(60)
y = [random.uniform(10, 15) for i in x]
y_avg = [random.uniform(15, 25) for i in x]

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

# 图像绘制
plt.plot(x, y, label="最低", color="c", linestyle="-.")
plt.plot(x, y_avg, label="平均")

# 添加x,y轴刻度
y_ticks = range(40)
x_ticks_labels = ["11h{}m".format(i) for i in x]

plt.yticks(y_ticks[::5])
plt.xticks(x[::5], x_ticks_labels[::5]) # 参数1必须是数字

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

# 添加描述信息
plt.xlabel("时间", fontsize=20)
plt.ylabel("温度", fontsize=20)
plt.title("一小时温度变化图", fontsize=30)

# 显示图例
plt.legend(loc="best")

# 图像显示
plt.show()

在这里插入图片描述

8. 图例位置一览

位置字符串位置编号
“best”0
“upper right”1
“upper left”2
“lower left”3
“lower right”4
“right”5
“center left”6
“center right”7
“lower center”8
“upper center”9
“center”10

8. 多个坐标系显示图像

import matplotlib.pyplot as plt
import random

# 生成数据
x = range(60)
y = [random.uniform(10, 15) for i in x]
y_avg = [random.uniform(15, 25) for i in x]

# 创建画布
fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(20, 8), dpi=100)

# 图像绘制
axes[0].plot(x, y, label="最低", color="c", linestyle="-.")
axes[1].plot(x, y_avg, label="平均")

# 添加x,y轴刻度
y_ticks = range(40)
x_ticks_labels = ["11h{}m".format(i) for i in x]

axes[0].set_xticks(x[::5])
axes[0].set_yticks(y_ticks[::5])
axes[0].set_xticklabels(x_ticks_labels[::5])

axes[1].set_xticks(x[::5])
axes[1].set_yticks(y_ticks[::5])
axes[1].set_xticklabels(x_ticks_labels[::5])

# 添加网格
axes[0].grid(True, linestyle="--", alpha=1)
axes[1].grid(True, linestyle="--", alpha=1)

# 添加描述信息
axes[0].set_xlabel("时间", fontsize=10)
axes[0].set_ylabel("温度", fontsize=10)
axes[0].set_title("一小时温度变化图", fontsize=20)

axes[1].set_xlabel("时间", fontsize=10)
axes[1].set_ylabel("温度", fontsize=10)
axes[1].set_title("一小时温度变化图", fontsize=20)

# 显示图例
axes[0].legend(loc="best")
axes[1].legend(loc="best")

# 图像显示
plt.show()

在这里插入图片描述

5. 折线图的应用场景

  • 表述数据变化
  • 画各种数学函数图像
    • 注意:plt.plot()除了可以画折线图,也可以用于画各种数学函数图像
import matplotlib.pyplot as plt
import numpy as np

# 准备数据
x = np.linspace(-10, 10, 1000)
y = np.sin(x)

# 创建画布
plt.figure(figsize=(20, 10), dpi=100)

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

# 添加网格显示
plt.grid()

# 显示图像
plt.show()

在这里插入图片描述

6. 常见图形绘制

1. 常见图形种类及意义

  1. 折线图
    • 定义:以折线的上升或下降来表示统计数量的增减变化的统计图。
    • 特点:能够显示数据的变化趋势,反映事物的变化情况。(变化)
  2. 散点图
    • 定义:用两组数据构成多个坐标点,考察坐标点的分布,判断两变量之间是否存在某种关联或总结坐标点的分布模式。
    • 特点:判断变量之间是否存在数量关联趋势,展示离群点(分布规律)
    import matplotlib.pyplot as plt
    
    # 数据准备
    x = [225.98, 247.07, 253.14, 457.85, 241.58, 301.01, 20.67, 288.64, 163.56, 120.06, 207.83, 342.75, 147.9, 53.06, 224.72, 29.51, 21.61, 483.21, 245.25, 399.25, 343.35]
    y = [196.63, 203.88, 210.75, 372.74, 202.41, 247.63, 24.9, 239.34, 140.32, 104.15, 176.84, 288.23, 128.79, 49.64, 191.74, 33.1, 30.74, 400.02, 205.35, 330.64, 283.45]
    
    # 创建画布
    plt.figure(figsize=(20, 8), dpi=100)
    
    # 图像绘制
    plt.scatter(x, y)
    
    # 图像显示
    plt.show()
    
    在这里插入图片描述
  3. 柱状图
    • 定义:排列在工作表的列或行中的数据可以绘制到柱状图中。
    • 特点:绘制连离散的数据,能够一眼看出各个数据的大小,比较数据之间的差别。(统计/对比)
    import matplotlib.pyplot as plt
    
    # 准备数据
    # 电影名字
    movie_name = ['雷神3:诸神黄昏', '争议联盟', '东方快车谋杀案', '寻梦环游记', '全球风暴', '降魔传', '追捕', '七十七天', '密战', '狂兽', '其它']
    
    # 横坐标
    x = range(len(movie_name))
    
    # 票房数据
    y = [73853, 57767, 22354, 15969, 14839, 8725, 8716, 8318, 7916, 6764, 52222]
    
    # 创建画布
    plt.figure(figsize=(20, 8), dpi=100)
    
    # 绘制图像
    plt.bar(x, y, color=['b', 'r', 'g', 'y', 'c', 'm', 'y', 'k', 'c', 'g', 'b'])
    
    # x轴
    plt.xticks(x, movie_name, fontsize=15)
    
    # 网格
    plt.grid()
    
    # 标题
    plt.title("某月电影房票统计")
    
    # 显示图像
    plt.show()
    
    在这里插入图片描述
  4. 直方图
    • 定义:由一系列高度不等的纵向条纹或线段表示数据分布的情况。一般用横轴表示数据范围,纵轴表示分布情况。
    • 特点:绘制连续性的数据展示一组或者多组数据的分布情况(统计)
  5. 饼图
    • 定义:用于表示不同分类的占比情况,通过弧度大小来对比各种分类。
    • 特点:分类数据的占比情况(占比)
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值