简体   繁体   English

使用tight_layout()缩小Matplotlib Gridspec中各个子图之间的空间

[英]Shrinking space between individual subplots in Matplotlib Gridspec with tight_layout()

I'm using matplotlib.gridspec and tight_layout() to create a complex plot layout. 我正在使用matplotlib.gridspectight_layout()创建复杂的图版面。 My current code looks like 我当前的代码看起来像

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import matplotlib
from matplotlib.ticker import MaxNLocator

fig = plt.figure(figsize=(15,15))
gs1 = gridspec.GridSpec(8, 2)

gs1.update(left=0.05, right=0.95, wspace=0.05, hspace=0.05)

ax1 = plt.subplot(gs1[0:4, 0]) # Original
ax2 = plt.subplot(gs1[0:4, 1]) # Model
ax3 = plt.subplot(gs1[4:8, 0]) # Residual+Sky
ax4 = plt.subplot(gs1[4:7, 1]) # SB profile
ax5 = plt.subplot(gs1[7:8, 1])# SB residuals

# Hide tick labels
plt.setp(ax1.get_yticklabels(), visible=False)
plt.setp(ax1.get_xticklabels(), visible=False)
plt.setp(ax2.get_yticklabels(), visible=False)
plt.setp(ax2.get_xticklabels(), visible=False)
plt.setp(ax3.get_yticklabels(), visible=False)
plt.setp(ax3.get_xticklabels(), visible=False)
plt.setp(ax4.get_xticklabels(), visible=False)

ax4.invert_yaxis()
ax4.set_ylabel(r'Surface Brightness, $\mu$ [mag arcsec$^{-2}$]')
ax5.set_ylabel(r'$\Delta\mu$')
ax5.set_xlabel('Semi-major Axis [arcsec]')
ax5.grid(b=True)
ax4.set_xscale('log')
ax5.set_xscale('log')

gs1.tight_layout(fig)

nbins = len(ax5.get_xticklabels())
ax5.yaxis.set_major_locator(MaxNLocator(nbins=nbins, prune='upper'))
ax4.yaxis.set_major_locator(MaxNLocator(nbins=nbins, prune='upper'))

# Show the plot
plt.show()

which produces a layout that looks like 产生看起来像

在此处输入图片说明

What I need to do is either 我需要做的是

  1. shrink the vertical space between ax4 and ax5 (the two bottom-right subplots), or 缩小ax4和ax5之间的垂直空间(两个右下子图),或者

  2. make ax4 and ax5 share the same x-axis, such that the space between the subplots is zero 使ax4和ax5共享相同的x轴,以使子图之间的空间为零

I really like the way gridspec and tight_layout() formats the plots, however I don't know of a way to "force" the spacing between individual subplots. 我真的很喜欢gridspectight_layout()格式化图的方式,但是我不知道一种“强制”各个子图之间间距的方法。 Is there an easy way to do this using both matplotlib.gridspec and tight_layout() ? 是否有一种简单的方法同时使用matplotlib.gridspectight_layout()

You can use the get_position and set_position methods of the axes instance to change the location of an axes in the figure ( http://matplotlib.org/api/axes_api.html ). 您可以使用轴实例的get_position和set_position方法来更改图中轴的位置( http://matplotlib.org/api/axes_api.html )。

get_position returns a Bbox instance and you can use get_points to obtain a 2x2 numpy array of the form [[x0, y0], [x1, y1]], where x0, y0, x1, y1 are the figure coordinates of your axes. get_position返回一个Bbox实例,您可以使用get_points以[[x0,y0],[x1,y1]]的形式获取2x2 numpy数组,其中x0,y0,x1,y1是轴的图形坐标。

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import matplotlib
from matplotlib.ticker import MaxNLocator

fig = plt.figure(figsize=(15,15))
gs1 = gridspec.GridSpec(8, 2)

gs1.update(left=0.05, right=0.95, wspace=0.05, hspace=0.05)

ax1 = plt.subplot(gs1[0:4, 0]) # Original
ax2 = plt.subplot(gs1[0:4, 1]) # Model
ax3 = plt.subplot(gs1[4:8, 0]) # Residual+Sky
ax4 = plt.subplot(gs1[4:7, 1]) # SB profile
ax5 = plt.subplot(gs1[7:8, 1])# SB residuals

# Hide tick labels
plt.setp(ax1.get_yticklabels(), visible=False)
plt.setp(ax1.get_xticklabels(), visible=False)
plt.setp(ax2.get_yticklabels(), visible=False)
plt.setp(ax2.get_xticklabels(), visible=False)
plt.setp(ax3.get_yticklabels(), visible=False)
plt.setp(ax3.get_xticklabels(), visible=False)
plt.setp(ax4.get_xticklabels(), visible=False)

ax4.invert_yaxis()
ax4.set_ylabel(r'Surface Brightness, $\mu$ [mag arcsec$^{-2}$]')
ax5.set_ylabel(r'$\Delta\mu$')
ax5.set_xlabel('Semi-major Axis [arcsec]')
ax5.grid(b=True)
ax4.set_xscale('log')
ax5.set_xscale('log')

gs1.tight_layout(fig)

nbins = len(ax5.get_xticklabels())
ax5.yaxis.set_major_locator(MaxNLocator(nbins=nbins, prune='upper'))
ax4.yaxis.set_major_locator(MaxNLocator(nbins=nbins, prune='upper'))

# change axis location of ax5
pos4 = ax4.get_position()
pos5 = ax5.get_position()

points4 = pos4.get_points()
points5 = pos5.get_points()

points5[1][1]=points4[0][1]

pos5.set_points(points5)

ax5.set_position(pos5)
# Show the plot
plt.show()

This code should produce this: plot with adjusted ax5 此代码应产生以下内容: 调整了ax5的绘图

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 matplotlib tiny_layout + gridspec + fig.suptitle 看起来很糟糕 - matplotlib tight_layout + gridspec + fig.suptitle looks bad matplotlib 子图没有间距,限制图形大小和tight_layout() - matplotlib subplots with no spacing, restricted figure size and tight_layout() Cartopy:即使使用tight_layout,子图之间的垂直空间也太大 - Cartopy: Too much vertical space between subplots even with tight_layout Matplotlib tight_layout - 删除额外的白色/空白空间 - Matplotlib tight_layout — remove extra white/empty space Matplotlib(GridSpec)-子图轴标签被截断了吗? 尝试过`tight_layout` - Matplotlib (GridSpec) - Subplot axis labels being cut-off? Tried `tight_layout` 子图:tight_layout 改变图形大小 - Subplots: tight_layout changes figure size Pandas / Matplotlib直方图问题 - x轴刻度消失了子图和额外不需要的图表空间。 尝试了tight_layout和subplots_adjust - Pandas/Matplotlib histogram issues - x-axis ticks disappearing w subplots and extra unneeded chart space. Tried tight_layout and subplots_adjust 为什么matplotlib.gridspec的紧密布局不能与pyplot.subplot一起正常工作,而与fig.add_subplot一起工作呢? - Why does matplotlib.gridspec's tight_layout not work correctly with pyplot.subplot whereas it does with fig.add_subplot? 使用matplotlib时的ValueError tight_layout() - ValueError when using matplotlib tight_layout() matplotlib渲染中tight_layout的问题 - Issue with tight_layout in matplotlib rendering
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM