断轴示例:子图大小不均匀

2023-12-22

我还没有找到调整底部和顶部图高度的解决方案断轴示例 https://matplotlib.org/stable/gallery/subplots_axes_and_figures/broken_axis.html of 绘图库 http://matplotlib.org/examples.

顺便说一句:两个图之间的空间可以通过以下方式调整:

plt.subplots_adjust(hspace=0.03)

UPDATE:

我几乎已经使用 gridspec 弄清楚了:

"""
Broken axis example, where the y-axis will have a portion cut out.
"""
import matplotlib.pylab as plt
# NEW:
import matplotlib.gridspec as gridspec
import numpy as np

pts = np.array([ 0.015,  0.166,  0.133,  0.159,  0.041,  0.024,  0.195,
    0.039, 0.161,  0.018,  0.143,  0.056,  0.125,  0.096,  0.094, 0.051,
    0.043,  0.021,  0.138,  0.075,  0.109,  0.195,  0.05 , 0.074, 0.079,
    0.155,  0.02 ,  0.01 ,  0.061,  0.008])
pts[[3,14]] += .8

# NEW:
gs = gridspec.GridSpec(2, 1, height_ratios=[1, 3])
ax = plt.subplot(gs[0])
ax2 = plt.subplot(gs[1])
plt.subplots_adjust(hspace=0.03)

ax.plot(pts)
ax2.plot(pts)
ax.set_ylim(.78,1.)
ax2.set_ylim(.0,.22)
ax.spines['bottom'].set_visible(False)
ax2.spines['top'].set_visible(False)
ax.xaxis.tick_top()
ax.tick_params(labeltop='off')
ax2.xaxis.tick_bottom()

d = .03
kwargs = dict(transform=ax.transAxes, color='k', clip_on=False)
ax.plot((0-d,0+d), (0-d,0+d), **kwargs)   # top-left diagonal
ax.plot((1-d,1+d), (0-d,0+d), **kwargs)   # top-right diagonal
kwargs.update(transform=ax2.transAxes)  # switch to the bottom axes
ax2.plot((0-d,0+d),(1-d,1+d), **kwargs) # bottom-left diagonal
ax2.plot((1-d,1+d),(1-d,1+d), **kwargs) # bottom-right diagonal

plt.show()

剩下的问题是:

  1. 两条平行线段(断轴标记) 由于 y 轴长度不同,不再平行。

  2. 进一步的问题是如何方便地定位 ylabel。

最后没看到这个选项sharex=True在网格规范中。这有关系吗?

UPDATE:

添加了 ylim 和 xlim v2 参数来确定高度比,使数据单位相等:

ylim  = [0.8, 1.0]
ylim2 = [0.0, 0.3]
ylimratio = (ylim[1]-ylim[0])/(ylim2[1]-ylim2[0]+ylim[1]-ylim[0])
ylim2ratio = (ylim2[1]-ylim2[0])/(ylim2[1]-ylim2[0]+ylim[1]-ylim[0])
gs = gridspec.GridSpec(2, 1, height_ratios=[ylimratio, ylim2ratio])
ax = plt.subplot(gs[0])
ax2 = plt.subplot(gs[1])
ax.set_ylim(ylim)
ax2.set_ylim(ylim2)

2018年更新

有一个github项目https://github.com/bendichter/brokenaxes https://github.com/bendichter/brokenaxes这可能使用起来更方便。


