有谁知道如何将概率分布着色,如下图所示。我尝试了各种方法,但都没有得到想要的结果。它可以是R或Python,因为我已经尝试过这两种方法。

发布于 2020-05-11 18:34:10
如果您有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.Spectral((height.max()-height)/height.max())。它将色彩映射表应用于高度值,高度值应该在(0, 1)的范围内,因此我们通过height.max()对bin值进行归一化。

https://stackoverflow.com/questions/61727389
复制相似问题