python - Combining two matplotlib colormaps -
i merge 2 colormaps one, such can use 1 cmap
negative values , other 1 positive values.
at moment masked arrays , plotting 1 image 1 cmap
, other image other, resulting in:
with following data
dat = np.random.rand(10,10) * 2 - 1 pos = np.ma.masked_array(dat, dat<0) neg = np.ma.masked_array(dat, dat>=0)
i plotted pos
gist_heat_r
, neg
binary
.
i have single colorbar combined cmap
's, not correct approach me.
so, how take 2 existing cmaps
's , merge them one?
edit: admit, duplicate, answer that's given more clear here. example images make more clear.
colormaps interpolation functions can call. map values interval [0,1] colors. can sample colors both maps , combine them:
import numpy np import matplotlib.pyplot plt import matplotlib.colors mcolors data = np.random.rand(10,10) * 2 - 1 # sample colormaps want use. use 128 each 256 # colors in total colors1 = plt.cm.binary(np.linspace(0., 1, 128)) colors2 = plt.cm.gist_heat_r(np.linspace(0, 1, 128)) # combine them , build new colormap colors = np.vstack((colors1, colors2)) mymap = mcolors.linearsegmentedcolormap.from_list('my_colormap', colors) plt.pcolor(data, cmap=mymap) plt.colorbar() plt.show()
result:
note: understand might have specific needs this, in opinion not approach: how distinguish -0.1 0.9? -0.9 0.1?
one way prevent sample maps ~0.2 ~0.8 (e.g.: colors1 = plt.cm.binary(np.linspace(0.2, 0.8, 128))
) wont go way black:
Comments
Post a Comment