我试着分享一个imshow
的x轴,它必须是正方形和一个经典的绘图:
我花了两天时间,现在我疯了。有人知道如何排列它们吗?在
用于生成图像的代码如下所示。在
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
。这允许以新创建的子图为代价来缩小图像,并且可以同样地用于定位colorbar。在相关问题 更多 >
编程相关推荐