python - 创建彩色概率分布
问题描述
有谁知道如何以如下图所示的方式获得概率分布的颜色。我尝试了各种方法,但没有达到预期的结果。它可以是 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.Spectral((height.max()-height)/height.max())
. 它将颜色图应用于应该在范围内的高度值,(0, 1)
因此我们将 bin 值标准化为height.max()
。
推荐阅读
- javascript - Android 上的 Chrome 按键事件报告很有趣
- c# - 一个用于多站点的 Asp.Net Core Api 服务
- windbg - WinDbg 中的 ba 命令(Break on Access)不能像 Daniel Pravat 和 Mario Hewardt 在 Advanced Windows Debugging 一书中所宣传的那样工作
- android - 没有测试的模块中出现“未找到测试”错误
- python - 在odoo 12中使用python的方法执行cron作业
- c++ - 如何让它沿对角线方向移动?
- git - 如何启用我的 Azure 管道以使用 Git 签出子模块?
- html - 在 Linux 中使用 rmarkdown::render 通过 r 和 .rmd 创建一个 html
- java - 嵌套异常是 java.time.format.DateTimeParseException: Text '3030821'
- apache-spark - 推荐什么 - 在 spark 表中保留空列表/数组与 Null?