Overlaying contour colorbar over pcolormesh

  matplotlib, python

I have the following python code:

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 10, 100)
y = np.linspace(0, 10, 100)
X, Y = np.meshgrid(x, y, indexing='ij')
Z = np.sin(X+Y)

plt.pcolormesh(X, Y, Z)
plt.colorbar()
plt.contour(X, Y, Z, levels=[0.5, 0.75], colors=['black','cyan'])

plt.show()

Which gives the following output:

The wrong output -- no contour marks on the colorbar

I would like to place the contour marks on the colorbar, like this:

The more correct output -- contour marks on the colorbar

I have experimented and read up but I can’t see a way to effectively plot two quantities on the same colorbar.

Source: Python Questions

LEAVE A COMMENT