Would anybody know how to get the probability distribution coloured in a way as displayed in the image below. I have tried various ways, however without the desired result. It could be either R or Python, because I have tried in both.
If you have the bin values then you can use a colormap to generate the bar colors:
from scipy import stats
import numpy as np
from matplotlib import pyplot as plt
# generate a normal distribution
values = stats.norm().rvs(10000)
# calculate histogram -> get bin values and locations
height, bins = np.histogram(values, bins=50)
# bar width
width = np.ediff1d(bins)
# plot bar
# to get the desired colouring the colormap needs to be inverted and all values in range (0,1)
plt.bar(bins[:-1] + width/2, height, width*0.8,
color=plt.cm.Spectral((height.max()-height)/height.max()))
The key to the colouring is this code: plt.cm.Spectral((height.max()-height)/height.max())
. It applies a colormap to then height values, which should be in the range (0, 1)
, so we normalise the bin values by height.max()
.