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 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')
# Plot histogram.
n, bins, patches = plt.hist(data, 25, normed=1, color='green')
plt.show()
which outputs this:
Instead of the color being green
for the entire histogram, I'd like the columns to follow a color-coding given by the colormap defined in cm
and the values of the bins
. This would mean that bins closer to zero (not in height but in position) should look bluer and those closer to one redder, according to the chosen colormap RdYlBu_r
.
Since plt.histo
doesn't take a cmap
argument I don't know how to tell it to use the colormap defined in cm
.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…