how to add error bars to histogram diagram in python

jack

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

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Python histogram with points and error bars

From Dev

Python histogram with points and error bars

From Dev

How do you add error bars to Bokeh plots in python?

From Dev

Gnuplot Histogram w/ Error Bars

From Dev

Bar diagram with error bars in Scilab

From Dev

ggplot2 histogram: how do I add textual annotation onto histogram bars using ggplot2

From Dev

Add error bars to a barplot

From Dev

How to plot error bars in polar coordinates in python?

From Dev

Python gaussian fit with same color as bars of histogram

From Dev

How to pick unique colors of histogram bars in matplotlib?

From Dev

How to avoid gaps between bars of histogram in R?

From Dev

How to avoid gaps between bars of histogram in R?

From Dev

How to add label values and error bars to a JFreeChart line chart?

From Dev

How to add error bars in matplotlib for multiple groups from dataframe?

From Dev

How to add label values and error bars to a JFreeChart line chart?

From Dev

How to add colorbar to a histogram?

From Dev

Python histogram error bar

From Dev

Python, Seaborn - How to add significance bars and asterisks to boxplots

From Dev

How to add a legend to an Argand Diagram with several items in python?

From Dev

Python Pandas error bars are not plotted and how to customize the index

From Dev

Python Pandas error bars are not plotted and how to customize the index

From Dev

How to generate a word frequency histogram, where bars are ordered according to their height

From Dev

How to make the bars in histogram panel clickable - kibana ES

From Dev

How can I colour specific data bars in R Histogram

From Dev

gnuplot rowstacked histogram: how to put sum above bars

From Dev

Round bars in matplotlib histogram

From Dev

How to normalize a histogram in python?

From Dev

How to plot colorful histogram type constellation diagram in Matlab

From Dev

How to add a diagram in OO Writer

Related Related

  1. 1

    Python histogram with points and error bars

  2. 2

    Python histogram with points and error bars

  3. 3

    How do you add error bars to Bokeh plots in python?

  4. 4

    Gnuplot Histogram w/ Error Bars

  5. 5

    Bar diagram with error bars in Scilab

  6. 6

    ggplot2 histogram: how do I add textual annotation onto histogram bars using ggplot2

  7. 7

    Add error bars to a barplot

  8. 8

    How to plot error bars in polar coordinates in python?

  9. 9

    Python gaussian fit with same color as bars of histogram

  10. 10

    How to pick unique colors of histogram bars in matplotlib?

  11. 11

    How to avoid gaps between bars of histogram in R?

  12. 12

    How to avoid gaps between bars of histogram in R?

  13. 13

    How to add label values and error bars to a JFreeChart line chart?

  14. 14

    How to add error bars in matplotlib for multiple groups from dataframe?

  15. 15

    How to add label values and error bars to a JFreeChart line chart?

  16. 16

    How to add colorbar to a histogram?

  17. 17

    Python histogram error bar

  18. 18

    Python, Seaborn - How to add significance bars and asterisks to boxplots

  19. 19

    How to add a legend to an Argand Diagram with several items in python?

  20. 20

    Python Pandas error bars are not plotted and how to customize the index

  21. 21

    Python Pandas error bars are not plotted and how to customize the index

  22. 22

    How to generate a word frequency histogram, where bars are ordered according to their height

  23. 23

    How to make the bars in histogram panel clickable - kibana ES

  24. 24

    How can I colour specific data bars in R Histogram

  25. 25

    gnuplot rowstacked histogram: how to put sum above bars

  26. 26

    Round bars in matplotlib histogram

  27. 27

    How to normalize a histogram in python?

  28. 28

    How to plot colorful histogram type constellation diagram in Matlab

  29. 29

    How to add a diagram in OO Writer

HotTag

Archive