# mplot5.py
# Written by Todd Wareham for CS 2500
"""
Given an upper limit nlim as a command-line argument, plots the functions 
 n, n*n, n*N*N, and 2**n on the range [1, .., nlim](increment by 2) in
 one plot with a legend describing the plot-lien / function association.
 If an additional command-line argument plot-file is also given, saves plot 
 to that file in the format specified by the file's extension.

Note that this is implemented using x-data/function plotting, where x-data is
 explicitly created as a NumPy-style array.
"""

from pylab import *


if len(sys.argv) not in [2, 3]:
    print "usage: ", sys.argv[0], " nlim {plot-file}"
    sys.exit(1)

nlim = int(sys.argv[1])

## Set up plot data

x = arange(1, (nlim + 1), 2)

## Describe plot characteristics

xlabel("n")
ylabel("f(n)")
title("function plots")

## Create plotted lines in one plot with descriptive legend

p = []
p += plot(x, x, "k-o")
p += plot(x, x * x, "b--s")
p += plot(x, x * x * x, "g-.+")
p += plot(x, 2 ** x, "m:x")
ylim(0,100)

l = ["$n$", "$n^2$", "$n^3$", "$2^n$"]
legend(p, l, loc="upper left")

## Display plotted figure

print ">>> Creating requested plot ..."

show()

## Save plotted figure

if len(sys.argv) == 3:
    print ">>> Saving requested plot ..."
    savefig(sys.argv[2])

print ">>> Plotting finished"

