我有一个用imshow()显示的空间数据图.
我需要能够覆盖产生数据的晶格.我有一个格子的png文件加载为黑白图像.我要叠加的这个图像的部分是黑色线条是格子,而不是看到线条之间的白色背景.
我想我需要将每个背景(白色)像素的alpha设置为透明(0?).
我是新来的,我真的不知道怎么问这个问题.
编辑:
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...?
Run Code Online (Sandbox Code Playgroud)

没有你的数据,我无法测试这个,但有点像
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)
Run Code Online (Sandbox Code Playgroud)
或者,您可以imshow提供NxMx4 np.array的RBGA值,这样您就不必使用颜色贴图
im2 = np.zeros(lattice.shape + (4,))
im2[:, :, 3] = lattice # assuming lattice is already a bool array
imshow(im2)
Run Code Online (Sandbox Code Playgroud)