5

I want to plot in log scale, which I have done using:

plt.contourf(cube[0,:,:].data, levels=np.arange(0,6000,10), norm=mplc.LogNorm())

But then I want the colorbar to still be a linear scale...is this possible, and if so how do I do this? The norm keyword doesn't seem to be valid within the plt.colorbar() command.

Saullo G. P. Castro
  • 56,802
  • 26
  • 179
  • 234
Rachel
  • 51
  • 1
  • 2
  • I'm sure there's more direct method, but a hack would be to make a second, unseen image, without log scaling, and connect the colorbar to that. – mdurant Oct 15 '14 at 18:08
  • 1
    Do you mean like this http://stackoverflow.com/questions/18191867/matplotlib-contour-plot-proportional-colorbar-levels-in-logarithmic-scale ? – Deditos Oct 16 '14 at 19:45

1 Answers1

2

See this answer on how to generate a colormap without an image. A working example:

import matplotlib.pyplot as plt
import matplotlib.colors as mplc
import numpy as np
data = np.random.random((10,10))*6e3
my_cmap = plt.cm.coolwarm

# contour plot
plt.contourf(data, levels=np.arange(0,6000,10), cmap=my_cmap, norm=mplc.LogNorm())

# colorbar
sm = plt.cm.ScalarMappable(cmap=my_cmap, norm=plt.Normalize(vmin=0, vmax=6000))
sm._A = []
plt.colorbar(sm)

Log contourf linear colorbar

Community
  • 1
  • 1
j08lue
  • 1,647
  • 2
  • 21
  • 37
  • 1
    This solution looks right at first glance, but there is a big problem: The tics on the colour bar do not correspond with the values in the image anymore. I compared what this technique does to my data and I now have two plots of the same data. One plotted with the logarythmic colormap and one with linear colour map. The pixels in the plots have different colours, but the color bars are identical. This means the numbers on the color bar don't show the actual value that color represents in the plot. – Ente Fetz Aug 18 '21 at 19:14