Rotation of colorbar tick labels in matplotlib

前端 未结 2 585
暖寄归人
暖寄归人 2021-01-19 01:23

I would like to rotate the colorbar tick labels so that they read vertically rather than horizontally. I have tried as many variations as I can think of with cbar.ax.s

相关标签:
2条回答
  • 2021-01-19 01:49

    If you're happy with tick locations and labels and only want to rotate them:

    cbar.ax.set_xticklabels(cbar.ax.get_xticklabels(), rotation='vertical')
    
    0 讨论(0)
  • 2021-01-19 02:03

    You can use cbar.ax.set_xticklabels to change the rotation (or set_yicklabels if you had a vertical colorbar).

    cbar.ax.set_xticklabels(clevs1[::1],rotation=90)
    

    EDIT:

    To set the ticks correctly, you can search for where in your clevs1 array the first tick should be using np.argmin, and use that to index clevs1 when you set_xticklabels:

    tick_start = np.argmin(abs(clevs1-clevs[0]))
    cbar.ax.set_xticklabels(clevs1[tick_start:],rotation=90)
    

    0 讨论(0)
提交回复
热议问题