Vertical xtick labels on top, not bottom

后端 未结 2 863
温柔的废话
温柔的废话 2021-02-12 09:24

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

2条回答
  •  遥遥无期
    2021-02-12 10:10

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

提交回复
热议问题