Python 创建彩色概率分布

Python 创建彩色概率分布,python,graph,probability,Python,Graph,Probability,有人知道如何以下图所示的方式将概率分布着色吗。我尝试了各种方法,但是没有达到预期的效果。它可以是R或Python,因为我已经在这两种语言中进行了尝试 如果您有bin值,则可以使用颜色映射生成条形图颜色: from scipy import stats import numpy as np from matplotlib import pyplot as plt # generate a normal distribution values = stats.norm().rvs(10000)

有人知道如何以下图所示的方式将概率分布着色吗。我尝试了各种方法,但是没有达到预期的效果。它可以是R或Python,因为我已经在这两种语言中进行了尝试


如果您有bin值,则可以使用颜色映射生成条形图颜色:

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()))
着色的关键是这个代码:
plt.cm.spectrum((height.max()-height)/height.max())
。它将颜色映射应用于高度值,高度值应在
(0,1)
范围内,因此我们通过
height.max()
将bin值标准化