3 回答
TA贡献1802条经验 获得超10个赞
找到解决方案了!
c_white = matplotlib.colors.colorConverter.to_rgba('white',alpha = 0)
c_black= matplotlib.colors.colorConverter.to_rgba('black',alpha = 1)
cmap_rb = matplotlib.colors.LinearSegmentedColormap.from_list('rb_cmap',[c_white,c_black],512)
pl = plt.pcolor(mp1,cmap=cmap_rb)
TA贡献1804条经验 获得超2个赞
如果mp1只有值 0 和 1,中间没有任何值,则可以屏蔽掉零并仅绘制零。np.where(mp1 == 1, 1, np.nan)只会显示那些。使用vmin=0将确保 0 映射到“二元”颜色图的白色,并将vmax=11 映射到黑色。
import matplotlib.pyplot as plt
import numpy as np
mp0 = np.random.randn(15, 30).cumsum(axis=1).cumsum(axis=0) # random backgroud
mp1 = np.zeros((15, 30)) # start with all zeros
mp1[np.random.randint(0, 15, 30), np.random.randint(0, 30, 30)] = 1 # set some random positions to 1
plt.pcolormesh(mp0, cmap='rainbow')
plt.pcolormesh(np.where(mp1 == 1, 1, np.nan), cmap='binary', vmin=0, vmax=1)
plt.tight_layout()
plt.show()
TA贡献1799条经验 获得超6个赞
定义自己的 RGBA 颜色图并不太困难(来源)。例如,要定义具有线性变化透明度的黑色贴图:
from matplotlib.colors import ListedColormap
cmap = np.zeros([256, 4])
cmap[:, 3] = np.linspace(0, 1, 256)
cmap = ListedColormap(cmap)
用法示例:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
x = 2 * np.pi * np.linspace(-1, 1, 100)
z = np.sin(x.reshape(1, -1) + x.reshape(-1, 1))
cmap = np.zeros([256, 4])
cmap[:, 3] = np.linspace(0, 1, 256)
cmap = ListedColormap(cmap)
plt.figure()
plt.pcolormesh(z + 1, cmap='bwr', edgecolors=None)
plt.pcolormesh(np.fliplr(z), cmap=cmap, edgecolors=None)
plt.savefig("temp")
添加回答
举报