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
and sometimes it produces outputs like
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?