I want to plot a simple 1D histogram where the bars should follow the color-coding of a given colormap.
Here\'s an MWE:
import numpy as
An alternative approach is to use plt.bar which takes in a list of colors. To determine the widths and heights you can use numpy.histogram. Your colormap can be used by finding the range of the x-values and scaling them from 0 to 1.
import numpy as n
import matplotlib.pyplot as plt
# Random gaussian data.
Ntotal = 1000
data = 0.05 * n.random.randn(Ntotal) + 0.5
# This is the colormap I'd like to use.
cm = plt.cm.get_cmap('RdYlBu_r')
# Get the histogramp
Y,X = n.histogram(data, 25, normed=1)
x_span = X.max()-X.min()
C = [cm(((x-X.min())/x_span)) for x in X]
plt.bar(X[:-1],Y,color=C,width=X[1]-X[0])
plt.show()
