Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm using matplotlib to plot log-normalized images but I would like the original raw image data to be represented in the colorbar rather than the [0-1] interval. I get the feeling there's a more matplotlib'y way of doing this by using some sort of normalization object and not transforming the data beforehand... in any case, there could be negative values in the raw image.

import matplotlib.pyplot as plt import numpy as np  def log_transform(im):     '''returns log(image) scaled to the interval [0,1]'''     try:         (min, max) = (im[im > 0].min(), im.max())         if (max > min) and (max > 0):             return (np.log(im.clip(min, max)) - np.log(min)) / (np.log(max) - np.log(min))     except:         pass     return im  a = np.ones((100,100)) for i in range(100): a[i] = i f = plt.figure() ax = f.add_subplot(111) res = ax.imshow(log_transform(a)) # the colorbar drawn shows [0-1], but I want to see [0-99] cb = f.colorbar(res) 

I've tried using cb.set_array, but that didn't appear to do anything, and cb.set_clim, but that rescales the colors completely.

like image 800
Adam Fraser Avatar asked Mar 30 '10 15:03

Adam Fraser


People also ask

How do you normalize a Colorbar in Python?

norm = normi; #mpl. colors. Normalize(vmin=-80, vmax=20); plt. axis([1, 1000, -400, 400]);

How do you normalize Imshow?

Just specify vmin=0, vmax=1 . By default, imshow normalizes the data to its min and max. You can control this with either the vmin and vmax arguments or with the norm argument (if you want a non-linear scaling).

How do I change my color on PLT Imshow?

The most direct way is to just render your array to RGB using the colormap, and then change the pixels you want.

What is plot Imshow?

imshow. The matplotlib function imshow() creates an image from a 2-dimensional numpy array. The image will have one square for each element of the array. The color of each square is determined by the value of the corresponding array element and the color map used by imshow() .


1 Answers

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, cm from matplotlib.colors import LogNorm  # C = some matrix f = 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() 
like image 164
Steve Tjoa Avatar answered Sep 28 '22 20:09

Steve Tjoa