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()

最新文章

  1. Python之美--Decorator深入详解
  2. 剑指offer题目11-20
  3. java.lang.IllegalStateException异常简单分析和简单解决
  4. Weblogic环境下hibernate、antlr类加载冲突问题分析及解决方案
  5. O2O在线教育平台策划方案
  6. Create screenshots of a web page using Python and QtWebKit | Roland's Blog
  7. PHP+lghttpd + postgresql 环境搭建
  8. JSF之经常使用注解
  9. hdu4758 Walk Through Squares 自动机+DP
  10. html基础认识,高手别看
  11. c语言中标识符的作用域
  12. Python_json
  13. js实现点气球小游戏
  14. Hdp安装问题杂解
  15. 图片人脸检测——OpenCV版(二)
  16. 阿里云 Ubuntu16.04 apache2 ssl证书下载与安装(必须有域名)
  17. HDU6205
  18. 虚拟机centos 7联网设置之NAT方式
  19. 记一次treegrid checkbox 选择问题
  20. TEXT 15 A text a day...

热门文章

  1. svg操纵方案 基于 D3 还是 angular?
  2. indexedDB入门
  3. yii CFormModel中的rules验证机制
  4. SGE:qsub/qstat/qdel/qhost 任务投递和监控
  5. English trip -- VC(情景课) 7 B Clothing 服装
  6. 利用 AttachThreadInput 改变其它进程的输入法状态
  7. Rspec: everyday-rspec实操。5:controller test(了解基础)
  8. 12月22日 update_columns,完成第9节。
  9. 让CLOVER默认引导WINDOWS
  10. sql表联接