Skip to content

Instantly share code, notes, and snippets.

@bryan-lunt
Created September 12, 2012 20:07
Show Gist options
  • Save bryan-lunt/3709518 to your computer and use it in GitHub Desktop.
Save bryan-lunt/3709518 to your computer and use it in GitHub Desktop.
Display A Matrix in Pylab, with custom tics.
'''
Created on Jun 1, 2012
@author: lunt
SOME CODE TAKEN FROM: http://stackoverflow.com/questions/3529666/matplotlib-matshow-labels
'''
import pylab
import scipy as S
def visualize_matrix(myMatrix,title,xlabels,ylabels,cmap=None):
fig = pylab.figure()
ax = fig.add_subplot(111)
ax.set_title(title)
M,N = myMatrix.shape
cax = ax.matshow(myMatrix,cmap=cmap)
ax.set_xticks(S.arange(0,float(N),1.0))
ax.set_yticks(S.arange(0,float(M),1.0))
xticks = list()
xticks.extend(xlabels)
ax.set_xticklabels(xticks)
yticks = list()
yticks.extend(ylabels)
ax.set_yticklabels(yticks)
fig.colorbar(cax)
return fig
if __name__ == "__main__":
import scipy as S
mat1 = S.rand(4,8)
ylabels = ["this","was","a","test"]
xlabels = ['A','B','C','D','E','F','G','H']
fig = visualize_matrix(mat1, "this is a test!",xlabels,ylabels)
pylab.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment