I am trying to get a colorbar that contains the values of the imshow and has the 3 lines of the contours overplotted. One of the matplotlib examples shows something that is close, but they only have colored contours. This image has a colorbar of the type that I would like though.
Here is my code and image, and it just ignores the colorbar of the cmap of the imshow.
plt.imshow(Bho, origin='l')
plt.contour(Bho, [300,400,500],origin='lower', colors=['white', 'yellow', 'red'])
plt.colorbar()
plt.show()
The thing you are missing is that you need to pass the returned object from imshow
to your colorbar as well. I prepared a minimum working example that demonstrates how to get the image values and the defined levels in the colorbar.
import numpy as np
import matplotlib.pyplot as plt
Bho = np.random.random(size=10000).reshape(100,100)
fig, ax = plt.subplots()
im = ax.imshow(Bho, origin='l')
_cs2 = ax.contour(Bho, levels=[0.2,0.4] ,origin='lower', colors=['white','red'])
cbar = fig.colorbar(im, ax=ax)
cbar.add_lines(_cs2)
plt.show()
Result