how to add error bars to histogram diagram in python

这一生的挚爱 提交于 2021-02-08 05:33:42

问题


Hi I want to add error bars to the histogram within this code.I have seen few post about it but I didn't find them helpful.this code produce random numbers with Gaussian distribution and a kernel estimation apply to it.I need to have errorbars to estimate how much the histogram is inaccurate with changing the bandwidth

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()

回答1:


Combining the answer mentioned above with your code:

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()

And have a look at the imports, as mentioned by MaxNoe




回答2:


You can do it like this:

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()

Result:




回答3:


This looks like a duplicate: Matplotlib histogram with errorbars

i.e. you have to use matplotlib.bar() to get error bars

Which in your example will look something like this: You can replace

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

with

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)

Play around with the parameters until you find something you like :)



来源:https://stackoverflow.com/questions/35390276/how-to-add-error-bars-to-histogram-diagram-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!