I need to share the same colorbar for a row of subplots. Each subplot has a symmetric logarithmic scaling to the color function. Each of these tasks has a nice solution explained here on stackoverflow: For sharing the color bar and for nicely formatted symmetric logarithmic scaling.
However, when I combine both tricks in the same code, the colorbar "forgets" that is is supposed to be symmetric logarithmic. Is there a way to work around this problem?
Testing code is the following, for which I combined the two references above in obvious ways:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import ImageGrid
from matplotlib import colors, ticker
# Set up figure and image grid
fig = plt.figure(figsize=(9.75, 3))
grid = ImageGrid(fig, 111, # as in plt.subplot(111)
nrows_ncols=(1,3),
axes_pad=0.15,
share_all=True,
cbar_location="right",
cbar_mode="single",
cbar_size="7%",
cbar_pad=0.15,
)
data = np.random.normal(size=(3,10,10))
vmax = np.amax(np.abs(data))
logthresh=4
logstep=1
linscale=1
maxlog=int(np.ceil(np.log10(vmax)))
#generate logarithmic ticks
tick_locations=([-(10**x) for x in xrange(-logthresh, maxlog+1, logstep)][::-1]
+[0.0]
+[(10**x) for x in xrange(-logthresh,maxlog+1, logstep)] )
# Add data to image grid
for ax, z in zip(grid,data):
print z
im = ax.imshow(z, vmin=-vmax, vmax=vmax,
norm=colors.SymLogNorm(10**-logthresh, linscale=linscale))
# Colorbar
ax.cax.colorbar(im,ticks=tick_locations, format=ticker.LogFormatter())
ax.cax.toggle_label(True)
#plt.tight_layout() # Works, but may still require rect paramater to keep colorbar labels visible
plt.show()