SarahDavis
SarahDavis

Reputation: 51

Make histogram from data in python

I am trying to make a histogram from data saved in a .dat file: I have made other types of plots, but when trying to make the histogram i receive an error: ValueError: x has only one data point. bins or range kwarg must be given. The table definitely has (many) more than one data point! code below...

import numpy as  np
import matplotlib.pyplot as plt

a=open('/24_5_15b.dat','r')

header0=a.readline()

W1=[]
W2=[]

for line in a:
    line=line.strip()
    columns=line.split()
    W1=float(columns[13])
    W2=float(columns[15])

    w1=np.asarray(W1)
    w2=np.asarray(W2)   

    n, bins, patches = plt.hist(w1, 20, normed=1, histtype='bar', rwidth=0.8)

plt.show() 

When I ask to print w1 it prints all the values. All the numbers are floats - would this make a difference? Thanks...

Upvotes: 1

Views: 1681

Answers (1)

Matthew
Matthew

Reputation: 658

From the looks of this, you are trying to plot a histogram with the first line:

for line in a:
    ...
    n, bins, patches = plt.hist(w1, 20, normed=1, histtype='bar', rwidth=0.8)

to make a histogram, you need to pass all of the data, not one at a time.

I would recommend using genfromtxt for this too, with which you can use something like:

a = np.genfromtxt('/24_5_15b.dat')  # delimiter is " " by default
w1 = a[:,13]
w2 = a[:,15]

plt.hist(w1, ...)

Upvotes: 1

Related Questions