#!/usr/bin/env python
import sys

import matplotlib.pyplot as plt
from matplotlib.ticker import AutoMinorLocator

### Constants
COLORS = ['r','b', 'g','c','m']
IMAGE_TYPE = '.png'

### Implementation
def read_data(filename):
    data = []
    with open(fname, 'r') as f:
        for line in f:
            cols = [col.strip() for col in line.split(',')]
            if len(data) == 0:
                for i in range(len(cols)):
                    data.append([])
            for i, col in enumerate(cols):
                data[i].append(int(col))
    return data

def set_minor_axis():
    axis = plt.gca()
    x_minor = AutoMinorLocator()
    axis.xaxis.set_minor_locator(x_minor)
    y_minor = AutoMinorLocator()
    axis.yaxis.set_minor_locator(y_minor)

def plot(data, title, output):
    plot_cmd = []
    for i, col in enumerate(data[1:]):
        plot_cmd.extend([data[0], col, COLORS[i % len(COLORS)]])
    plt.plot(*plot_cmd)
    plt.title(fname)
    set_minor_axis()
    plt.grid()
    plt.savefig(output)

### Main
if len(sys.argv) != 2:
    print 'prog xxx.csv'
    sys.exit(1)

fname = sys.argv[1]
title = fname
if len(fname.split('.')) == 1:
    output = fname + IMAGE_TYPE
else:
    output = '.'.join(fname.split('.')[:-1]) + IMAGE_TYPE

data = read_data(fname)
plot(data, title, output)
