Python Forum
histogram with matplotlib
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
histogram with matplotlib
#1
Hi everyone,

I have tried the following code from the matplotlib tutorial.

https://matplotlib.org/gallery/statistic...tures.html

in my computer, but I don't get the same Gaussian fit by running the python code on my computer. I only get a straight line :-(

Can anyone tell me why?

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

np.random.seed(19680801)

# example data
mu = 100  # mean of distribution
sigma = 15  # standard deviation of distribution
x = mu + sigma * np.random.randn(437)

num_bins = 50

fig, ax = plt.subplots()

# the histogram of the data
n, bins, patches = ax.hist(x, num_bins, density=1)

# add a 'best fit' line
y = ((1 / (np.sqrt(2 * np.pi) * sigma)) *
     np.exp(-0.5 * (1 / sigma * (bins - mu))**2))
ax.plot(bins, y, '--')
ax.set_xlabel('Smarts')
ax.set_ylabel('Probability density')
ax.set_title(r'Histogram of IQ: $\mu=100$, $\sigma=15$')

# Tweak spacing to prevent clipping of ylabel
fig.tight_layout()
plt.show()

#############################################################################
#
# ------------
#
# References
# """"""""""
#
# The use of the following functions and methods is shown in this example:

matplotlib.axes.Axes.hist
matplotlib.axes.Axes.set_title
matplotlib.axes.Axes.set_xlabel
matplotlib.axes.Axes.set_ylabel
Reply


Messages In This Thread
histogram with matplotlib - by vaugirard - May-09-2018, 10:08 PM
RE: histogram with matplotlib - by scidam - May-09-2018, 11:55 PM
RE: histogram with matplotlib - by vaugirard - May-09-2018, 11:59 PM
RE: histogram with matplotlib - by killerrex - May-10-2018, 12:13 AM
RE: histogram with matplotlib - by vaugirard - May-30-2018, 09:37 PM
RE: histogram with matplotlib - by killerrex - May-30-2018, 10:16 PM
RE: histogram with matplotlib - by vaugirard - May-30-2018, 10:21 PM
RE: histogram with matplotlib - by vaugirard - May-31-2018, 04:21 AM
RE: histogram with matplotlib - by vaugirard - May-31-2018, 09:40 PM
RE: histogram with matplotlib - by snippsat - Jun-01-2018, 02:39 AM
RE: histogram with matplotlib - by vaugirard - Jun-01-2018, 04:13 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to fit a 2D histogram like in ROOT? Zandar 2 4,258 Aug-06-2019, 03:01 PM
Last Post: Zandar
  histogram error alyssaantarctica 1 3,856 Jul-09-2018, 10:46 PM
Last Post: Larz60+
  Plot Probability Density of an Histogram Python_TLS 1 2,947 Jun-28-2018, 06:19 AM
Last Post: scidam
  Help creating a histogram using an array rubadub 0 2,247 Apr-17-2018, 08:57 PM
Last Post: rubadub
  Histogram help dandatadan 1 2,614 Nov-25-2017, 06:07 AM
Last Post: heiner55
  mcerp: error while plotting a histogram rakhmadiev 2 4,654 Apr-02-2017, 09:08 PM
Last Post: rakhmadiev
  plotting histogram vvv 1 3,373 Mar-10-2017, 11:47 PM
Last Post: zivoni

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020