#! /usr/bin/python3
import sys
import matplotlib.pyplot as plt
from math import *

if len(sys.argv) != 3:
    print("usage:", sys.argv[0], "<filename> <lspace>")
    exit()

f=open(sys.argv[1])
lines = [line.strip() for line in f.readlines()]
f.close()

plt.figure(figsize=(4, 3.2), dpi=80)

# first two lines format the graph
#plt.title(lines[0])
plt.xlabel(lines[1].split("|")[0])
plt.ylabel(lines[1].split("|")[1])

# third line has the title of each implementation
impl_names=[name for name in lines[2].split(" ")]

# first column defines the inputs
impl_domain = [float(x.split(" ")[0]) for x in lines[3:]]
#colors = ["C0", "C1", "C2", "C3"]
#colors = ["C1", "C4", "C3", "C5"]

# plot inputs against output column
impl_range = []
for i in range(len(impl_names)):
    impl_range.append([float(x.split(" ")[i+1].strip("s")) for x in lines[3:]])
    plt.loglog(impl_domain, impl_range[i], '-', label=impl_names[i]) #, color=colors[i])

#loc={'best', 'upper right', 'upper left', 'lower left', 'lower right', 'right', 'center left', 'center right', 'lower center', 'upper center', 'center'}
plt.legend(loc="best",fancybox=True, shadow=True)
plt.grid(True, which='both',color='0.95')
outputname = sys.argv[1].split(".")[0]
plt.subplots_adjust(left=float(sys.argv[2]), right=0.995, top=0.995, bottom=0.09)
plt.savefig(outputname + ".eps", bbox_inches='tight')
plt.show()
#plt.savefig(outputname + ".png")

