Colorbar as a subplot in Gridspec (python): change size

元气小坏坏 提交于 2021-02-10 03:47:12

问题


I have this gridspec subplot in python:

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. Either I make the plot of the colorbar shorter

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")

回答1:


You don't need to plot the colorbar in an extra subplot grid

I would recommend to look here: 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)

Result of axins setting for my plot



来源:https://stackoverflow.com/questions/43120429/colorbar-as-a-subplot-in-gridspec-python-change-size

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!