创建彩色概率分布

问题描述 投票:0回答:1

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

Desired colouring

python graph probability
1个回答
1
投票

如果你有bin值,那么你可以使用colormap来生成条形图的颜色。

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()). 它将一个colormap应用到高度值上,高度值的范围应该为 (0, 1)因此,我们用以下方法将二进制值标准化 height.max().

example

© www.soinside.com 2019 - 2024. All rights reserved.