python - 如何填充 matplotlib 直方图的中心 95% 置信区间?

标签 python matplotlib histogram fill

我可以毫无问题地制作 matplotlib 直方图。但是,我想知道是否可以使用 fill Between 之类的东西来更改数据中心 95% CI 的填充颜色。

当我使用带有 numpy 直方图和 bincenters 的技巧时,我只能让 fill Between 工作。即:

bins = np.linspace(-a.max(),a.max(),400)
hist = np.histogram(a,bins = bins)[0]
bincenters = 0.5*(bins[1:] + bins[:-1])

b = plt.plot(bincenters,hist, linestyle = 'None')
plt.fill_between(bincenters,hist, color = '#7f7f7f')

plt.fill_between(bincenters, hist, interpolate=False,
                where=((bincenters>=lower_p) & (bincenters<=upper_p)), hatch = '...', facecolor = '#7f7f7f')```


Here's my existing code that I'd rather use to create the matplotlib histogram (which I think looks better) with some extras plotting on top: 

#Create Histogram
axes[1] = boota.plot.hist(ax = axes[1],bins = 50, legend = None, histtype = 'bar', color = '#7f7f7f')
axes[1].set_xlabel('Spatial Decay Rate (α)', size = 16, fontweight = 'bold')
axes[1].set_ylabel('Frequency', labelpad = 11, size = 16, fontweight = 'bold')

#Ticklabels 
axes[0].tick_params(labelsize = 14)
axes[1].tick_params(labelsize = 14)

#draw vertical line at remote powerlaw (rem_a)
rem_a = 0.649
axes[1].axvline(x=rem_a, color='k', linestyle='dashed', linewidth=1.5, label='remote decay \nrate $α_r$ = 0.649')
legend = axes[1].legend(ncol = 1, loc = 'upper left', fontsize='large')
legend.draw_frame(False)

at2 = AnchoredText("B",prop=dict(size=20), loc='upper right',frameon=False)
axes[1].add_artist(at2)

最佳答案

查看fill_betweenx我认为这更适合这里

import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm

arr = np.random.normal(size=500)
ci = norm(*norm.fit(arr)).interval(0.95)  # fit a normal distribution and get 95% c.i.
height, bins, patches = plt.hist(arr, alpha=0.3)
plt.fill_betweenx([0, height.max()], ci[0], ci[1], color='g', alpha=0.1)  # Mark between 0 and the highest bar in the histogram

enter image description here

关于python - 如何填充 matplotlib 直方图的中心 95% 置信区间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57829702/

相关文章:

Python - 动画波动方程的数值解

opencv - 比较两个图像特定区域的直方图?打开简历

python - 使用字典找出用户何时取消的方法

python - Angular.js 和 Genshi - 使用 $ 发生冲突

python - 用一行代码在 Jupyter 中对单个 block 进行计时

r - ggplot2 中具有可变大小 binwidth 的直方图

python - 绘制具有恒定条宽但不同 bin 大小的直方图

python - 是否可以使用递归来编写该公式

python - 添加自定义刻度和标签

python - 为数据点添加注释