How do you control the colorbar after it is created automatically with the iris.quickplot
module?
From matplotlib v1.3 onwards, the return value from contouring/pcolormeshing should have a "colorbar" attribute, which allows us to get hold of the colorbar artist created for that contour/mesh (aka. a "ScalarMappable").
Let's get some data to try it out with:
import iris
temp = iris.load_cube(iris.sample_data_path('air_temp.pp'))
print temp.summary(shorten=True)
air_temperature / (K) (latitude: 73; longitude: 96)
Let's look at the default quickplot:
import iris.quickplot as qplt
import matplotlib.pyplot as plt
plt.figure(figsize=(10, 10))
qplt.contourf(temp)
plt.gca().coastlines()
plt.show()
So, supposing we wanted to control some aspect of the colorbar, we currently don't have a reference to the colorbar Artist, but we can get it by storing the result of contourf and accessing its colorbar
attribute:
cs = qplt.contourf(temp)
cbar = cs.colorbar
For instance, let's change the ticks of the colorbar:
import iris.quickplot as qplt
import matplotlib.pyplot as plt
plt.figure(figsize=(11, 11))
cs = qplt.contourf(temp)
cbar = cs.colorbar
cbar.set_ticks([250, 270, 290])
plt.gca().coastlines()
plt.show()