Make a transparent image transparent, overlay on imshow ()

I have a spatial data graph that I show with imshow ().

I need to be able to overlay the crystal lattice that created the data. I have a lattice png file that loads as a black and white image. Parts of this image I want to overlay - black lines that are a grid and do not see a white background between the lines.

I think I need to set the alpha for each background (white) to transparent (0?).

I am so new to this that I really don't know how to ask this question.

EDIT:

import matplotlib.pyplot as plt import numpy as np lattice = plt.imread('path') im = plt.imshow(data[0,:,:],vmin=v_min,vmax=v_max,extent=(0,32,0,32),interpolation='nearest',cmap='jet') im2 = plt.imshow(lattice,extent=(0,32,0,32),cmap='gray') #thinking of making a mask for the white background mask = np.ma.masked_where( lattice < 1,lattice ) #confusion here b/c even tho theimage is gray scale in8, 0-255, the numpy array lattice 0-1.0 floats...? 

enter image description here

+2
source share
2 answers

Without your data, I cannot verify this, but something like

 import matplotlib.pyplot as plt import numpy as np import copy my_cmap = copy.copy(plt.cm.get_cmap('gray')) # get a copy of the gray color map my_cmap.set_bad(alpha=0) # set how the colormap handles 'bad' values lattice = plt.imread('path') im = plt.imshow(data[0,:,:],vmin=v_min,vmax=v_max,extent=(0,32,0,32),interpolation='nearest',cmap='jet') lattice[lattice< thresh] = np.nan # insert 'bad' values into your lattice (the white) im2 = plt.imshow(lattice,extent=(0,32,0,32),cmap=my_cmap) 

Alternatively, you can pass imshow np.array np.array values ​​so you don't have to guess with the color map

 im2 = np.zeros(lattice.shape + (4,)) im2[:, :, 3] = lattice # assuming lattice is already a bool array imshow(im2) 
+6
source

A simple way is to simply use the image as a background, not an overlay. In addition, you will need to use PIL or Python Image Magic to convert the selected color to transparent.

Remember that you will probably also need to resize your plot or image to fit the size.

Update:

If you run the tutorial here with your image, and then build the data on it, you should get what you need, note that the tutorial uses PIL, so you also need to install.

0
source

Source: https://habr.com/ru/post/969347/


All Articles