matplotlib 教程简析

介绍

官方教程
中文教程

功能简介

基础功能

使用简介

  • 基础概念
    legend,marker; figure,axes,axis ; spines
    Artist( text, line2D , collection , patch ... )
  • 数据类型
    array
  • 命名空间
    matplotlib.pyplot 可与 numpy 使用 (pylab已经弃用)
  • 生成多个图
    plt.subplots(row,col) 可生成多个axes
  • 交互模式
    plt.draw() 如果不需要在屏幕显示,调用该函数
  • 便捷功能
    线段的简化:
    通过 path.simplify and path.simplify_threshold
    Marker的简化:
    通过markevery plt.plot(x, y, markevery=10)详情参看Markevery Demo
    线分块:
    mpl.rcParams['agg.path.chunksize'] = 0
    使用快速格式:
    为了避免其他格式被改变,可以在最后部分调用,不会改变其他设置
    import matplotlib.style as mplstyle
    mplstyle.use('fast')

    Pyplot示例

  • 折线图、散点图、柱状图
  • 散点图的图示、图的颜色
  • 绘制多个图
  • 插入文本
  • 插入数学公式
  • 插入注释文本
  • 坐标轴会自动适应不同数据:线性、指数等

图的种类

  • 线 plot()
  • 多个子图 subplot()
  • 显示图片 imshow()
  • 轮廓线与伪彩色 pcolormesh() , contour()
  • 直方图 hist()
  • 路径 matplotlib.path
  • 3D 3D plotting
  • 流量图 streamplot()
  • 椭圆 为了某些具体任务..
  • 条形图 bar()
  • 饼图 pie()
  • 表格 table()
  • 散点图 scatter()
  • 填充曲线 fill()
  • 时序数据 matplotlib.tickermatplotlib.dates
  • 指数 semilogx(), semilogy()loglog()
  • 极坐标图 polar()
  • 图示 legend()
  • tex文本 matplotlib.mathtextusetex

图片示例

  • 导入图片模块 import matplotlib.image as mpimg ; 可以使用Pillow library, Matplotlib 只支持PNG
  • 读取图片 img = mpimg.imread('../../doc/_static/stinkbug.png')
  • 显示图片 imgplot = plt.imshow(img)
  • 色条 plt.colorbar()
  • 不同的图片模式 (设置显示范围等)

一个完整的plot的例子

  • 数据
  • 加载数据
  • 控制格式 plt.style.use('fivethirtyeight') plt.rcParams.update({'figure.autolayout': True})
    tips: Customizing Matplotlib with style sheets and rcParams
  • 变更plt属性,一次改变多个 pyplot.setp()
  • 调整图的尺寸 fig, ax = plt.subplots(figsize=(8, 4))
  • 坐标轴格式控制 ax.xaxis.set_major_formatter(formatter) ticker.FuncFormatter
  • 保存图片 figure.Figure.savefig()

使用style sheets与 rcParams控制格式

style sheets
  • 可选用的格式 plt.style.available
  • 定义自己的格式 mpl_configdir/stylelib/presentation.mplstyle
  • 格式组合 plt.style.use(['dark_background', 'presentation'])
  • 临时格式 with plt.style.context('dark_background'):
rcParams
  • 直接使用 matplotlib.rcParams 修改格式,如 mpl.rcParams['lines.linewidth'] = 2
  • 使用matplotlib.rc()修改格式,如 mpl.rc('lines', linewidth=4, color='g')
  • 使用matplotlib.rcdefaults()恢复默认配置
  • 默认配置都在 matplotlibrc 文件中

中级功能

Artist 教程

三层结构:

  1. 画图区域 matplotlib.backend_bases.FigureCanvas
  2. 画图方法 matplotlib.backend_bases.Renderer
  3. 使用renderercanvas上绘图 matplotlib.artist.Artist

Artist 分为两类:

  1. primitives 包括:Line2D, Rectangle, Text, AxesImage, etc
  2. containers 包括:Axis, Axes and Figure

Axes 通常按行列分布,使用 add_axes() 在任意位置创建axes

legend 教程

legend entry: 每个legend由一个或多个legend entry构成
legend key: 左边标记的颜色或模式
legend label: 描述handle的文本
legend handle: 描述的handle

cycler教程

处理周期数据 ps.

改变Figure布局

subplots() : 创建并布局axes
GridSpec : 重置figure布局
SubplotSpec : 给定布局,创建子图
subplot2grid() : 在grid内部创建子图

布局教程

  • constrained_layout = ture 会避免坐标、文字重叠现象
  • colorbars,Suptitle,Legends 布局的影响
  • 手动布局
  • 另一种选择 tight_layout()

控制图片方向

通过 originextent kwargs参数 控制图片的axes

高级功能

路径

绘制路径 path

路径效果

设置路径艺术字效果

转换坐标系

偏移?

颜色

指定颜色

设置颜色方法

  • RGB or RGBA 通过0-1的浮点数 (0.1, 0.2, 0.5) or (0.1, 0.2, 0.5, 0.3)
  • RGB or RGBA 字符串 #0f0f0f or #0f0f0f80
  • 一个0-1浮点数,表示灰度 0.5
  • 颜色缩写 {'b', 'g', 'r', 'c', 'm', 'y', 'k', 'w'}
  • X11/CSS4 颜色名 black
  • xkcd 颜色 xkcd:sky blue
  • 大写C与数字 C0 根据style进行变化,数字最大也不知道是多少...(seaborn 有六种颜色 axes.prop_cycle: cycler('color', ['4C72B0', '55A868', 'C44E52', '8172B2', 'CCB974', '64B5CD']))

定制Colorbars

  • 基本连续的colorbar
  • 离散间隔的colorbar
  • 设定长度的colorbar

定制colormap

获取改变colormap的值;设置多个colormap

colormap 分布

通常是线性分布,在某些情况下需要调整

选择colormap

选择colormap的色彩模式

文本

Matplotlib Plots 中的文本

基本命令

  • text axes上任意位置添加文本
  • annotate axes上任意位置添加注释,有一个可选的箭头
  • xlabel axes的x轴标签
  • ylabel axes的y轴标签
  • title axes标题
  • figtext figure上任意位置添加文本
  • suptitle figure添加标题

x-轴与y-轴文本的格式

  • 分布位置
  • 字体格式 text的参数
  • 间隔 pad

标题

  • 分布位置
  • 间隔 pad

刻度与刻度标签

  • 主刻度、副刻度
  • 刻度显示的范围
  • 刻度显示的格式

文本的属性与布局

  • alpha 透明度
  • color 颜色
  • position 位置
  • rotation 旋转

坐标转换?
非拉丁字符?

注释

  • 文本的样式
  • 箭头的格式(可以是圆、方框、各种箭头等)
    ps. 注释数据的高亮是由marker实现的

数学表达式

  • plot支持latex的数学表达式
    ps. 使用mathtype进行编辑,导出就可以了

使用latex渲染文本

没有必要..

实际使用

根据 官方文档示例 + API文档 根据需要进行修改与使用

使用figure.Figure.savefig()保存成PDF

猜你喜欢

转载自www.cnblogs.com/DanielOwen/p/12158934.html