python - Custom coloration for matrix in matplotlib -
i have 3 matrices i'd plot, solution i've come plotting 1 after other, , leaves me last matrix plotted.
ax.imshow(mat1, cmap='blues', interpolation='nearest') ax.imshow(mat2, cmap='binary', interpolation='nearest') ax.imshow(mat3, cmap='autumn', interpolation='nearest') # actual plot
what want display 0s in 3 matrices in white, , higher values in different tones depending on matrix, e.g.: blue, black , red. also, in example, red cells have precedence on black , black on blue. solution i'm imagining function that, given triple (blue, black, red) different values each component, returns color cell should colored, , feed colormap, don't know how or if it's possible.
every kind of , different solutions (that's happen) welcome , appreciated. in advance.
you want fourth image, rgb value @ each point function of single value of first 3 matrixes @ corresponding point? if so, can produce algebra 3 values rgb fourth?
your question suggests confusion how plotting turns data colors. colormap takes single-valued data, normalizes it, , maps named array of colors. 0 values might mapped color, depending on colormap , rest of data.
a bitmap defines (red, green, blue) values @ each pixel. proper bitmaps have header sections, data (m,n,3) array. imshow
plots array; expects rgb values in [0,1] range.
if have 3 data matrices, have choose how map values rgb values. here's example 3 kinds of mapping rgb. first 2 rows dummy data range of values, shown either colormap or simplest rgb representation. last row shows ways of combining 3 dummy matrices 1 image using whole colorspace.
# -*- coding: utf-8 -*- import matplotlib.pyplot plt import numpy np #dummy data x = 8 y = 15 mat = [] mat.append(np.arange(x * y).reshape((x, y)) / float(x * y) ) mat.append(np.arange(x * y).reshape((y, x)).t / float(x* y)) mat.append(np.arange(y) * np.arange(x)[:,np.newaxis] / float(99)) # note: data approximately in rgb range. if yours isn't, normalize, # here or in makergb function. # (the colormap normalizes single-valued data). fig, axs = plt.subplots(figsize=(7,4), nrows=3, ncols=3, gridspec_kw={'hspace':0.6}) axs[0,0].imshow(mat[0], cmap='reds', interpolation='nearest') axs[0,1].imshow(mat[1], cmap='greens', interpolation='nearest') axs[0,2].imshow(mat[2], cmap='blues', interpolation='nearest') axs[0,0].set_xlabel('reds colormap') axs[0,1].set_xlabel('greens colormap') axs[0,2].set_xlabel('blues colormap') def asonehue(mat, hue): """ use single-valued matrix represent 1 hue in rgb file.' """ rgbout = np.zeros((len(mat),len(mat[0]),3)) rgbout[:,:,i] = mat return rgbout in (0,1,2): axs[1,i].imshow(asonehue(mat[i],i)) axs[1,0].set_xlabel('reds bitmap') axs[1,1].set_xlabel('greens bitmap') axs[1,2].set_xlabel('blues bitmap') # different ways combine 3 values def makergb0(mats): rgbout = np.zeros((len(mats[0]),len(mats[0][0]),3)) #rgbout = np.ones((len(mats[0]),len(mats[0][0]),3)) in (0,1,2): rgbout[:,:,i] = mats[i] return rgbout axs[2,0].imshow(makergb0(mat)) axs[2,0].set_xlabel('color layers') def makergb1(mats): rgbout = np.zeros((len(mats[0]),len(mats[0][0]),3)) i,j,k = rgbout.shape x in range(i): y in range(j): rgbout[x,y] = (mats[0][x][y] / 2, mats[1][x][y], 1 - mats[2][x][y]) return rgbout axs[2,1].imshow(makergb1(mat)) axs[2,1].set_xlabel('algebraic') def makergb2(mats): rgbout = np.zeros((len(mats[0]),len(mats[0][0]),3)) i,j,k = rgbout.shape x in range(i): y in range(j): if mats[0][x][y] > .8: rgbout[x,y] = (mats[0][x][y], 0, 0) elif mats[1][x][y] > .8: rgbout[x,y] = (0, mats[1][x][y], 0) else: rgbout[x,y] = (mats[0][x][y], mats[1][x][y], mats[2][x][y]) return rgbout axs[2,2].imshow(makergb2(mat)) axs[2,2].set_xlabel('if-else') plt.show()
Comments
Post a Comment