34

I would like to compare two different sets of data on the same imshow plot to make it easy to see the differences. My first instinct is to make the colors in the colormap transparent (the lower values especially) but I haven't been able to get this to work:

from matplotlib.colors import colorConverter
import matplotlib.pyplot as plt
import numpy as np

# create dummy data
zvals =  np.random.rand(100,100)*10-5
zvals2 = np.random.rand(100,100)*10-5

# generate the transparent colors
color1 = colorConverter.to_rgba('white',alpha=0.0)
color2 = colorConverter.to_rgba('black',alpha=0.8)

# make the colormaps
cmap1 = mpl.colors.LinearSegmentedColormap.from_list('my_cmap',['green','blue'],256)
cmap2 = mpl.colors.LinearSegmentedColormap.from_list('my_cmap2',[color1,color2],256)

img2 = plt.imshow(zvals,interpolation='nearest',cmap=cmap1,origin='lower')
img3 = plt.imshow(zvals2,interpolation='nearest',cmap=cmap2,origin='lower')

plt.show()

There is no error but the white and black of the second plot do not show any transparency. I also tried the colorConverter method to set the color in a normal plt.plot situation and the color also did not become transparent though the correct color was displayed.

Any additional advice on how to overlay/compare imshow plots would be much appreciated

2
  • 2
    Why don't you plot the subtraction of the two image data sets? Or the subtraction of a relevant transformation, like converting them to a decibel scale.
    – ely
    Apr 12, 2012 at 16:38
  • So far I haven't gotten very good results with this method, but I am continuing to try while hoping someone knows how to get the individual color transparency to work
    – Anake
    Apr 12, 2012 at 16:53

2 Answers 2

46

You can set the alpha argument in your imshow command.

In your example, img3 = plt.imshow(zvals2, interpolation='nearest', cmap=cmap2, origin='lower', alpha=0.6)

EDIT:

Thanks for the clarification. Here is a description of what you can do:

  • First, choose a matplotlib colormap object (in your case, for white and black, you can take the 'binary' colormap). Or create your own colormap as you did, if the colormap you want doesn't already exist.
  • Then initialize this colormap object: this will cause it to internally create an array called "_lut" which holds rgba values.
  • Then, you can fill the alpha values according to what you want to achieve (in your example, create an array from 0 to 0.8)
  • You can then use this colormap

Below is an example using your code:

from matplotlib.colors import colorConverter
import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl

# create dummy data
zvals = np.ones((100,100))# np.random.rand(100,100)*10-5
zvals2 = np.random.rand(100,100)*10-5

# generate the colors for your colormap
color1 = colorConverter.to_rgba('white')
color2 = colorConverter.to_rgba('black')

# make the colormaps
cmap1 = mpl.colors.LinearSegmentedColormap.from_list('my_cmap',['green','blue'],256)
cmap2 = mpl.colors.LinearSegmentedColormap.from_list('my_cmap2',[color1,color2],256)

cmap2._init() # create the _lut array, with rgba values

# create your alpha array and fill the colormap with them.
# here it is progressive, but you can create whathever you want
alphas = np.linspace(0, 0.8, cmap2.N+3)
cmap2._lut[:,-1] = alphas

img2 = plt.imshow(zvals, interpolation='nearest', cmap=cmap1, origin='lower')
img3 = plt.imshow(zvals2, interpolation='nearest', cmap=cmap2, origin='lower')

plt.show()

image

4
  • 2
    Sorry I wasn't clear, I would specifically like to have a very low alpha value for the values closer to 0 so that the background doesn't cover the color of the plot below. Have edited accordingly
    – Anake
    Apr 12, 2012 at 16:34
  • +1 for edit addition. It seems that LinearSegmentedColormap, etc don't work for alpha values, and yours is a good work-around.
    – tom10
    Apr 12, 2012 at 18:08
  • Thanks, an inspired work around. Still not exactly what I want as I don't want there to be any white, just increasing amounts of black but that doesn't seem to be possible
    – Anake
    Apr 15, 2012 at 19:00
  • 1
    @Anake if you just want increasing amounts of black (by progressive increasing of alphas), just use the same code, but change the color1 line to be color1 = colorConverter.to_rgba('black') (similar than color2).
    – gcalmettes
    Apr 15, 2012 at 21:07
4

If you make customized cmap, it can easily solve the over-ray problem

cmap1 = matplotlib.colors.ListedColormap(['none', 'green'])
cmap2 = matplotlib.colors.ListedColormap(['none', 'red'])


img2 = plt.imshow(~~~ cmap=cmap1 ~~~)
img3 = plt.imshow(~~~ cmap=cmap2 ~~~)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.