我自己的解决方案如下(使用 gridspec,假设两个 y 轴的单位应该相等):

 """
 Broken axis example, where the y-axis will have a portion cut out.
 """
 import matplotlib.pylab as plt
 import matplotlib.gridspec as gridspec
 import numpy as np

 pts = np.array([ 0.015,  0.166,  0.133,  0.159,  0.041,  0.024,  0.195,
     0.039, 0.161,  0.018,  0.143,  0.056,  0.125,  0.096,  0.094, 0.051,
     0.043,  0.021,  0.138,  0.075,  0.109,  0.195,  0.05 , 0.074, 0.079,
     0.155,  0.02 ,  0.01 ,  0.061,  0.008])
 pts[[3,14]] += .8

 ylim  = [0.82, 1.0]
 ylim2 = [0.0, 0.32]
 ylimratio = (ylim[1]-ylim[0])/(ylim2[1]-ylim2[0]+ylim[1]-ylim[0])
 ylim2ratio = (ylim2[1]-ylim2[0])/(ylim2[1]-ylim2[0]+ylim[1]-ylim[0])
 gs = gridspec.GridSpec(2, 1, height_ratios=[ylimratio, ylim2ratio])
 fig = plt.figure()
 ax = fig.add_subplot(gs[0])
 ax2 = fig.add_subplot(gs[1])
 ax.plot(pts)
 ax2.plot(pts)
 ax.set_ylim(ylim)
 ax2.set_ylim(ylim2)
 plt.subplots_adjust(hspace=0.03)

 ax.spines['bottom'].set_visible(False)
 ax2.spines['top'].set_visible(False)
 ax.xaxis.tick_top()
 ax.tick_params(labeltop='off')
 ax2.xaxis.tick_bottom()

 ax2.set_xlabel('xlabel')
 ax2.set_ylabel('ylabel')
 ax2.yaxis.set_label_coords(0.05, 0.5, transform=fig.transFigure)

 kwargs = dict(color='k', clip_on=False)
 xlim = ax.get_xlim()
 dx = .02*(xlim[1]-xlim[0])
 dy = .01*(ylim[1]-ylim[0])/ylimratio
 ax.plot((xlim[0]-dx,xlim[0]+dx), (ylim[0]-dy,ylim[0]+dy), **kwargs)
 ax.plot((xlim[1]-dx,xlim[1]+dx), (ylim[0]-dy,ylim[0]+dy), **kwargs)
 dy = .01*(ylim2[1]-ylim2[0])/ylim2ratio
 ax2.plot((xlim[0]-dx,xlim[0]+dx), (ylim2[1]-dy,ylim2[1]+dy), **kwargs)
 ax2.plot((xlim[1]-dx,xlim[1]+dx), (ylim2[1]-dy,ylim2[1]+dy), **kwargs)
 ax.set_xlim(xlim)
 ax2.set_xlim(xlim)

 plt.savefig('broken_axis-mod.png')
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

断轴示例:子图大小不均匀 的相关文章

