简体   繁体   中英

colorbar units don't appear in figure (matplotlib)

Im trying to do the following figure, but the units below the colorbar do not appear. The units only appear when I make the width and the height of the figure extremely large.

This is the code I use for inserting a customized colorbar:

from mpl_toolkits.axes_grid.axes_grid import AxesGrid
from mpl_toolkits.axes_grid.anchored_artists import AnchoredText
def add_at(ax, t, loc=1):
    fp = dict(size=16, weight='bold')
    _at = AnchoredText(t, loc=loc, prop=fp)
    ax.add_artist(_at)
return _at

now I make the figure

fig = plt.figure(figsize = (12, 5))
axs1 = plt.subplot2grid((2,2), (0,0))

im = m.contourf(x, y, var,levels=np.arange(vmin, vmax, delta))

and finally I add the colorbar

cax = fig.add_axes([0.42, 0.05, 0.25, 0.03]) 
cbar = fig.colorbar(im, cax, orientation='horizontal')
cbar.ax.tick_params(labelsize=12) 
cbar.set_label('units', size=16, weight='bold')

finally I save the figure

fig.savefig('name.png', dpi=300)

But I see this the figure with the colorbar but the units dont appear for lack of space. I tried adjusting the width and height of the figure, but it doesnt work unless I do a huge figure but then the spacing between sublpots is wrong.

this is the figure:

链接

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM