开发者

Vertical xtick labels on top, not bottom

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


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


Reading your post, and trying by myself, I found one very simple way of setting the ticks on top on the axes: pylab.gca().tick_top()

Cheers!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