How can I draw a log-normalized imshow plot with a colorbar representing the raw data in matplotlib How can I draw a log-normalized imshow plot with a colorbar representing the raw data in matplotlib python python

How can I draw a log-normalized imshow plot with a colorbar representing the raw data in matplotlib


Yes, there is! Use LogNorm. Here is a code excerpt from a utility that I wrote to display confusion matrices on a log scale.

from pylab import figure, cmfrom matplotlib.colors import LogNorm# C = some matrixf = figure(figsize=(6.2,5.6))ax = f.add_axes([0.17, 0.02, 0.72, 0.79])axcolor = f.add_axes([0.90, 0.02, 0.03, 0.79])im = ax.matshow(C, cmap=cm.gray_r, norm=LogNorm(vmin=0.01, vmax=1))t = [0.01, 0.1, 0.2, 0.4, 0.6, 0.8, 1.0]f.colorbar(im, cax=axcolor, ticks=t, format='$%.2f$')f.show()


If you just want the image to be log-normalized (to enhance details), but not the data (to preserve physical values), then you have to apply the transformation on the colormap itself. You can do that with the function cmap_map() given in the cookbook:https://scipy-cookbook.readthedocs.io/items/Matplotlib_ColormapTransformations.html