将子图与颜色栏对齐 [英] Align subplot with colorbar

查看:47
本文介绍了将子图与颜色栏对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试共享 imshow 的x轴,该轴必须为正方形和经典图:

I am trying to share the x-axis of a imshow that have to be square and a classique plot:

  1. 展示必须是正方形的
  2. 带有颜色条
  3. 下面的情节应该共享相同的轴(或至少看起来与 imshow 对齐)

我花了两天时间,现在我疯了.有人知道如何对齐它们吗?

I spent two days on it, and now I am crazy. Did someone know how to align them ?

用于产生图像的代码如下.

The code used to produce the image is bellow.

def myplot( Nbin=20 ):

X = np.random.rand(1000)
Y = np.random.rand(1000)
h2, yh2, xh2 = np.histogram2d( Y, X, bins=[Nbin,Nbin] )
h1, xh1 = np.histogram( X, bins=Nbin )
######################################
######################################
fig = plt.figure(  )
gs = gridspec.GridSpec( 3, 2 )
######################################
######################################
ax1 = plt.subplot( gs[:-1,:] )
im = plt.imshow( h2, interpolation='nearest', origin='lower',
                 extent=[xh2[0],xh2[-1],yh2[0],yh2[-1]] )
cb = plt.colorbar( im, ax=ax1 )
plt.xlim( xh1[0], xh1[-1] )
plt.ylim( xh1[0], xh1[-1] )
ax1.tick_params( axis='x', which='both', bottom='on', top='on', labelbottom='off' )
######################################
######################################
ax2 = plt.subplot( gs[-1,:] )
plt.plot( xh1[:-1] + np.diff(xh1)/2., h1 )
plt.xlim( xh1[0], xh1[-1] )
cm = plt.cm.Blues
cb2 = plt.colorbar( ax=ax2 )
ax2.tick_params( axis='x', which='both', bottom='on', top='on', labelbottom='on' )
######################################
######################################
fig.tight_layout()
fig.subplots_adjust(hspace=0.05)
cb2.ax.set_visible(False)

推荐答案

我可以想象将第二个轴直接放在图像下方的最简单方法是使用 mpl_toolkits.axes_grid1.make_axes_locatable.这样可以缩小图像,但要以新创建的子图为代价,并且同样可以用于放置颜色条.

I could imagine that the easiest way to have the second axes directly below the image is to use mpl_toolkits.axes_grid1.make_axes_locatable. This allows to shrink the image at the expense of the newly created subplot and can equally be used to position the colorbar.

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable

Nbin=20

X = np.random.rand(1000)
Y = np.random.rand(1000)
h2, yh2, xh2 = np.histogram2d( Y, X, bins=[Nbin,Nbin] )
h1, xh1 = np.histogram( X, bins=Nbin )

fig = plt.figure(  )


ax1 = plt.subplot(111)
im = ax1.imshow( h2, interpolation='nearest', origin='lower',
                 extent=[xh2[0],xh2[-1],yh2[0],yh2[-1]] )

plt.xlim( xh1[0], xh1[-1] )
plt.ylim( xh1[0], xh1[-1] )
ax1.tick_params( axis='x', which='both', bottom='on', top='on', labelbottom='off' )


divider = make_axes_locatable(ax1)
ax2 = divider.append_axes("bottom", size="50%", pad=0.08)
cax = divider.append_axes("right", size="5%", pad=0.08)
cb = plt.colorbar( im, ax=ax1, cax=cax )

#ax2 = plt.subplot( gs[-1,:] )  # , sharex=ax1
ax2.plot( xh1[:-1] + np.diff(xh1)/2., h1 )
ax2.set_xlim( xh1[0], xh1[-1] )
cm = plt.cm.Blues

ax2.tick_params( axis='x', which='both', bottom='on', top='on', labelbottom='on' )

plt.show()

这篇关于将子图与颜色栏对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