You need to specify the color of the faces from some form of colormap, for example if you want 20 bins and a spectral
colormap,
nbins = 20
colors = plt.cm.spectral(np.linspace(nbins))
You can then use this to specify the color of the bars, which is probably easiest to do by getting histogram data first (using numpy
) and plotting a bar chart. You can then add the colorbar to a seperate axis at the bottom.
As a minimal example,
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
nbins = 20
minbin = 0.
maxbin = 1.
data = np.random.normal(size=10000)
bins = np.linspace(minbin,maxbin,20)
cmap = plt.cm.spectral
norm = mpl.colors.Normalize(vmin=data.min(), vmax=data.max())
colors = cmap(bins)
hist, bin_edges = np.histogram(data, bins)
fig = plt.figure()
ax = fig.add_axes([0.05, 0.2, 0.9, 0.7])
ax1 = fig.add_axes([0.05, 0.05, 0.9, 0.1])
cb1 = mpl.colorbar.ColorbarBase(ax1, cmap=cmap,
norm=norm,
orientation='horizontal')
ax.bar(bin_edges[:-1], hist, width=0.051, color=colors, alpha=0.8)
ax.set_xlim((0., 1.))
plt.show()
Which yields,
