我有这个gridspec subplot in python :
它是 seaborn 热图的 3x3 Gridspec 矩阵,单个颜色条占据整个第三列。我想让颜色条看起来更短。在我看来,有两种选择:
一个。要么我让颜色条的情节更短
我设法减少了 gridspec 中最后一列的可用空间。
不幸的是,我还没有找到合适的方法来做到这一点。谁能帮帮我?为了清楚起见,这里的代码。 非常感谢您。
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")
最佳答案
您不需要在额外的子图网格中绘制颜色条
我建议看这里:https://matplotlib.org/3.1.1/gallery/axes_grid1/demo_colorbar_with_inset_locator.html
您可以像这样绘制颜色条:
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)
关于python - Colorbar 作为 Gridspec 中的子图(python): change size,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43120429/