【问题标题】:how to add error bars to histogram diagram in python如何在python中将误差线添加到直方图中
【发布时间】:2016-05-25 05:40:15
【问题描述】:

您好,我想在此代码中的直方图中添加误差线。我看过一些关于它的帖子,但我没有发现它们有帮助。此代码产生具有高斯分布的随机数,并且适用于内核估计。我需要有误差条来估计直方图在改变带宽时不准确的程度

from random import * 
import numpy as np 
from matplotlib.pyplot import* 
from matplotlib import* 
import scipy.stats as stats

def hist_with_kde(data, bandwidth = 0.3):
    #set number of bins using Freedman and Diaconis
    q1 = np.percentile(data,25)
    q3 = np.percentile(data,75)


    n = len(data)**(.1/.3)
    rng = max(data) - min(data)
    iqr = 2*(q3-q1)

    bins =int((n*rng)/iqr)
    print(bins)
    x = np.linspace(min(data),max(data),200)

    kde = stats.gaussian_kde(data,'scott')

    kde._compute_covariance()
    kde.set_bandwidth()


    plot(x,kde(x),'r') # distribution function
    hist(data,bins=bins,normed=True) # histogram

data = np.random.normal(0,1,1000)
hist_with_kde(data,30)

show()

【问题讨论】:

  • 如果我执行你的代码会出错(在第 30 行:date = ...)
  • 您缺少一个“;”或第 30 行中的换行符,如下所示:data = np.random.normal(0,1,1000); hist_with_kde(data,30)
  • 我更正了感谢@MikkelBueTellus 和 bastelflp
  • 请不要到处做这些* 导入。

标签: python python-3.x numpy matplotlib scipy


【解决方案1】:

这看起来像是重复的:Matplotlib histogram with errorbars

即你必须使用 matplotlib.bar() 来获取误差线

在您的示例中将如下所示: 你可以替换

hist(data,bins=bins,normed=True)

y, binEdges = np.histogram(data,bins=bins)
bincenters = 0.5*(binEdges[1:]+binEdges[:-1])
menStd = np.sqrt(y)
width=0.1
bar(bincenters,y,width=width, color='r', yerr=menStd)

玩转参数,直到找到你喜欢的东西:)

【讨论】:

  • 我已经读过,但我不能将它应用到我的代码中。我是在 python 中绘图的初学者
  • 移至回答部分
【解决方案2】:

将上面提到的answer 与您的代码结合起来:

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


def hist_with_kde(data, bandwidth = 0.3):
    #set number of bins using Freedman and Diaconis
    q1 = np.percentile(data, 25)
    q3 = np.percentile(data, 75)

    n = len(data)**(.1/.3)
    rng = max(data) - min(data)
    iqr = 2*(q3-q1)

    bins =int((n*rng)/iqr)
    print(bins)
    x = np.linspace(min(data), max(data), 200)

    kde = stats.gaussian_kde(data, 'scott')

    kde._compute_covariance()
    kde.set_bandwidth()

    plt.plot(x, kde(x), 'r')  # distribution function

    y, binEdges = np.histogram(data, bins=bins, normed=True)
    bincenters = 0.5*(binEdges[1:]+binEdges[:-1])
    menStd = np.sqrt(y)
    width = 0.2
    plt.bar(bincenters, y, width=width, color='r', yerr=menStd)


data = np.random.normal(0, 1, 1000)
hist_with_kde(data, 30)

plt.show()

然后看看 MaxNoe 提到的导入

【讨论】:

  • 谢谢你这么快的帮助。我只有一个麻烦。为什么我在你的代码中看不到我的内核密度分布?我更喜欢同时有错误和内核分布
  • 我更新了代码并将误差线放在了Mikkel Bue Tellus 提到的 kde 函数中,但现在你有一个规范的 hist,但误差线没有规范 - 所以情节不匹配。
【解决方案3】:

你可以这样做:

import numpy as np
import matplotlib.pyplot as plt

plt.style.use('ggplot')

data = np.random.normal(size=10000)

# plt.hist gives you the entries, edges 
# and drawables we do not need the drawables:
entries, edges, _ = plt.hist(data, bins=25, range=[-5, 5])

# calculate bin centers
bin_centers = 0.5 * (edges[:-1] + edges[1:])

# draw errobars, use the sqrt error. You can use what you want there
# poissonian 1 sigma intervals would make more sense
plt.errorbar(bin_centers, entries, yerr=np.sqrt(entries), fmt='r.')

plt.show()

结果:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-25
    • 1970-01-01
    • 1970-01-01
    • 2020-12-31
    • 1970-01-01
    • 2016-01-03
    • 2016-01-04
    相关资源
    最近更新 更多