我想要一个与我的图表矩阵相关联的颜色图表。 颜色图表与我的矩阵对齐,颜色图表上的每种颜色对应矩阵上的一行
答案 0 :(得分:0)
你的问题对我来说有点神秘,但鉴于你的其他问题和你发布的示例图片,我认为你的意思是下面的情节? x和y'颜色栏'仅在行或列中只有1种颜色时才起作用,否则显示“最后”。
# inital settings
size = 50 # size of the maxtrix = size*size
n = 20 # number of points in the matrix
vmin=0 # min value for the colorcoding
vmax=3 # max value for the colorcoding
#generate the data and colormap
data = np.vstack((np.random.randint(0,size,n),np.random.randint(0,size,n))).T
param = np.random.randint(vmin,vmax+1,n)
# 0-red, 1-blue, 2-green and 3-yellow
cmap = mpl.colors.ListedColormap([[1,0,0], [0,0,1], [0,1,0], [1,1,0]])
# create the n*x matrix and x/y 'colorbars'
mtrx = np.zeros((size,size))-1
xcolors = np.zeros(size)-1
ycolors = np.zeros(size)-1
# map the data to the n*n matrix and x/y 'colorbars'
for n, item in enumerate(data):
x, y = item
xcolors[x] = param[n]
ycolors[y] = param[n]
mtrx[x,y] = 1 # relace 1 with param [n] to color the matrix points as well
# mask all 'empty' matrix entries
mtrx = np.ma.masked_values(mtrx,-1)
xcolors = np.ma.masked_values(xcolors.reshape((xcolors.size,1)),-1)
ycolors = np.ma.masked_values(ycolors.reshape((1,xcolors.size)),-1)
fig = plt.figure(figsize=(6,6))
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])
x_ax = fig.add_axes([0.05, 0.1, 0.05, 0.8])
y_ax = fig.add_axes([0.1, 0.05, 0.8, 0.05])
ax.imshow(mtrx, cmap=plt.cm.Greys_r, interpolation='none')
ax.set_title('My matrix')
x_ax.imshow(xcolors, cmap=cmap, interpolation='none',vmin=vmin, vmax=vmax)
y_ax.imshow(ycolors, cmap=cmap, interpolation='none',vmin=vmin, vmax=vmax)
for tmpax in [ax, y_ax, x_ax]:
tmpax.set_yticks([])
tmpax.set_xticks([])
tmpax.set_yticklabels([])
tmpax.set_xticklabels([])
ax.set_xticks(np.arange(-0.5,size + .5,1))
ax.set_yticks(np.arange(-0.5,size + .5,1))
ax.grid(True, color='w', linestyle='-')
plt.setp(y_ax, frame_on=False)
plt.setp(x_ax, frame_on=False)