How can I make the Gaussian normal match the histogram?


Sam Sloan

I was wondering if there is a good way to match Gaussian normals in the form of numpy arrays with a histogram np.histogram(array, bins).

How can I draw such a curve on the same graph and adjust it in height and width according to the histogram?

Andrew

You can fit a histogram using a Gaussian (i.e. normal) distribution, for example using scipy's curve_fit. I wrote a small example below. Note that depending on your data, you may need to find a way to make a good guess at the starting value (p0) for the fit. Differences in starting values ​​may cause the fit to fail.

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

def fit_func(x,a,mu,sigma,c):
    """gaussian function used for the fit"""
    return a * norm.pdf(x,loc=mu,scale=sigma) + c

#make up some normally distributed data and do a histogram
y = 2 * np.random.normal(loc=1,scale=2,size=1000) + 2
no_bins = 20
hist,left = np.histogram(y,bins=no_bins)
centers = left[:-1] + (left[1] - left[0])

#fit the histogram
p0 = [2,0,2,2] #starting values for the fit
p1,_ = curve_fit(fit_func,centers,hist,p0,maxfev=10000)

#plot the histogram and fit together
fig,ax = plt.subplots()
ax.hist(y,bins=no_bins)
x = np.linspace(left[0],left[-1],1000)
y_fit = fit_func(x, *p1)
ax.plot(x,y_fit,'r-')
plt.show()

Gaussian Fitted Histogram

Related


How can I make the Gaussian normal match the histogram?

Sam Sloan I was wondering if there is a good way to match Gaussian normals in the form of numpy arrays with a histogram np.histogram(array, bins). How can I draw such a curve on the same graph and adjust it in height and width according to the histogram? Andre

How can I make the Gaussian normal match the histogram?

Sam Sloan I was wondering if there is a good way to match Gaussian normals in the form of numpy arrays with a histogram np.histogram(array, bins). How can I draw such a curve on the same graph and adjust it in height and width according to the histogram? Andre

How can I make the Gaussian normal match the histogram?

Sam Sloan I was wondering if there is a good way to match Gaussian normals in the form of numpy arrays with a histogram np.histogram(array, bins). How can I draw such a curve on the same graph and adjust it in height and width according to the histogram? Andre

How can I make the Gaussian normal match the histogram?

Sam Sloan I was wondering if there is a good way to match Gaussian normals in the form of numpy arrays with a histogram np.histogram(array, bins). How can I draw such a curve on the same graph and adjust it in height and width according to the histogram? Andre

Normal Gaussian curve for histogram in R

year I create a histogram by reading binary data in R from a file. I'm trying to generate a normal fit Gaussian curveto the histogram values , but I can only generate a curve that varies with the peak of the histogram at each point. Sorry if this is a very bas

Normal Gaussian curve for histogram in R

year I create a histogram by reading binary data in R from a file. I'm trying to generate a normal fit Gaussian curveto the histogram values , but I can only generate a curve that varies with the peak of the histogram at each point. Sorry if this is a very bas

How can I make this histogram in ggplot/R?

Crimea Please find My data qbelow . I have two covariates: q$Studieand q$best.respcorresponding to each of the five different studies reporting the best response after a certain treatment. q$best.respThere are three levels table(q$best.resp) 0 1 2 62 42

How can I make this histogram in ggplot/R?

Crimea Please find My data qbelow . I have two covariates: q$Studieand q$best.respcorresponding to each of the five different studies reporting the best response after a certain treatment. q$best.respThere are three levels table(q$best.resp) 0 1 2 62 42

How to plot a Gaussian on a histogram

Noodle How can I plot a Gaussian on the histogram generated by my code? Here is the code below. It works perfectly and generates histograms, but since I'm just starting out with pyplot, I'm having trouble adding a gaussian curve. I'm still struggling with the

How to plot a Gaussian on a histogram

Noodle How can I plot a Gaussian on the histogram generated by my code? Here is the code below. It works perfectly and generates histograms, but since I'm just starting out with pyplot, I'm having trouble adding a gaussian curve. I'm still struggling with the

How to make a histogram using sklearn with 1D Gaussian mixture?

Théré Hernandez I want to do a histogram with mixed 1D Gaussian images. Thanks Meng for the photos. My histogram looks like this: I have a file with a lot of data (4,000,000 numbers) in columns: 1.727182 1.645300 1.619943 1.709263 1.614427 1.522313 I'm using

How to make a histogram using sklearn with 1D Gaussian mixture?

