简体   繁体   English

将颜色条与矩阵图关联

[英]associate a colorbar with a matrix graph

I want a color graph which is associate with my graph matrix. 我想要一个与我的图形矩阵关联的彩色图形。 the colour graph is lined up with my matrix and each color on the color graph corresponds with a row on the matrix 颜色图与我的矩阵对齐,颜色图上的每种颜色对应于矩阵上的一行

Your questions are a bit mysterious to me, but given your other questions and the example picture you posted i assume you mean something like the plot below? 您的问题对我来说有点神秘,但是鉴于您的其他问题和您发布的示例图片,我假设您的意思是下面的情节? The x and y 'colorbars' only work if there is only 1 color present in a row or column, otherwise the 'last' is shown. x和y'colorbars'仅在行或列中只有一种颜色时起作用,否则显示'last'。

# 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)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM