-5

I have some data (TEMP_2) and I wanted to obtain a distribution for that data. I know how to do the histogram using:

import numpy as np
from pylab import *

plt.figure(1)
data1 = loadtxt("TEMP_2")

a= data1[:,1]
plt.hist(a,100, normed=True,)
show ()

But, I wanted to have a distribution. Can anybody please help me with this..

data file:

1000 299.23
2000 310.56
3000 308.21
4000 305.86
5000 305.21
6000 301.35
7000 295.37
8000 307.80
9000 295.61
:      :
:      :
200000 307.18
Joe Day
  • 6,965
  • 4
  • 25
  • 26
Shila D
  • 3
  • 1
  • 2
    You'll have to be more specific about what you want. The raw data is itself a (very specific) distribution – Adam Rosenfield Oct 18 '12 at 15:43
  • I wanted to obtain a distribution plot with out showing the bins. Just , a one line distribution plot – Shila D Oct 18 '12 at 15:47
  • As I mentioned before, I don't want to present my data with the histogram.. I mean , the bars of the histogram. – Shila D Oct 18 '12 at 15:49
  • First you said you want to have a distribution and plot it, but then you say you don't want to present it with the histogram. I'm confused :/ What do you want? – jsalonen Oct 18 '12 at 15:51
  • 1
    I need something like this:http://www.google.com/imgres?um=1&hl=en&sa=N&biw=1301&bih=609&tbm=isch&tbnid=80rm0nRHEBZwyM:&imgrefurl=http://glowingpython.blogspot.com/2012/07/distribution-fitting-with-scipy.html&docid=nWXdc4vI56MtfM&imgurl=http://4.bp.blogspot.com/-LKBzu8-e3HA/UAUPkuhGUaI/AAAAAAAAAak/pXHiEoNTfm4/s1600/normaldist.png&w=812&h=612&ei=ZSWAUMCPJOqW0QGKioGACg&zoom=1&iact=rc&dur=552&sig=114445739387104976107&page=1&tbnh=142&tbnw=175&start=0&ndsp=17&ved=1t:429,r:12,s:0,i:108&tx=111&ty=72 – Shila D Oct 18 '12 at 15:51
  • You have to tell it what distribution you want to plot. Do you want a normal? What's the mean and stdev? – Joel Cornett Oct 18 '12 at 16:08

2 Answers2

1

Try with:

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

mean, sigma = norm.fit(data) #your data here
x = np.linspace(-3,3,100)
plt.plot(x,mlab.normpdf(x,mean,sigma))

plt.show()

As described here: python pylab plot normal distribution

Community
  • 1
  • 1
luke14free
  • 2,529
  • 1
  • 17
  • 25
0

In order to plot a normal distribution that fits your data you need to do the following:

First you need to calculate, which normal distribution best fits your data. In scipy there is norm.fit. Next you just need to plot a normal distribution with the given properties (mean, stdev).

Full script:

# Load data
import numpy as np
from pylab import *
data1 = loadtxt("TEMP_2")
a = data1[:,1]

# Fit data into normal distribution
from scipy.stats import norm
mean, stdev = norm.fit(a)

# Plot normal distribution
import matplotlib.mlab as mlab
x = np.linspace(min(a), max(a), 100)
plot(x, mlab.normpdf(x, mean, stdev))
show()

Result:

Plot screenshot

If you want to plot the "bins" too, then just add before plot.show():

plt.hist(a, len(data1), normed=True,)
jsalonen
  • 29,593
  • 15
  • 91
  • 109
  • You're welcome. Please be aware that as I found your question to be very vague, I'm really unsure if this is the correct kind of a distribution plot. – jsalonen Oct 18 '12 at 16:38
  • Gaussian distribution was the one I looking for..Sorry not mentioning it before... I think we are in the right page... Thanks! – Shila D Oct 18 '12 at 16:52