python - 如何在 GridSpec 中将框架居中?

标签 python image matplotlib centering

如何在 GridSpec 分割框架中将图像居中?我有下面的图片,我想让上面的图片居中。

Picture to be centered

相关代码(部分代码,尤其是单元,来自AMUSE框架):

lim = [-5,5] | units.kpc
bins = [100,100]
xyrange = [[-5,5],[-5,5]]
cmap = cm.jet

hist2d_xy,_,_ = np.histogram2d(particles.x.value_in(units.kpc),particles.y.value_in(units.kpc),bins=bins, range=xyrange)
hist2d_xz,_,_ = np.histogram2d(particles.x.value_in(units.kpc),particles.z.value_in(units.kpc),bins=bins, range=xyrange)

maximum_value = max([hist2d_xy.max(),hist2d_xz.max()])

gs = gridspec.GridSpec(2,1, height_ratios=[3,1])
ax1 = pyplot.subplot(gs[0])


pyplot.imshow(np.flipud(hist2d_xy.T),cmap=cmap,extent = np.array(xyrange).flatten(), interpolation='none',norm=colors.LogNorm(vmin=1,vmax=maximum_value))
cbar = pyplot.colorbar()

ax2 = pyplot.subplot(gs[1])
pyplot.imshow(np.flipud(hist2d_xz.T),cmap=cmap,extent = np.array(xyrange).flatten(), interpolation='none',norm=colors.LogNorm(vmin=1,vmax=maximum_value))
pyplot.ylim([-1,1])
cbar = pyplot.colorbar()
pyplot.tight_layout()
pyplot.savefig(file_location_for_pictures+'test_%.2f.png' %threshold)
pyplot.close()

最佳答案

我替换了

gs = gridspec.GridSpec(2,1, height_ratios=[3,1])
ax1 = pyplot.subplot(gs[0])

ax2 = pyplot.subplot(gs[1])

ax1 = pyplot.subplot2grid((6,6), (0,1), colspan = 4, rowspan = 4)

ax2 = pyplot.subplot2grid((6,6), (4,0), colspan = 6, rowspan = 2)

(在正确的位置),现在它生成了这张图片(编辑了颜色并添加了一些文本,但想法很明确):

Centered upper picture

关于python - 如何在 GridSpec 中将框架居中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28408159/

相关文章:

javascript - 如何使用 javascript 从文件中获取图像尺寸(高度、宽度)

android - 如何使用 FTPClient 将图片上传到 FTP?

arrays - 在数组中的元素之间进行插值

python - 控制 x 刻度日期值

python - Crontab 不会运行 python 脚本

python - 加快 urllib.urlretrieve

python - 为什么 scipy.ndimage.gaussian_filter 没有内核大小?

python - Pandas:使用 python 检查值是否为纪元时间

python - 在 `any` 函数中找到失败的元素

python - 不要在子图中显示标签