Vertical xtick labels on top, not bottom

后端 未结 2 861
温柔的废话
温柔的废话 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:05

    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 讨论(0)
  • 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')
    
    0 讨论(0)
提交回复
热议问题