Place the color bar label above the horizontal color bar (instead of below)

I create a horizontal color bar 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 to be printed above the color bar (instead of below). How can i do this?

+4
source share
3 answers

Here is how I did it. It seems that the key is to use the set_xlabel method of the color panel axis 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') 
+4
source

You can try using the following code:

 cb.ax.xaxis.set_ticks_position('top') cb.ax.xaxis.set_label_position('top') 
+5
source

You can also use: cb.ax.tick_params (axis = 'x', direction = 'to', labeltop = 'on')

There are also many other options that can be set there.

+3
source

Source: https://habr.com/ru/post/1439810/


All Articles