随机推荐

  • 使用 json.net 反序列化没有类型信息的多态 json 类

    This 图像 API https api imgur com endpoints gallery调用返回一个包含两者的列表画廊图片 and 画廊相册以 JSON 表示的类 鉴于没有 type 属性告诉反序列化器要表示哪个类 我看不到如何使
  • Android Hello, Gallery教程——“R.styleable无法解析”

    在制作 Hello Gallery 教程 示例应用程序时 之后按照说明进行操作 http developer android com guide tutorials views hello gallery html在网站上 Eclipse
  • 避免并行继承层次结构

    我有两个并行的继承链 Vehicle lt Car lt Truck lt etc VehicleXMLFormatter lt CarXMLFormatter lt TruckXMLFormatter lt etc 我的经验是 随着并行继
  • Control-C 在 Windows 7 上的 git bash 中杀死 Ipython

    在 Linux 上摸爬滚打了这么多年之后 我又回到了可怕的 Windows 环境 我使用 Ipython 并在 git bash 中启动它 我很难使用其他东西 因为我的办公室的环境配置为使用它 因此 当我启动 Ipython 并且错误地启动
  • iPhone OpenGL ES 2.0 与 Cocos2D 混合给出了意想不到的结果

    我有非常简单的 CCScene 只有 1 个 CCLayer 包含 采用标准混合模式的 CCSprite 背景 CCRenderTexture 绘制画笔 其精灵附加到背景精灵上方的根 CCLayer bgSprite CCSprite sp
  • JPA多对多关系未插入生成的表中

    我的项目中有多对多关系 虽然我可以在两个实体表中写入 但关系表没有写入任何内容 以下是我如何使用 JPA 注释来声明这一点 教授 java Entity Table name Professor public class Professor
  • 无法在 ASP.Net MVC 3 项目中使用实体框架保存更改

    学习 asp net mvc 3 EF 代码优先 我对两者都是新手 我的例子很简单 但我仍然无法使它工作 缺少一些简单而明显的东西 我有一堂课 public class Product HiddenInput DisplayValue fa
  • Excel VBA VLookup - 错误 13 - “类型不匹配”

    我正在开发一个 Excel VBA 宏 它从另一张工作表获取客户的电子邮件 我从 VLookup 中收到错误 13 类型不匹配 For Each c In Range D3 D130 Cells If c gt 500 Then Dim e
  • 如何对 istream/istringstream 使用“固定”浮点字段?

    C 有一个名为 fixed 的 I O 操纵器 用于以固定 非科学 形式输入 输出浮点数 它对于输出工作正常 但我不明白如何让输入正常工作 考虑这个例子 include
  • JS - Onload 事件未触发[重复]

    这个问题在这里已经有答案了 这会触发 onload 事件 p Demo p 这不会触发 onload 事件 p Demo p 在第二个示例中 为什么事件没有触发 支持的元素onload are img
  • Mongoose 私人聊天消息模型

    我正在尝试将用户之间的私人消息添加到我的数据模型中 我一直在两种可能的方法之间来回选择 1 每个用户都有一个 user id chat id 对的数组 它们对应于他们正在参与的聊天 聊天模型仅存储 chat id 和消息数组 2 根本不存储
  • 处理 Windows 服务停止/暂停请求期间的延迟

    我有一个源自的 Windows 服务类ServiceBase使用一个System Timers Timer频繁运行代码 处理程序OnStop and OnPause使用计时器线程的一些信号来检查计时器是否仍在运行并等待其完成 在这种情况下
  • MySQL时间戳自动更新性能

    我们正在考虑向 mysql 表添加一个自动更新的时间戳字段 以跟踪上次更新行的时间 如 mysql 文档中所述 https dev mysql com doc refman 8 0 en timestamp initialization h
  • 如何允许授予对上传到 AWS S3 的对象的公共读取访问权限?

    我创建了一个策略 允许访问我的帐户中的单个 S3 存储桶 然后 我创建了一个仅包含此策略的组和一个属于该组的用户 用户可以按预期查看 删除文件并将文件上传到存储桶 然而 用户似乎无法授予对上传文件的公共读取权限 当 的时候授予对此对象的公共
  • 为傻瓜定制的等待

    In 异步 等待常见问题解答 http blogs msdn com b pfxteam archive 2012 04 12 10293335 aspx 斯蒂芬 图布 说 An 等待的是公开的任何类型GetAwaiter返回有效的方法aw
  • 使用字符串动态创建 (LLBLGen) Linq 查询

    我们需要生成在编码期间 设计时 100 未知的 LINQ 查询 这是因为我们的框架中的逻辑可用 该框架与任何数据项目 100 分离 对于数据 我们使用 LLBLGen 生成的数据访问代码 通常 通过使用 DLL 上的调用 我们向框架指定 而
  • T4模板VS2010获取主机组装

    我想获得 T4 模板所在项目的汇编的参考 我知道我可以通过以下方式获取项目路径Host ResolveAssemblyReference ProjectDir 我也许可以添加bin debug projectName dll因为我的程序集名
  • c# mvc:将数据从视图持久保存到控制器,然后通过 List 的单个对象

    我正在与一个View返回List
  • 缓存机制是在工厂类内部还是外部更好?

    我的问题与严格的语言无关 它更多的是一个通用的编程概念 如果我有一个 Factory 类 它有一个返回 Parser 对象的方法 并且我知道这些解析器类不需要在每个迭代周期实例化一次以上 当然 在工厂之外 就使用和对象分离而言 最好为工厂内
  • 断轴示例:子图大小不均匀

    我还没有找到调整底部和顶部图高度的解决方案断轴示例 https matplotlib org stable gallery subplots axes and figures broken axis html of 绘图库 http mat