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. Windows上玩转TensorFlow(一)

    Windows上TensorFlow的安装和环境搭建: 1.安装Python 3.5.2 2.通过Pip3安装TensorFlow CPU版 https://www.tensorflow.org/in ...

  2. ros 使用python代码启动launch文件

    在开发中我们经常会遇到使用python代码启动launch文件这样的问题.一般的做法是使用subprocess调用roslaunch.但是这种方法使用起来并不方便.要涉及到自己去控制进程的状态.由于r ...

  3. 对不队——Alpha冲刺

    第五天  日期:2018/6/20 一. 今日完成任务:专家审稿逻辑的开发 冯晓.马思远:会议网站栏目管理开发,软件功能测试 王爽.彭辉:审稿管理员分稿和稿件查找功能开发,博客撰写 吴琼.郝延婷:更换 ...

  4. 《剑指offer》第十六题(数值的整数次方)

    // 面试题:数值的整数次方 // 题目:实现函数double Power(double base, int exponent),求base的exponent // 次方.不得使用库函数,同时不需要考 ...

  5. Android仿QQ微信开场导航以及登陆界面

    相信大家对于微信等社交应用的UI界面已经都很熟悉了,该UI最值得借鉴的莫过于第一次使用的时候一些列产品介绍的图片,可以左右滑动浏览,最后 进入应用,这一效果适用于多种项目中,相信今后开发应用一定会用得 ...

  6. Linux - 命令重定向

    命令重定向, 就是将目前得到的数据转移到指定的地方.分为以下几种: >>>1>2>1>>2>>< 1. > 与 >>先看一 ...

  7. Creating SSL Certificates for CRM Test Environment

    不必找第三方去申请证书了, Windows Server 自己也可以作为一个CA的. When working on a CRM Test environment there are many sce ...

  8. 20170503xlVBA房地产数据分类连接

    Sub NextSeven_CodeFrame4() Application.ScreenUpdating = False Application.DisplayAlerts = False Appl ...

  9. android--------Retrofit+RxJava的使用

    Retrofit是Square公司开发的一款针对Android网络请求的一个当前很流行的网络请求库. http://square.github.io/retrofit/ https://github. ...

  10. android--------HttpURLConnection的get,post和图片加载

    URLConnection是个抽象类,它有两个直接子类分别是HttpURLConnection和JarURLConnection.另外一个重要的类是URL,通常URL可以通过传给构造器一个String ...