Théré Hernandez I want to do a histogram with mixed 1D Gaussian images. Thanks Meng for the photos. My histogram looks like this: I have a file with a lot of data (4,000,000 numbers) in columns: 1.727182 1.645300 1.619943 1.709263 1.614427 1.522313 I'm using

How can I make this case match the regex?

msqar I need to match CSS as string. I don't need any #characters (Id). Need to match anything, including as stand-alone words "body", "li", "ol", "ul". "body"The words , "li", , "ol", need to be detected individually "ul". Need to detect words like: "selector

How can I make this regex match fail?

AKWF I want a regex that matches a two digit year range and the word, Caterpillaror Fordsomething like this: ABC123 08-10 Caterpillar But this is not: ABC123 fits 08-10 Caterpillar or this: ABC123 for 08-10 Caterpillar XYZ789 The ABC123sum XYZ789can change

How can I make this case match the regex?

msqar I need to match CSS as string. I don't need any #characters (Id). Need to match anything, including as stand-alone words "body", "li", "ol", "ul". "body"The words , "li", , "ol", need to be detected individually "ul". Need to detect words like: "selector

How can I make this regex match fail?

AKWF I want a regex that matches a two digit year range and the word, Caterpillaror Fordsomething like this: ABC123 08-10 Caterpillar But this is not: ABC123 fits 08-10 Caterpillar or this: ABC123 for 08-10 Caterpillar XYZ789 The ABC123sum XYZ789can change

How can I make this case match the regex?

msqar I need to match CSS as string. I don't need any #characters (Id). Need to match anything, including as stand-alone words "body", "li", "ol", "ul". "body"The words , "li", , "ol", need to be detected individually "ul". Need to detect words like: "selector

How can I make the image "normal" in a slanted div?

Wiski So I want to make a diamond with an image, but the image is distorted. I am going to make it level (normal) and fill with diamonds. code I wrote . .diamond { width: 300px; height: 300px; border: 3px solid white; outline: 2px solid black; outlin

How can I make the button normal after closing the window?

De Beer from tkinter import * win = Tk() def OptionsMenu(): OptionsWin = Tk() options.config(state=DISABLED) options = Button(win, text="Options", command=OptionsMenu) Here is the code I am trying to execute. I want the "Options" button to be disa

How can i make the header normal size. CSS Grid

username How would I make the header full width? I want the content class to be on the right and the skills class to be 30% to 70% on the right. If you can understand what I can't solve, please tell me what you did to make it work. thanks. I can't find any inf

How can I make the image "normal" in a slanted div?

Wiski So I want to make a diamond with an image, but the image is distorted. I am going to make it level (normal) and fill with diamonds. code I wrote . .diamond { width: 300px; height: 300px; border: 3px solid white; outline: 2px solid black; outlin

How can I make the button normal after closing the window?

De Beer from tkinter import * win = Tk() def OptionsMenu(): OptionsWin = Tk() options.config(state=DISABLED) options = Button(win, text="Options", command=OptionsMenu) Here is the code I am trying to execute. I want the "Options" button to be disa

How can I make this "img" element behave like normal flow?

John West Here is my markup, basically a gallery of pictures. <div class="img"> <div class="cont"><a href="#"><img src="logo.jpg" alt="logo" /></a></div> <div class="desc">This is the awesome logo</div> </div> <div class="img"> <div class="cont"><a href="#

How can I make quotes act as normal text in a regex function?

dream logic I'm trying to use regex to extract information from a large text file on google sheets, but in the regex I use quotes instead of treating everything like the text I want to use, the quotes make it so regex into many different parts. Can I add some

How can I make the image "normal" in a slanted div?

Wiski So I want to make a diamond with an image, but the image is distorted. I am going to make it level (normal) and fill with diamonds. code I wrote . .diamond { width: 300px; height: 300px; border: 3px solid white; outline: 2px solid black; outlin

How can I make the image "normal" in a slanted div?

Wiski So I want to make a diamond with an image, but the image is distorted. I am going to make it level (normal) and fill with diamonds. code I wrote . .diamond { width: 300px; height: 300px; border: 3px solid white; outline: 2px solid black; outlin

How can I make the button normal after closing the window?

De Beer from tkinter import * win = Tk() def OptionsMenu(): OptionsWin = Tk() options.config(state=DISABLED) options = Button(win, text="Options", command=OptionsMenu) Here is the code I am trying to execute. I want the "Options" button to be disa

How can i make the header normal size. CSS Grid

username How would I make the header full width? I want the content class to be on the right and the skills class to be 30% to 70% on the right. If you can understand what I can't solve, please tell me what you did to make it work. thanks. I can't find any inf