# mplot4.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 
 separate sub-plots on 2x2 grid. 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)

## Create plotted lines in separate sub-plots

subplot(2, 2, 1)
xlabel("n")
ylabel("n")
plot(x, x, "b-o")
ylim(0,100)

subplot(2, 2, 2)
xlabel("n")
ylabel("n*n")
plot(x, x * x, "b-o")
ylim(0,100)

subplot(2, 2, 3)
plot(x, x * x * x, "b-o")
xlabel("n")
ylabel("n*n*n")
ylim(0,100)

subplot(2, 2, 4)
xlabel("n")
ylabel("2**n")
plot(x, 2 ** x, "b-o")
ylim(0,100)

## 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"

