0
点赞
收藏
分享

微信扫一扫

Matplotlib学习笔记

sin信仰 2022-03-17 阅读 70

Matplotlib.pyplot笔记

jupyter notebook 使用快捷键

  • Shift+Enter 执行本单元代码并跳转下一单元
  • Ctrl+Enter 执行本单元代码并留在本单元
  • cell前面的*表示正在运行

命令模式:按ESC进入

  • Y,cell模式
  • M,markdown 模式
  • A,在当前cell上面添加cell
  • B,在当前cell下面添加cell
  • 双击D,删除当前cell
  • Z,回退
  • L,在当前cell上面加上引号
  • Ctrl +shift+P,对话框输入命令直接运行
  • Ctrl+Home,快速跳转到首个cell
  • Ctrl+End,快速跳转到最后一个cell

编辑模式:按Enter进入

  • 多光标操作:Ctrl+鼠标点击
  • 回退:Ctrl+Z
  • 重做:Ctrl+Y
  • 补全代码:Tab
  • 添加/取消注释:Ctrl+/
  • 屏蔽自动输出:最后一行加分号

Matplotlib操作

  • pyplot API参考资料地址:https://www.osgeo.cn/matplotlib/api/pyplot_summary.html
# 简单Matplotlib、画图
import matplotlib.pyplot as plt
plt.figure()#添加画布
plt.plot([1,0,9],[4,5,6])# [x1,x2,x3],[y1,y2,y3]
plt.show()# 显示图像

在这里插入图片描述

Matplotlib三层结构

容器层

  • 画板层(Canvas) 可理解为现实生活中的油画画板支架
  • 画布层(Figure) 可理解为油画画布,plt.figure()
  • 绘图区/坐标系(Axes) 可理解为画布上可以绘画的区域plt.plots()

辅助显示层

  • 绘图区之上的辅助功能,如坐标、图例、标题

图像层

  • 指Axes内通过plot(折线图)、scatter(散点图)、bar(柱状图)、histogram(直方图)、pie(饼图)等函数根据数据绘制出的图像

折线图与基础绘图功能

折线图绘制过程

  • 1 导包 import matplotlib.pyplot as plt
  • 2 创建画布
  • 3 绘制折线图
  • 4 显示图像
  • 5 例如:一周图像显示
  • API:https://www.osgeo.cn/matplotlib/api/_as_gen/matplotlib.pyplot.plot.html#matplotlib.pyplot.plot
import matplotlib.pyplot as plt
plt.figure(figsize=(5,5),dpi=100)# figsize 图像大小 dpi 分辨率
plt.plot([5,6,7,8,9,10,11],[12,14,16,20,22,24,23])
#plt.savefig("天气折线图.png")
plt.show()

在这里插入图片描述

辅助显示层完善折线图

  • 例:画出某城市11点到12点这一个小时内每分钟温度变化折线图,温度范围在15-18度
import random
import matplotlib.pyplot as plt
x = range(60)
y = [random.uniform(16,18) for i in x]
plt.figure(figsize=(10,5),dpi=90)
plt.plot(x,y)
# 修改x,y的标注
# 修改x,y刻度值
x_label=[f"11:{i}" for i in x]# 推导式生成列表
plt.yticks(range(-10,40,5))# 起始值,终值,步长
plt.xticks(x[::5],x_label[::5])
# 增加网格
plt.grid(True,linestyle='--',alpha=0.5)# alpha  透明度
# 添加标题
plt.xlabel("Time")
plt.ylabel("Temp")
plt.title("Weather")
plt.show()

在这里插入图片描述

两个折线的折线图绘制

x = range(60)
y = [random.uniform(16,18) for i in x]
y_Beijing = [random.uniform(8,10) for i in x]

plt.figure(figsize=(10,5),dpi=90)
# 绘制图像
plt.plot(x,y,color="b",linestyle="--",label='Shanghai')
plt.plot(x,y_Beijing,color="r",linestyle=':',label='Beijing')
# 修改x,y的标注
# 修改x,y刻度值
x_label=[f"11:{i}" for i in x]# 推导式生成列表
plt.yticks(range(-10,40,5))# 起始值,终值,步长
plt.xticks(x[::5],x_label[::5])
# 增加网格
plt.grid(True,linestyle='--',alpha=0.5)# alpha  透明度
# 添加标题
plt.xlabel("Time")
plt.ylabel("Temp")
plt.title("Weather")
# 添加图例
plt.legend()
plt.show()

在这里插入图片描述

图形风格

  • r 红色 - 实线
  • g 绿色 - - 虚线
  • b 蓝色 -. 点划线
  • w 白色 : 点虚线
  • c 青色 ’ ’ 留空、空格
  • m 洋红
  • y 黄色
  • k 黑色

显示图例

面向对象的绘图方法 多个坐标系显示–plt.subplots()

  • 面向对象的绘图方法和面向过程的绘图方法有一点不同,在面向过程中xticks一次完成的任务在面向对象的方法中分两步进行即xticks和xticklabels
x = range(60)
y = [random.uniform(16, 18) for i in x]
y_Beijing = [random.uniform(8, 10) for j in x]
# 创建画布
figure, axes = plt.subplots(nrows=1, ncols=2, figsize=(20, 10), dpi=90)
# 绘制图像
axes[0].plot(x, y, color="b", linestyle="--", label='Shanghai')
axes[1].plot(x, y_Beijing, color="r", linestyle=':', label='Beijing')
# 修改x,y的标注
# 修改x,y刻度值
x_label = [f"11:{i}" for i in x]  # 推导式生成列表
axes[0].set_yticks(range(-10, 40, 5))  # 起始值,终值,步长
axes[0].set_xticks(x[::5])
axes[0].set_xticklabels(x_label[::5])
axes[1].set_yticks(range(-10, 40, 5))  # 起始值,终值,步长
axes[1].set_xticks(x[::5])
axes[1].set_xticklabels(x_label[::5])
# 增加网格
axes[0].grid(True, linestyle='--', alpha=0.5)  # alpha  透明度
axes[1].grid(True, linestyle='--', alpha=0.5)
# 添加标题
axes[0].set_xlabel("Time")
axes[0].set_ylabel("Temp")
axes[0].set_title("Weather")
axes[1].set_xlabel("Time")
axes[1].set_ylabel("Temp")
axes[1].set_title("Weather")
# 添加图例
axes[0].legend()
axes[1].legend()
plt.show()

在这里插入图片描述

折线图应用场景

  • 公司产品不同区域活跃用户数
  • APP下载量
  • 点击次数随时间变化
  • 某一个事物随时间变化指标

绘制数学函数图像

  • plt.plot()可以绘制各种数学函数图像
# 准备x,y数据
import numpy as np
x = np.linspace(-10,10,2000)# 在区间产生2000数(区间左闭右闭)
y = np.sin(x)
plt.figure(figsize=(20,10),dpi=100)
plt.plot(x,y)
plt.grid(linestyle="--",alpha=0.2)
plt.show()

在这里插入图片描述

散点图及其他常见图表

  • API:https://www.osgeo.cn/matplotlib/api/_as_gen/matplotlib.pyplot.scatter.html#matplotlib.pyplot.scatter
  • 散点图特点:判断变量之间是否存在数量关联趋势,展示分布规律
  • 例如学习时间和成绩的关系
x = [0.50,0.75,1.00,1.25,1.50,1.75,1.75,2.00,2.25,2.50,2.75,3.00,3.25,3.50,4.00,4.25,4.50,4.75,5.00,5.50]
y = [10,  22,  13,  43,  20,  22,  33,  50,  62, 48,  55,  75,  62,  73,  81,  76,  64,  82,  90,  93]
plt.figure(figsize=(20,10),dpi=100)
plt.xlabel("Score")
plt.ylabel("Time")
plt.scatter(x,y)
plt.show()

在这里插入图片描述

柱状图

  • 例如电影票房的柱状图(2022/3/3-3/9)
  • API:https://www.osgeo.cn/matplotlib/api/_as_gen/matplotlib.pyplot.bar.html#matplotlib.pyplot.bar
date = ["3/3",'3/4','3/5','3/6','3/7','3/8','3/9']
summary = [2082.78,3926.75,7916.52,6230.19,2178.87,4488.13,1857.78]
plt.figure(figsize=(20,10),dpi=100)
plt.xlabel("date")
plt.ylabel("income")
plt.bar(date,summary,width=0.3)
plt.grid(linestyle="--",alpha=0.3)
plt.show() 

在这里插入图片描述

COVID-19中美单日对比

  • 2022年3月3日-3月9日单日新冠对比
date = ["3/3",'3/4','3/5','3/6','3/7','3/8','3/9']
china = [19857,31534,18220,19158,5939,15050,12258]
america = [72680,72966,52081,16968,4903,62455,32978]
plt.figure(figsize=(20,10),dpi=100)
plt.xlabel("Date")
plt.ylabel("Number")
x1=[i-0.1 for i in range(0,7)]
x2=[i+0.1 for i in range(0,7)]
plt.grid(linestyle="--",alpha=0.5)
plt.bar(x1,china,width=0.2,color='r',label='China') # 这里的label指的是图中柱子或折线的标签
plt.bar(x2,america,width=0.2,color='b',label='America')
plt.xticks([i for i in range(0,7)],date)
plt.legend()
plt.show()

在这里插入图片描述

直方图

  • API:https://www.osgeo.cn/matplotlib/api/_as_gen/matplotlib.pyplot.hist.html#matplotlib.pyplot.hist
  • 组数:在统计数据时,我们把数据按照不同的范围分成几个组,分成的组的个数称为组数
  • 组距:每一组两个端点的差
  • 已知 最高175.5 最矮150.5 组距5
  • 求 组数:(175.5 - 150.5) / 5 = 5
  • 直方图绘制例如:身高分布、成绩分布
  • 绘制过程:设置组距,设置组数
score = [60,63,66,70,78,73,74,79,81,83,83,85,86,87,88,88,92,95,95,92,97,90,100]
plt.figure(figsize=(10,5),dpi=100)
distance = 5
num=int((max(score)-min(score))/distance)
plt.grid(linestyle='--',alpha=0.5)
plt.xlabel("Score")
plt.ylabel("Number")
plt.hist(score,bins=num,color="b")# density = True 可以显示频率
plt.show()

在这里插入图片描述

饼图

  • API:https://www.osgeo.cn/matplotlib/api/_as_gen/matplotlib.pyplot.pie.html#matplotlib.pyplot.pie
  • 例如 2020年中国GDP不同产业构成
  • plt.pie(x,labels=,autopct=%1.2f%%,colors=)
  • labels每部分名称autopct占比显示指定%1.2f%%
gdp=[77754.1,384255.3,553976.8]
name=["Primary industry","Secondary industry","Tertiary industry"]
plt.figure(figsize=(20,100),dpi=100)
plt.pie(gdp,labels=name,autopct="%1.2f%%",colors=["c","m","y"])
plt.legend()
plt.show()

在这里插入图片描述

举报

相关推荐

0 条评论