Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Histogram in Matplotlib with input file

I wish to make a Histogram in Matplotlib from an input file containing the raw data (.txt). I am facing issues in referring to the input file. I guess it should be a rather small program. Any Matplotlib gurus, any help ?

I am not asking for the code, some inputs should put me on the right way !

like image 995
Arkapravo Avatar asked Apr 07 '10 06:04

Arkapravo


People also ask

How will you make a histogram using MatPlotLib?

Create Histogram In Matplotlib, we use the hist() function to create histograms. The hist() function will use an array of numbers to create a histogram, the array is sent into the function as an argument.

How do you get data from a histogram in Python?

To get the values from a histogram, plt. hist returns them, so all you have to do is save them.


1 Answers

i would recommend using 'loadtxt' which is actually in the NumPy library. There are related functions in Matplotlib (csv2rec) but Matplotlib is actually standardizing on loadtxt.

Here's how it works:

from matplotlib import pyplot as PLT

with open('name_of_your_file.csv') as f:
  v = NP.loadtxt(f, delimiter=",", dtype='float', comments="#", skiprows=1, usecols=None)

'v', the object returned from 'loadtxt', is an n x m NumPy array.

'loadtxt' accepts either a file or a file descriptor. The instance above has most of the method signature. 'skiprows' is an integer that specifies the number of rows counting from the top that you want to skip; it's common to set it to "1" to skip the header row; 'usecols' begins at '0' and is a list reciting the columns you want to include ('None' is the default, and means 'include all'). The other parameters work as expected.

To plot a histogram from this data:

from matplotlib import pyplot as PLT

v_hist = NP.ravel(v)   # 'flatten' v
fig = PLT.figure()
ax1 = fig.add_subplot(111)

n, bins, patches = ax1.hist(v_hist, bins=50, normed=1, facecolor='green')
PLT.show()
like image 184
doug Avatar answered Sep 23 '22 11:09

doug