首页 > 其他分享 >[matplotlib] axes 布局

[matplotlib] axes 布局

时间:2023-06-24 23:45:41浏览次数:42  
标签:subplot axes level 布局 matplotlib add plt right fig

从high-level方法和low-level方法讲figure上的axes布局。
从high-level的角度,我们可以用以下几种方法,直接设置布局:

import matplotlib.pylot as plt
# 创建2x2的布局

# 方法1:
fig, axes = plt.subplots(nrows=2, ncols=2)

# 方法2:
fig, axes = plt.subplot_mosaic([['upper left', 'upper right'],  ['lower left', 'lower right']])

从low-level的角度来看,figure的布局由两个特性决定:

  1. GridSpec
    明确figure上的网格(grid)形状,而所有之后的绘图(subplot)都是在网格形状上进行布局。
  2. SubplotSpec
    确定subplot的定位。

基本2x2布局

high-level

import matplotlib.pylot as plt
# 创建2x2的布局

# 方法1:
fig, axs = plt.subplots(ncols=2, nrows=2, figsize=(5.5, 3.5),
                        layout="constrained")

# 方法2:
fig, axd = plt.subplot_mosaic([['upper left', 'upper right'],
                               ['lower left', 'lower right']],
                              figsize=(5.5, 3.5), layout="constrained")

low-level

fig = plt.figure(figsize=(5.5, 3.5), layout="constrained")
spec = fig.add_gridspec(ncols=2, nrows=2)

ax0 = fig.add_subplot(spec[0, 0])
ax1 = fig.add_subplot(spec[0, 1])
ax2 = fig.add_subplot(spec[1, 0])
ax3 = fig.add_subplot(spec[1, 1])

axes在行或列方向拓展

high-level

fig, axd = plt.subplot_mosaic([['upper left', 'right'],
                               ['lower left', 'right']],
                              figsize=(5.5, 3.5), layout="constrained")

image

low-level

fig = plt.figure(figsize=(5.5, 3.5), layout="constrained")
spec = fig.add_gridspec(2, 2)

ax0 = fig.add_subplot(spec[0, :])
ax10 = fig.add_subplot(spec[1, 0])
ax11 = fig.add_subplot(spec[1, 1])

image

调整axes的宽和高比例

high-level
通过指定width_ratio和height_ratio参数,或者指定gridspec_kw参数

gs_kw = dict(width_ratios=[1.4, 1], height_ratios=[1, 2])
fig, axd = plt.subplot_mosaic([['upper left', 'right'],
                               ['lower left', 'right']],
                              gridspec_kw=gs_kw, figsize=(5.5, 3.5),
                              layout="constrained")

image

low-level

fig = plt.figure(layout=None, facecolor='0.9')
# add_gridspec()left和right指定在figure上绘图的左右范围,wspace和hspace指定axes之间上下左右间距
gs = fig.add_gridspec(nrows=3, ncols=3, left=0.05, right=0.75,
                      hspace=0.1, wspace=0.05)

ax0 = fig.add_subplot(gs[:-1, :])
ax1 = fig.add_subplot(gs[-1, :-1])
ax2 = fig.add_subplot(gs[-1, -1])

image

在同一figure上设置多个axes grid

high-level

#利用subfigure方法
fig = plt.figure(layout="constrained")
subfigs = fig.subfigures(1, 2, wspace=0.07, width_ratios=[1.5, 1.])
axs0 = subfigs[0].subplots(2, 2)
axs1 = subfigs[1].subplots(3, 1)

image

low-level

# 利用add_subgidspec方法
fig = plt.figure(layout="constrained")
gs0 = fig.add_gridspec(1, 2)

gs00 = gs0[0].subgridspec(2, 2)
gs01 = gs0[1].subgridspec(3, 1)

image

标签:subplot,axes,level,布局,matplotlib,add,plt,right,fig
From: https://www.cnblogs.com/jetpeng/p/17501807.html

相关文章

  • [matplotlib] Artist中的container
    Artist对象分为primitives和containers两种。primitives对象主要是我们想在figure上画出的各类图形对象。containers对象主要用来放置primitives对象,有四种,即figure、axes、axis和tick。1.Figure创建figure:importmatplotlib.pyplotaspltfig=plt.figure()使用plt.......
  • [matplotlib] Legend 图例
    组成legendentries图例项,每个项包括一个key和一个labellegendkeylegendlabellegendhandler即产生legendentry的相应的原对象。创建legendax.legend(handlers,labels)handlers和labels可以是列表,labels可以利用handler对象自己的label,也可以设置legend的时候重新......
  • [matplotlib] 基础知识
    基本架构脚本层(scripting)脚本层是Matplotlib结构中的最顶层。我们编写的绘图代码大部分代码都在该层运行,它的主要工作是负责生成图形与坐标系。美工层(artist)美工层是结构中的第二层,它提供了绘制图形的元素时的给各种功能,例如,绘制标题、轴标签、坐标刻度等。后端......
  • vue学习第25天 移动WEB开发----响应式布局
    目标:1)响应式原理2)使用媒体查询完成响应式导航3)使用Bootstrap的栅格系统4)使用Bootstrap的响应式工具5)完成阿里百秀首页案例 目录:1)响应式开发2)Bootstrap前端开发框架3)Bootstrap栅格系统4)阿里百秀首页案例  ......
  • Android 线性布局平分宽度item的隐藏问题
    原文:Android线性布局平分宽度item的隐藏问题-Stars-One的杂货小窝一直只使用layout_weight来平分布局,但是如果隐藏了某个item,会导致其他item宽高有所变化于是询问了ChatGpt后,才是了解到LinearLayout的weightSum这个属性的使用需求一行有3个item平分线性布局,宽度都是......
  • mvc区域使用布局页
    使用主程序下的_Layout.cshtml视图页进行布局(可以设置每个页面的共有样式) 在区域内放置ViewStart.cshtml页 页内编辑母版页路径@{Layout="/Views/Shared/_Layout.cshtml";} 然后该区域下的所有页面就用到了这个母版页,如图 若该区域下的某个页不使用这个布......
  • c++中虚析构函数如何实现多态的、内存布局如何?
    作者:冯Jungle链接:https://www.zhihu.com/question/36193367/answer/2242824055来源:知乎著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。之前Jungle写过一篇文章《探究C++:虚函数表究竟怎么回事?》,主要通过测试代码来验证虚函数表的存在,进而说明C++的多态机制......
  • Dash应用页面整体布局技巧
    本文示例代码已上传至我的Github仓库:https://github.com/CNFeffery/dash-master大家好我是费老师,对于刚上手dash应用开发的新手朋友来说,如何进行合理且美观的页面整体布局构建是一道“难题”。今天的文章中,我就将为大家介绍有关dash应用页面布局的一些实用技巧,并附上几个可以直......
  • Android 使用 TableLayout 布局拉伸宽度
    布局文件<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical"android:layout_width="fill_parent" android:lay......
  • 【Android】如何实现同一个布局保证高度不变,使用不同高度的背景
    背景预实现一个切换tab,实现选中与未选中的背景切换,特别之处在于选中背景图和未选中背景图高度不相同,切换之后需要在java代码中动态设置LayoutParams改变高度。预期效果当前问题点选中背景为.9图,未选中背景为xml中通过shape实现。将当前ViewGroup设置为选中状态的固定高度选中效果正......