Matplotlib 外轴破坏子图布局

如何解决Matplotlib 外轴破坏子图布局

我想绘制一个包含 3 个子图的图形。中间的有 3 个不同的 x 轴,其中一个被分离并放置在子图下方。当我使用 Gridspec 进行布局时,绘图区域等距间隔,但不同子图的轴标签间的填充有很大不同:


这是重现该图的代码

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec

fig = plt.figure(figsize=(3.375,6.5))
gs0 = gridspec.GridSpec(3,1,figure=fig)

ax0 = fig.add_subplot(gs0[0])
ax0.set_xlabel('x label 0')

ax1 = fig.add_subplot(gs0[1])
ax1.set_xlabel('x label 1a')
secax1 = ax1.twiny()
secax1.xaxis.set_ticks_position('bottom')
secax1.xaxis.set_label_position('bottom')
secax1.spines['bottom'].set_position(('outward',40))
secax1.set_xlabel('x label 1b')
thax1 = ax1.twiny()
thax1.set_xlabel('x label 1c')

ax2 = fig.add_subplot(gs0[2])
ax2.set_xlabel('x label 2a')
ax2.set_ylabel('y label 2')
secax2 = ax2.twiny()
secax2.set_xlabel('x label 2b')

plt.tight_layout()
plt.savefig('3 subplots same size.png',dpi=300)
plt.show()

我正在寻找一种方法来使 完整 子图之间的间距相等,包括附加轴及其标签等所有内容。或者一种在网格内手动移动子图的方法。子图不需要保持相同的大小。

我尝试将 height_ratios 更改为

gs0 = gridspec.GridSpec(3,figure=fig,height_ratios=[1,1.5,1])

但它不会影响图之间的空间。

解决方法

您可以使用 plt.subplots 并在真实图之间放置不可见的“间隙图”,然后您可以通过更改间隙图的 height_ratios 来调整间隙

f,(ax0,gap1,ax1,gap2,ax2) = plt.subplots(5,1,figsize=(3.375,8),gridspec_kw={'height_ratios': [1,1.75,1]})


gap1.axis('off')# Make the gap plots invisable
gap2.axis('off')# Make the gap plots invisable

#ax0 = fig.add_subplot(gs0[0])
ax0.set_xlabel('x label 0')

#ax1 = fig.add_subplot(gs0[2])
ax1.set_xlabel('x label 1a')
secax1 = ax1.twiny()
secax1.xaxis.set_ticks_position('bottom')
secax1.xaxis.set_label_position('bottom')
secax1.spines['bottom'].set_position(('outward',40))
secax1.set_xlabel('x label 1b')
thax1 = ax1.twiny()
thax1.set_xlabel('x label 1c')

#ax2 = fig.add_subplot(gs0[5])
ax2.set_xlabel('x label 2a')
ax2.set_ylabel('y label 2')
secax2 = ax2.twiny()
secax2.set_xlabel('x label 2b')

输出

enter image description here

,

这种事情是 constrained_layouttight_layout 做得更好的地方。 tight_layout 只允许一个边距大小,因此在子图的行之间留出很多空间。 constrained_layout 为 gridspec 的每一行保留一个上下边距。

是的, constrained_layout 被标记为实验性的。这样它的行为就可以在没有警告的情况下改变。但 API 不太可能改变。

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec

fig,ax2)  = plt.subplots(3,6.5),constrained_layout=True)

ax0.set_xlabel('x label 0')

ax1.set_xlabel('x label 1a')
secax1 = ax1.twiny()
secax1.xaxis.set_ticks_position('bottom')
secax1.xaxis.set_label_position('bottom')
secax1.spines['bottom'].set_position(('outward',40))
secax1.set_xlabel('x label 1b')
thax1 = ax1.twiny()
thax1.set_xlabel('x label 1c')

ax2.set_xlabel('x label 2a')
ax2.set_ylabel('y label 2')
secax2 = ax2.twiny()
secax2.set_xlabel('x label 2b')

CLversion

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?