2013-02-15 72 views

回答

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) 

enter image description here