3
votes

When plotting a colorbar, the top label (I guess this would be called the offset) is mis-centred. This didn't use to happen, I have examples of old code where it was centred above the colorbar, but I have no clue what has changed.

Example:

import numpy as np
import matplotlib.pyplot as plt

z = np.random.random((10,10))

fig, ax = plt.subplots()
im = ax.imshow(z)
cb = fig.colorbar(im)

cb.formatter.set_powerlimits((0, 0))
cb.update_ticks()

plt.show()

Gives this:

Offset colorbar notation

As an example of how it used to look (taken from one of my old papers, so different data etc.)

Proper colorbar notation

Using the most recent anaconda python 2.7, on MacOSX, mpl version 1.5.0

Edit: I should also note, tight_layout() does not improve this either, though it is missing from the working example.

2
More direct comparison in this SO answer. - user707650
I guess this figure is the one reason why jet should not be removed as colormap, and still has its uses: I can stare at it endlessly and slowly float away into some form of hyperspace. - user707650

2 Answers

6
votes

You can simply use set_offset_position for the y-axis of the colorbar. Compare:

fig, ax = plt.subplots()                
im = ax.imshow(np.random.random((10,10)))                    
cb = fig.colorbar(im)     
cb.formatter.set_powerlimits((0, 0))
cb.ax.yaxis.set_offset_position('right')                         
cb.update_ticks()
plt.show()

enter image description here

versus

fig, ax = plt.subplots()
im = ax.imshow(np.random.random((10,10)))
cb = fig.colorbar(im)
cb.formatter.set_powerlimits((0, 0))
cb.ax.yaxis.set_offset_position('left')
cb.update_ticks()
plt.show()

enter image description here

All in all, it simply looks like the default has changed from right to left.

2
votes

Using your above code and matplotlib version 1.4.3 I get the following plot enter image description here

So this may be a version issue. One possible work around could be to use cb.ax.text()

# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt

z = np.random.random((10,10))

fig, ax = plt.subplots()
im = ax.imshow(z)
cb = fig.colorbar(im)
cb.ax.text(-0.25, 1, r'$\times$10$^{-1}$', va='bottom', ha='left')

plt.show()

This way you have more control over the centring. The above code gives me the following plot enter image description here Note that I use an r at the start of the string so that $\times$ produces the correct symbol.