Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rotation of colorbar tick labels in matplotlib

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.set_xticklabels and cbar.ax.ticklabel_format and so on with rotation='vertical' but haven't quite landed it yet.

I've provided a MWE below:

import numpy as np
import matplotlib.pyplot as plt

#example function
x,y = np.meshgrid(np.linspace(-10,10,200),np.linspace(-10,10,200))
z = x*y*np.exp(-(x+y)**2)

#array for contourf levels
clevs = np.linspace(z.min(),z.max(),50)

#array for colorbar tick labels
clevs1 =np.arange(-200,100,10)

cs1 = plt.contourf(x,y,z,clevs)

cbar = plt.colorbar(cs1, orientation="horizontal")
cbar.set_ticks(clevs1[::1])

plt.show()

Example Image

Any pointers would be greatly appreciated - I'm sure this must be pretty simple...

like image 880
Kieran Hunt Avatar asked Aug 17 '15 12:08

Kieran Hunt


1 Answers

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')
like image 91
Åsmund Avatar answered Oct 28 '22 01:10

Åsmund