forked from drdrang/simple-plotting
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplot
executable file
·71 lines (59 loc) · 1.54 KB
/
plot
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/python
from __future__ import division
from numpy import *
import matplotlib.pyplot as plt
import sys
import getopt
usage = '''plot [option] 'functions' minx maxx [miny maxy]
Options:
-h, --help print this message
-p, --pdf generate PDF plot
Generate a PNG (default) or PDF plot of the given function(s). The
independent variable in the function(s) is x. Multiple functions are
separated by a semicolon. The min and max values can be given as
expressions. The output is typically redirected to a file.
Example:
plot 'tan(x); x' pi 3*pi/2-.01 0 5 > plot.png
'''
# Handle the options.
try:
opts, args = getopt.getopt(sys.argv[1:], 'hp', ['help', 'pdf'])
except getopt.GetoptError as err:
sys.stderr.write(str(err) + '\n\n')
sys.stderr.write(usage)
sys.exit()
fmt = 'png'
for o, a in opts:
if o == '-p' or o == '--pdf':
fmt = 'pdf'
else:
sys.stderr.write(usage)
sys.exit()
# Handle the arguments.
try:
fcns = [x.strip() for x in args[0].split(';')]
minx = eval(args[1])
maxx = eval(args[2])
except IndexError:
sys.stderr.write(usage)
sys.exit()
plt.xlim(minx, maxx)
try:
miny = float(args[3])
maxy = float(args[4])
plt.ylim(miny, maxy)
except IndexError:
pass
# Make the plot.
x = linspace(minx, maxx, 100)
for i, f in enumerate(fcns):
y = eval(f)
plt.plot(x, y, "-", linewidth=2)
plt.legend(fcns, frameon=False);
plt.minorticks_on()
plt.grid(which='both', color='#aaaaaa')
# Output the plot.
if fmt == 'pdf':
plt.savefig(sys.stdout, format='pdf')
else:
plt.savefig(sys.stdout, format='png')