3

I use the following program to read an image file using matplotlib and plotting it. However, I do not want the colour scale to be in linear scale, but want to plot it in logarithmic scale. Can anyone please help me in solving this.

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np



tiff_file = mpimg.imread('D:/image/data_001.tif')
lum_img = tiff_file[:,:,0]
imarray = np.array(tiff_file)
imgplot = plt.imshow(lum_img, cmap='spectral')
plt.colorbar()

Thanks.

Karthick
  • 58
  • 4
  • 1
    This may help: [A logarithmic colorbar in matplotlib scatter plot](http://stackoverflow.com/q/17201172/1730674), which says to use `plt.imshow(..., norm=matplotlib.colors.LogNorm())` – askewchan Aug 31 '15 at 19:36
  • The reason, I need to have this in logarithmic scale is that the tiff image contains information about x-ray intensity and in linear scale, I hardly see anything. So, I want the information in the tiff data to be displayed in logarithmic scale. – Karthick Sep 01 '15 at 07:10
  • Askewchan: Thanks for your answer. I tried that command plt.scatter(x,y,edgecolors='none',s=marker_size,c=np.log(void_fraction)) Here the error for me is x and y. They are not assigned and since it is an array file, I am not sure which one is to be assigned as x and y. – Karthick Sep 01 '15 at 07:13
  • Keep your call to `imshow` the same as you have it in the question. Only add the `norm=...` argument I showed in my comment above. – askewchan Sep 01 '15 at 13:20
  • 1
    In full, it would be: `plt.imshow(lum_img, cmap='spectral', norm=matplotlib.colors.LogNorm())` – askewchan Sep 01 '15 at 13:25
  • let me know if it doesn't work. I marked this question as a duplicate, so that anybody who finds your question can find the answer there, but I'm still happy to help you if you need it :) just use `@askewchan` to get my attention. – askewchan Sep 01 '15 at 14:15
  • @askewchan : Thanks a lot for the help. It helped in changing the scale of the colorbar. I think there is some other fundamental mistake i am doing. The colorbar takes my x-ray intensity values from 0 to 255 instead of the actual information that is stored in it. I will try to find the mistake i am making and get back to you. Once again, thanks a lot for your help. – Karthick Sep 01 '15 at 14:49
  • Maybe `data_001.tif` only has values from 0 to 255? Where is the actual x-ray intensity value stored? – askewchan Sep 01 '15 at 15:13
  • @askewchan : Thanks. You were right. Somehow the tiff image has values from 0 to 255. So, I had to open the tiff file using imageJ and then save it as a txt image file. The .txt file was then imported and plotted. This gave the real values from 0 to few million counts. Once again thanks for your help. – Karthick Sep 02 '15 at 12:27
  • 1
    Oh, your tiffs are not 8-bit, but `matplotlib.image.imread` is probably assuming so (and saving as a `dtype='uint8'` array. You don't need to use ImageJ, just find a better image reader, like `PIL` or [`skimage.io.imread`](http://scikit-image.org/docs/stable/api/skimage.io.html#imread), for example. – askewchan Sep 02 '15 at 14:27
  • 1
    @askewchan : PIL works very well and I do not have to convert it from ImageJ. However, for plotting with colormap map and logarithmic scale normalization, I have to use plt. Anyhow everything works fine now. Thanks a lot – Karthick Sep 03 '15 at 15:29

0 Answers0