Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

place a colorbar label above horizontal colorbar (instead of below)

Tags:

matplotlib

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?

like image 915
andreas-h Avatar asked Oct 15 '12 11:10

andreas-h


People also ask

How to change colorbar position in matplotlib?

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.

How do you make one Colorbar for all subplots in Python?

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.


3 Answers

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')
like image 190
andreas-h Avatar answered Sep 29 '22 23:09

andreas-h


You could try using the following code:

cb.ax.xaxis.set_ticks_position('top')
cb.ax.xaxis.set_label_position('top')
like image 23
Achim Avatar answered Sep 29 '22 22:09

Achim


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.

like image 31
wiswit Avatar answered Sep 29 '22 22:09

wiswit