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 !
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.
To get the values from a histogram, plt. hist returns them, so all you have to do is save them.
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With