Matplotlib 中文用户指南 3.3 使用 GridSpec 自定义子图位置

  • ax:matplotlib.axes._subplots.AxesSubplot,的基本操作

    • ax.set_xticks([]), ax_set_yticks([]):关闭坐标刻度
    • ax.axis('off'):关闭坐标轴
    • ax.set_title():设置标题

1. subplots

fig, ax = plt.subplots(nrows=1, ncols=2, figsize=(8, 4))
ax[0].plot(...)
ax[0].set_xlabel(...)
ax[0].set_title(...) ax[1].plot(...)
ax[1].set_xlabel(...)
...
fig, (ax1, ax2, ax3) = plt.subplots(3, 1, sharex=True)

更一般的做法(也是matlab的风格)是这样的:

fig = plt.figure()          # 创建一个figure对象,底下的一切显示均在此figure上完成

plt.subplot(121)
plt.imshow(img1)
plt.subplot(122)
plt.imshow(img2) plt.show() # 当然放在一个range里边
for i in range(ndim):
plt.subplot(ndim//5, 5, i+1)
plt.hist()

2. subplot2grid

  • subplot2grid


    # 以下两条语句等价 ax = plt.subplot2grid((2,2),(0, 0))
    ax = plt.subplot(221) # 下标从 0 开始
    • colspan、rowspan 与 索引的对应关系
    import matplotlib.pyplot as plt
    
    def disable_axis(ax):
    ax.set_xticks([])
    ax.set_yticks([])
    # ax.axis('off') def set_title(ax, title):
    ax.set_title(title) def subplot2grid_demo():
    ax1 = plt.subplot2grid((3, 3), (0, 0), colspan=2)
    disable_axis(ax1)
    set_title(ax1, 'ax1')
    ax2 = plt.subplot2grid((3, 3), (1, 0))
    disable_axis(ax2)
    set_title(ax2, 'ax2')
    ax3 = plt.subplot2grid((3, 3), (1, 1))
    disable_axis(ax3)
    set_title(ax3, 'ax3')
    ax4 = plt.subplot2grid((3, 3), (0, 2), rowspan=2)
    disable_axis(ax4)
    set_title(ax4, 'ax4')
    ax5 = plt.subplot2grid((3, 3), (2, 0), colspan=3)
    disable_axis(ax5)
    set_title(ax5, 'ax5')
    # plt.xticks([])
    # plt.yticks([])
    # plt.axis('off')
    plt.show() if __name__ == '__main__':
    subplot2grid_demo()

3. GridSpec:方便的切片操作

GridSpec 提供了十分方便的切片操作,实现上述功能,则只需如下代码:

def gridspec_demo():
gs = gridspec.GridSpec(3, 3)
ax1 = plt.subplot(gs[0, :2])
ax2 = plt.subplot(gs[1, 0])
ax3 = plt.subplot(gs[1, 1])
ax4 = plt.subplot(gs[0:2, 2])
ax5 = plt.subplot(gs[2:, :])
plt.show()

matplotlib 操作子图(subplot,axes)的更多相关文章

  1. (转)Matplotlib的子图subplot的使用

    转:https://www.jianshu.com/p/de223a79217a 前言 Matplotlib的可以把很多张图画到一个显示界面,这就设计到面板切分成一个一个子图.这是怎么做到的呢.mat ...

  2. Matlab绘制子图subplot使用攻略

    参考:https://jingyan.baidu.com/article/915fc414ad794b51394b20e1.html Matlab绘制子图subplot使用攻略 听语音 原创 | 浏览 ...

  3. matplotlib 的 subplot, axes and axis

    fig = plt.figure('多图', (10, 10), dpi=80) #第一个指定窗口名称,第二个指定图片大小,创建一个figure对象 plt.subplot(222) #2*2的第二个 ...

  4. matplotlib ----- 多子图, subplots

    这一篇讲的比较详细. http://matplotlib.org/examples/pylab_examples/subplots_demo.html 官方文档给出的subplots用法, http: ...

  5. matplotlib添加子图(拼图功能)

    我们已经知道,matplotlib是python中的一个十分好用的作图库,它的简单的使用方法可以在之前的随笔中找到.传送门:https://www.cnblogs.com/chester-cs/p/1 ...

  6. matplotlib画子图时设置总标题

    matplotlib subplots绘图时 设置总标题 :fig.suptitle(name)

  7. matplotlib调整子图大小

    因为子图太多而导致每个子图很小,很密,如何调整

  8. matplotlib绘制子图

    fig,subs = plt.subplots(2,2) subs[0][0].plot(data_math_C1) subs[0][0].set_title('C_1 曲线') subs[0][1] ...

  9. python使用matplotlib:subplot绘制多个子图

    1 问题描述 matploglib 能够绘制出精美的图表, 有些时候, 我们希望把一组图放在一起进行比较, 有没有什么好的方法呢? matplotlib 中提供的 subplot 可以很好的解决这个问 ...

随机推荐

  1. shell 函数调用

    例一 #!/bin/bash create_link() { filelist=`ls $` for file in $filelist do echo $/$file done } create_l ...

  2. css3转盘

    此效果涉及到 : 1. css 画图形 2. css3 动态效果 3. 贝塞尔曲线 用法 <style> .lds-hourglass { display: inline-block; p ...

  3. Java中泛型使用

    Java中泛型使用 泛型作用: 泛型:集合类添加对象不用强转 反射机制:将泛型固定的类的所有方法和成员全部显示出来 核心代码: ArrayList<Ls> ff=new ArrayList ...

  4. 雷林鹏分享:Ruby 哈希(Hash)

    Ruby 哈希(Hash) 哈希(Hash)是类似 "employee" => "salary" 这样的键值对的集合.哈希的索引是通过任何对象类型的任意键 ...

  5. Jmeter JDBC的使用

    1.当我们在对接口进行断言或进行多个接口串联时,常常会需要从DB查询数据来做辅助,连接JDBC需要有支持DB的jar包:官网下载地址:https://dev.mysql.com/downloads/c ...

  6. WPF操作RichTextBox

    http://www.cnblogs.com/wzwyc/p/6291895.html

  7. LeetCode--141--环形链表

    问题描述: 给定一个链表,判断链表中是否有环. 思路:用快的指针追慢的指针,只要有圈,一定能追上. 错误: class Solution(object): def hasCycle(self, hea ...

  8. Dajngo的CBV和FBV

    CBV: class. base. view 路由: url(r'students/', views.StudentsView.as_view()) 视图: from django.views imp ...

  9. Mass Change Queries CodeForces - 911G (线段树合并)

    链接 大意: 给定序列, 每次操作将区间[l,r]中的x全改为y, 最后输出序列 权值范围比较小, 对每个权值开一颗线段树, 每次将x合并到y上即可 #include <iostream> ...

  10. 『Nltk』常用方法

    引言 在nltk的介绍文章中,前面几篇主要介绍了nltk自带的数据(书籍和语料),感觉系统学习意义不大,用到哪里看到那里就行(笑),所以这里会从一些常用功能开始,适当略过对于数据本体的介绍. 文本处理 ...