gpt4 book ai didi

python - 使图像空白透明,覆盖到 imshow()

转载 作者:行者123 更新时间:2023-11-28 20:47:44 29 4
gpt4 key购买 nike

我有一个用 imshow() 显示的空间数据图。

我需要能够叠加产生数据的晶格。我有一个png作为黑白图像加载的格子文件。我想要的图像部分overlay 是格子的黑线,看不到线之间的白色背景。

我想我需要将每个背景(白色)像素的 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...?

enter image description here

最佳答案

没有你的数据,我无法测试这个,但是类似

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)

或者,您可以将 RBGA 值的 NxMx4 np.array 交给 imshow,这样您就不必处理颜色图

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

imshow(im2)

关于python - 使图像空白透明,覆盖到 imshow(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18159874/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com