Colorbar作为Gridspec(python)中的子图:更改大小 [英] Colorbar as a subplot in Gridspec (python): change size
问题描述
我有这个
这是海洋热图的3x3 Gridspec矩阵,其中单个色条占据了整个第三列.我想使颜色条看起来更短.我的看法有两种选择:
It is a 3x3 Gridspec matrix of seaborn heatmaps with a single colorbar occupying the whole third column. I would like to make the colorbar look shorter. The way I see it, there are two choices:
a.要么使颜色条的图变短
a. Either I make the plot of the colorbar shorter
b.我设法减少了gridspec中最后一列的可用空间.
b. I manage to reduce the available space for the last column in the gridspec.
不幸的是,我还没有找到合适的方法来做到这一点.有人可以帮我吗?这里的代码为清晰起见.提前非常感谢您.
Unfortunately, I haven't found a proper way to do it. Could anyone help me? Here the code for clarity. Thank you very much in advance.
fig=plt.figure(figsize=(10,10))
gs = gridspec.GridSpec(2, 3, width_ratios=[1,1,0.1], height_ratios=[1,1])
gs.update(left=0.1, right=0.95, wspace=0.2, hspace=0.4)
#(0,0) PLOT
axh=plt.subplot( gs[0,0] )
sns.heatmap(M11,cmap="RdBu_r",cbar=False,linewidths=.5,xticklabels=True,yticklabels=True)
axh.set_xlabel('x_axis',fontsize=15);
axh.set_ylabel('y_axis',fontsize=15)
#(0,1) PLOT
ax0=plt.subplot( gs[0,1] )
sns.heatmap(M12,vmin=0,vmax=1,annot=False,cmap="RdBu_r",cbar=False,linewidths=.5,xticklabels=True,yticklabels=False)
ax0.set_xlabel('x_axis',fontsize=15);
#(1,0) PLOT
axh1=plt.subplot( gs[1,0],sharex=axh )
sns.heatmap(M21,cmap="RdBu_r",cbar=False,linewidths=.5,xticklabels=True,yticklabels=True)
axh1.set_xlabel('x_axis',fontsize=15);
axh1.set_ylabel('y_axis',fontsize=15)
#(1,1) PLOT
ax3=plt.subplot( gs[1,1] )
sns.heatmap(M22,vmin=0,vmax=1,annot=False,cmap="RdBu_r",cbar=False,linewidths=.5,xticklabels=True,yticklabels=False)
ax3.set_xlabel('x_axis',fontsize=15);
#(:,4) PLOT: COLORBAR
ax6=plt.subplot(gs[:,2] )
cb1 = matplotlib.colorbar.ColorbarBase(ax6, cmap="RdBu_r")
推荐答案
您无需在额外的子图网格中绘制颜色条
You don't need to plot the colorbar in an extra subplot grid
我建议在这里查看: https://matplotlib.org/3.1.1/gallery/axes_grid1/demo_colorbar_with_inset_locator.html
您可以像这样绘制颜色条:
You can plot the colorbar like:
fig = plt.figure(figsize=(6, 6))
grid = plt.GridSpec(4, 4, hspace=0, wspace=0)
main_ax = fig.add_subplot(grid[:-1, 1:])
y_hist = fig.add_subplot(grid[:-1, 0])
x_hist = fig.add_subplot(grid[-1, 1:]
im = main_ax.imshow(array, **kwags) # <- your plots here
y_hist.plot(x,y)
x_hist.plot(x,y)
axins = inset_axes(x_hist, # here using axis of the lowest plot
width="5%", # width = 5% of parent_bbox width
height="340%", # height : 340% good for a (4x4) Grid
loc='lower left',
bbox_to_anchor=(1.05, 0.3, 1, 1),
bbox_transform=x_hist.transAxes,
borderpad=0,
)
cb = fig.colorbar(im, cax=axins)
这篇关于Colorbar作为Gridspec(python)中的子图:更改大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!