Skip to content Skip to sidebar Skip to footer

How To Change Colour Of Certain Elements Of A Matrix In Matplotlib Matshow?

I have an adjacency matrix of a bipartite graph (of 1's and 0's) and bi-clusters (array of arrays of rows and columns) for this matrix. How do I set different colours for elements

Solution 1:

One approach might be to make a copy of your matrix and then give distinct values to the clusters you identify.

m = a_matrix.copy()# a copy we can change without altering the orignalc= cluster_1           # an alias to save typing# Naked NumPy doesn't seem to have a cartesian product, so roll our ownfor i inrange(c.shape[1]):for j inrange(c.shape[1]):if m[c[0,i]-1,c[1,j]-1]:
            m[c[0,i]-1,c[1,j]-1]=2

plt.matshow(m, cmap='jet', interpolation='nearest')
plt.show()

enter image description here

For more clusters, loop over the above, setting a distinct value for each cluster (and maybe choose or define a better colormap). I'm sure there are more efficient implementations of the cartesian product as well...

Post a Comment for "How To Change Colour Of Certain Elements Of A Matrix In Matplotlib Matshow?"