Way to show colorbar() without calling imshow() or scatter()

Julius Wang
Data Science Canvas
1 min readMar 30, 2016

Thanks to the info provided here and here, I gonna note it down here for plotting the colorbar in Python Matplotlib without using imshow() or scatter() to make a fake drawing first and clean it off later.

import matplotlib.pyplot as plt
from matplotlib import cm
import numpy as np
y = np.array([1, 4, 3, 2, 7, 11])
colors = plt.cm.hsv(y / float(max(y)))
sm = plt.cm.ScalarMappable(cmap=plt.cm.hsv, norm=plt.Normalize(vmin=0, vmax=1))
# fake up the array of the scalar mappable. Urgh…
sm._A = []
plt.colorbar(sm)
plt.bar(range(len(y)), y, color = colors)
plt.show()

Here’s what I get

--

--