1

Hopefully someone has an answer (or experience) as Googling doesn't help much.

Here's some example code of a formatter function:

from matplotlib.ticker import FuncFormatter

def latex_float(f, pos=0):
    float_str = "{0:.2g}".format(f)
    if "e" in float_str:
        base, exponent = float_str.split("e")
        return r"${0} \times 10^{{{1}}}$".format(base, int(exponent))
    else:
        return r"${}$".format(float_str)

And then later in the code

cbar = pl.colorbar(ticks=np.logspace(0, np.log10(np.max(hist)), 10), format=formatter)#'%.2e')
cbar.set_label('number density', fontsize=labelsize)
cbar.ax.tick_params(labelsize=labelsize-6)
pl.savefig('somefilename.png', bbox_inches='tight')

Sometimes, it will produce outputs like

enter image description here

and sometimes it produces outputs like

enter image description here

The goal: have the space between the colorbar and the colorbar title be a fixed width, regardless of tight_layout() being used (I would be okay with manually setting this width, but how?). How can I do that?

kratsg
  • 600
  • 1
  • 5
  • 17
  • possible duplicate of [Matplotlib - Move X-Axis label downwards, but not X-Axis Ticks](http://stackoverflow.com/questions/6406368/matplotlib-move-x-axis-label-downwards-but-not-x-axis-ticks) – Emilien Aug 21 '14 at 12:19
  • @Emilien - nope. Labelpad will push the label based on the widest tick available, rather than from the "axis" itself. Note that `tight_layout()` is being used as well, so there must be a way to do it consistently. It is also not linked to the question since what's mentioned there doesn't work with `tight_layout()` – kratsg Aug 21 '14 at 15:08

2 Answers2

1

This will allow you to set the position of the colorbar label anywhere you want using coordinates:

import numpy
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable

#Init data
data = numpy.random.random((10, 10))

#Create plotting frame
fig = plt.figure()
ax1 = fig.add_subplot(1, 1, 1)

#Plot data
im = ax1.imshow(data)

#Only required for the small spacing between figure and colorbar
divider = make_axes_locatable(ax1)
cax = divider.append_axes("right", size = "5%", pad = 0.05)

#Set colorbar, including title and padding
cbar = fig.colorbar(im, cax = cax)

cbar_title = "number density"
plt.text(1.2, .5, cbar_title,  fontsize=20, verticalalignment='center', rotation = 90, transform = ax1.transAxes)

fig.tight_layout()

What you are basically doing is just putting text at a certain position. To my knowledge, this is the only way to set the position of any title/label. This will not be influenced by the tick width as is the case with labelpad.

The result is the following:

enter image description here

The Dude
  • 3,795
  • 5
  • 29
  • 47
0

As taken from How do I adjust (offset) colorbar title in matplotlib , you can set the colorbar title manually by using

cb = colorbar()
cb.set_ticks([0,255])
ax = cb.ax
ax.text(1.3,0.5,'Foo',rotation=90)

Like that you can specify an x-distance of the title from the colorbar. (1.3 is the x value in this case.) You obviously need to set the 'ticks' according to your needs as well...

Hope this helps.

Community
  • 1
  • 1
WWhisperer
  • 97
  • 1
  • 7