0

我想要一个与我的图形矩阵相关联的彩色图形。颜色图与我的矩阵对齐,颜色图上的每种颜色对应于矩阵上的一行

4

1 回答 1

0

你的问题对我来说有点神秘,但考虑到你的其他问题和你发布的示例图片,我假设你的意思类似于下面的情节?x 和 y “颜色条”仅在一行或一列中仅存在一种颜色时才有效,否则显示“最后一个”。

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

在此处输入图像描述

于 2013-02-15T16:29:37.143 回答