垂直xtick标签在顶部,而不是底部 [英] Vertical xtick labels on top, not bottom

查看:56
本文介绍了垂直xtick标签在顶部,而不是底部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 Pylab 绘制混淆矩阵.沿水平轴的类标签很长,因此我想绘制垂直旋转的类标签.但是,我也想将它们绘制在轴的顶部,而不是下方.

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

您能建议一种在垂直旋转的轴上绘制带有xticks的混淆矩阵的方法吗?谢谢.

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

编辑

谢谢你,马克,你的帮助.通过更仔细地检查刻度属性,它使我走上了正确的轨道.您的答案和我想要的答案的唯一区别是将该想法应用于AxesImage,而不是情节.经过调查,这是答案:

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

对于那些正在阅读的人...不要忘记 show()!我忘了我需要刷新数字.请参阅下面的输出.

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

具有垂直标签的混淆矩阵.http://up.stevetjoa.com/rotate_ticklabels.png

推荐答案

如果我理解正确,这会让你很接近.您可能需要用空格填充"标签以将它们移出 xaxis 线.

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

根据评论进行编辑

如果希望它们在顶部x轴上垂直旋转,请尝试以下操作:

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

这篇关于垂直xtick标签在顶部,而不是底部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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