簡體   English   中英

如何填充matplotlib直方圖的中心95%置信區間?

[英]How to I fill the central 95% confidence interval of a matplotlib histogram?

我可以使matplotlib直方圖沒問題。 但是,我想知道是否可以使用諸如fillbetween類的fillbetween來更改數據中心95%CI的填充顏色。

我只能得到fillbetween如果我用一招用numpy的直方圖和bincenters時工作。 即:

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

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM