将颜色条与矩阵图相关联 [英] associate a colorbar with a matrix graph

查看:54
本文介绍了将颜色条与矩阵图相关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

您的问题对我来说有点神秘,但鉴于您的其他问题和您发布的示例图片,我认为您的意思类似于下面的情节?x 和 y 的颜色条"仅在行或列中只有 1 种颜色时才有效,否则将显示最后一个".

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.

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

这篇关于将颜色条与矩阵图相关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