I'm generating a horizontal colorbar with this code:
cbaxes = fig.add_axes([0.05, 0.15, 0.9, 0.025]) # setup colorbar axes.
cb = fig.colorbar(cax=cbaxes, mappable=mappable, orientation='horizontal',)
cb.set_label(r"$[10^{14}\ molec\,cm^{-2}]$", fontname='Arial', fontsize='small')
cbytick_obj = plt.getp(cb.ax.axes, 'xticklabels')
plt.setp(cbytick_obj, color='r', fontsize='x-small')
cb.ax.set_yticks(arange(vmin, vmax, 2), size='small')
However, I want the label be printed above the colorbar (instead of below). How can I do that?
The position of the Matplotlib color bar can be changed according to our choice by using the functions from Matplotlib AxesGrid Toolkit. The placing of inset axes is similar to that of legend, the position is modified by providing location options concerning the parent box. Axes into which the colorbar will be drawn.
To have one color bar for all subplots with Python, we can use matplotlib's subplots_adjust and colorbar methods. to call subplots_adjust on the fig subplot with the right argument to adjust the position of the subplots.
Here's how I did it. The key seems to be to call the colorbar's axes' set_xlabel
method instead:
cbaxes = fig.add_axes([0.05, 0.05, 0.9, 0.025])
cb = fig.colorbar(cax=cbaxes, mappable=mappable, orientation='horizontal')
cbaxes.set_xlabel(r"$[10^{14}\ molec\,cm^{-2}]$", fontname='Arial',
fontsize='small', labelpad=-35)
cb.set_ticks(arange(vmin, vmax + 1, 2))
cbytick_obj = plt.getp(cb.ax.axes, 'xticklabels')
plt.setp(cbytick_obj, fontsize='x-small')
You could try using the following code:
cb.ax.xaxis.set_ticks_position('top')
cb.ax.xaxis.set_label_position('top')
You could also use: cb.ax.tick_params(axis='x',direction='in',labeltop='on')
There are also plentiful other parameters that could be set up there.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With