Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vertical xtick labels on top, not bottom

Tags:

I want to plot a confusion matrix using Pylab. The class labels along the horizontal axis are long, so I want to plot them rotated vertically. However, I also want to plot them on top of the axis, not below.

This command can plot vertical labels on bottom:

pylab.imshow(confusion_matrix) pylab.xticks(..., rotation='vertical') 

and this command can plot horizontal labels on top without rotation:

pylab.matshow(confusion_matrix) 

but I cannot find anything that does both. The following command does not work.

pylab.matshow(confusion_matrix) pylab.xticks(..., rotation='vertical') 

Can you suggest a way to plot a confusion matrix with xticks on top of the axis with vertical rotation? Thank you.

EDIT

Thank you, Mark, for your help. It got me on the right track by inspecting the tick properties more closely. The only difference with your answer and my desired answer is applying that idea to an AxesImage, not a plot. After investigation, here is the answer:

im = pylab.matshow(confusion_matrix) for label in im.axes.xaxis.get_ticklabels():     label.set_rotation(90) im.figure.show() 

To those reading... don't forget about show()! I forgot that I needed to refresh the figure. See output below.

Confusion matrix with vertical labels. http://up.stevetjoa.com/rotate_ticklabels.png

like image 706
Steve Tjoa Avatar asked Jan 02 '10 19:01

Steve Tjoa


1 Answers

If I understand you correctly, this will get you close. You might have to 'pad' your labels out with spaces to move them off the xaxis line.

from matplotlib import pylab  pylab.plot([0, 6], [0, 6]) pylab.xticks([1,2,3,4,5,6],('one','two','three','four','five','six'),rotation='vertical',verticalalignment='bottom') 

EDIT IN RESPONSE TO COMMENT

If you want them rotated vertical on the top x-axis, try this:

pylab.plot([0, 6], [0, 6]) pylab.xticks([1,2,3,4,5,6],('one','two','three','four','five','six')) for tick in pylab.gca().xaxis.iter_ticks():     tick[0].label2On = True     tick[0].label1On = False     tick[0].label2.set_rotation('vertical') 
like image 92
Mark Avatar answered Oct 27 '22 16:10

Mark