forked from NITDgpOS/PlotIt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plotit.py
132 lines (106 loc) · 4 KB
/
plotit.py
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
from lib import plotutil as plu
import optparse
import signal
# Defaults
xstart = 0
xend = 100
stepsize = 1.0
color = "blue"
xlabel = "X-axis"
ylabel = "Y-axis"
line_style = "-"
dot_style = "o"
dot_size = 5
file_path = ""
def sigint_handler(signum, frame):
"""
Custom handler called by signal.single module to display shutdown message if user presses
Ctrl+C to quit program.
:param signum: the signal number
:param frame: the current stack frame
:returns: None
"""
print('shutting down PlotIt...')
exit(0)
signal.signal(signal.SIGINT, sigint_handler)
# Parse command line arguments and switches
parser = optparse.OptionParser()
parser.add_option('-f', '--function', dest='func',
help='Enter function to visualise')
parser.add_option('-s', '--xstart', dest='xstart',
help='Enter starting x-value')
parser.add_option('-e', '--xend', dest='xend',
help='Enter ending x-value')
parser.add_option('-z', '--stepsize', dest='stepsize',
help='Enter step size')
parser.add_option('-c', '--color', dest='color',
help='Enter the color for plot')
parser.add_option('-x', '--xlabel', dest='xlabel',
help='Enter the x-label for plot')
parser.add_option('-y', '--ylabel', dest='ylabel',
help='Enter the y-label for plot')
parser.add_option('-p', '--points', dest='xpoints',
help='Enter discrete x values for which the \
function will be plotted like [x1,x2,x3,...,xn]')
parser.add_option('-d', '--dot', dest='dot', help='Enter comma separated X and Y points like\
5,9,4,7,4,6 to plot the dots at\
(5,9),(4,7),(4,6) coordinates.')
parser.add_option('-l', '--line', dest='line',
help='Enter 2 Arrays of X and Y Coordinates like \
\'[x1,x2,x3,...,xn],[y1,y2,y3,...,yn]\'')
parser.add_option('-t', '--theme', dest='theme',
help='Enter theme for displaying plot (dark or light)')
parser.add_option('--symbol', dest='line_style',
help='Enter linestyle for plot, accepted linestyles "-", ":", "-.", "--"',
choices=["-",":","-.","--"])
parser.add_option('--save', dest='file_path',
help='Enter file path eg: path/filename.png')
(options, args) = parser.parse_args()
if not options.func and not options.line and not options.dot:
print('Please enter a function or 2 arrays of x and y coordinates or Coordinates of a single dot')
exit(0)
if options.color:
color = str(options.color)
if options.xlabel:
xlabel = str(options.xlabel)
if options.ylabel:
ylabel = str(options.ylabel)
if options.line_style:
line_style = str(options.line_style)
if options.theme:
theme = str(options.theme)
else:
theme = 'default'
if options.file_path:
file_path = str(options.file_path)
if options.func:
func = options.func
if options.xpoints and (options.xstart or options.xend or options.stepsize):
parser.error("Can't use either of xstart, xend or stepsize \
with the option xpoints")
elif options.xpoints:
xpoints = list(map(float, options.xpoints[1:-1].split(',')))
discrete = True
else:
if options.xstart:
xstart = int(options.xstart)
else:
xstart = 0
if options.xend:
xend = int(options.xend)
else:
xend = 100
if options.stepsize:
stepsize = int(options.stepsize)
else:
stepsize = 1
xpoints = range(xstart, xend + 1, stepsize)
discrete = False
plu.plot(func, xpoints, color, xlabel, ylabel, theme, False, line_style, file_path, discrete)
elif options.dot:
xyval = options.dot
plu.plot_dot(xyval, color, xlabel, ylabel, theme, False, dot_style, file_path)
elif options.line:
xypoints = options.line
plu.plot_line(xypoints, color, xlabel, ylabel, theme, False, line_style, file_path)
# Visualise using matplotlib