Overlay imshow plots in matplotlib

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

Leave a Comment

tech