Matplotlib: Data Analysis Tool for Pythonian

Some might say they love to analyze data using MATLAB because it has all necessary functions and widely used by scientists and engineers around the world for decade. Anyway, it would be better to have something similar available in Linux for free. Actually, we have gnuplot and Octave. Especially, Octave supports m scripting language like MATLAB does. However, I am a Pythonian and I would be happier with Python scripting language. That's why we have Matplotlib.

For example, we have data in CSV representing timestamp and a value. It is not exactly similar to histogram because it has 2 columns in the data file. In Matplotlib, you are able to use module csv as usual including parse the specific timestamp format. For example, the timestamp may be stored in short like "Oct 11.23" to represent "2006/10/11 23:00:00 UTC". You can parse the timestamp using time.strptime() using below format.

%b %d.%H

As a result, I could read two columns CSV as follows.

input_file = 'input.csv'

reader = csv.reader(open(input_file,'rb'))
xdata,ydata = [],[]
for row in reader:
    x,y = row
    x = '06 '+x
    x = epoch2num(time.mktime(time.strptime(x,'%y %b %d.%H')))
    y = float(y)
    if y > 1000:
        y = 0.0
    xdata.append(x)
    ydata.append(y)

Then plot that data in bar chart using Matplotlib.

figure(1)
bar(xdata,ydata,0.01,color='b')
show()

Simple. Huh?

Tags: , , , ,

Reply