-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathplot.py
185 lines (161 loc) · 5.45 KB
/
plot.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
###############################################################################
#
# CSCI 4446 - Chaotic Dynamics
#
# File: plot.py
# Author: Ken Sheedlo
#
# High level plotting library.
#
###############################################################################
from __future__ import division
import matplotlib.pyplot
import mpl_toolkits.mplot3d
import numpy
import utils
def _render_to_output(figure, file_prefix):
'''
Outputs a plot to the screen or a file.
Params:
figure The figure to show.
file_prefix Either a filename prefix (the file extension is always .png)
or None, in which case outputs to the screen.
Returns None, and results in a plot to the screen or a file.
'''
if file_prefix is None:
figure.show()
else:
figure.savefig('{0}.png'.format(file_prefix), dpi=220)
def render(*args, **kwargs):
'''
Renders a 2D plot.
'''
figure = matplotlib.pyplot.figure()
axes = figure.gca()
opt_actions = (
('title', axes.set_title),
('xlabel', axes.set_xlabel),
('ylabel', axes.set_ylabel),
('xticks', axes.set_xticks),
('yticks', axes.set_yticks),
('xticklabels', axes.set_xticklabels),
('yticklabels', axes.set_yticklabels),
('aspect', axes.set_aspect),
('xbound', lambda (x1, x2): axes.set_xbound(x1, x2)),
('ybound', lambda (y1, y2): axes.set_ybound(y1, y2)),
('legend', axes.legend),
('file_prefix', lambda _: None),
('ax_callback', lambda _: None),
)
opts, plot_args = utils.split_dict([name for (name, _) in opt_actions], kwargs)
if 'ax_callback' in opts:
opts['ax_callback'](axes)
else:
axes.plot(*args, **plot_args)
# Use tuples instead of a dict because order matters. Want to set xticks
# before xlabels, etc.
for (name, action) in opt_actions:
if name in opts:
action(opts[name])
_render_to_output(figure, opts.get('file_prefix'))
def render3d(*args, **kwargs):
'''
Renders a 3D plot.
'''
figure = matplotlib.pyplot.figure()
axes = figure.add_subplot(111, projection='3d')
opt_actions = (
('title', axes.set_title),
('xlabel', axes.set_xlabel),
('ylabel', axes.set_ylabel),
('zlabel', axes.set_zlabel),
('xticks', axes.set_xticks),
('yticks', axes.set_yticks),
('zticks', axes.set_zticks),
('xticklabels', axes.set_xticklabels),
('yticklabels', axes.set_yticklabels),
('zticklabels', axes.set_zticklabels),
('aspect', axes.set_aspect),
('xbound', lambda (x1, x2): axes.set_xbound(x1, x2)),
('ybound', lambda (y1, y2): axes.set_ybound(y1, y2)),
('zbound', lambda (z1, z2): axes.set_zbound(z1, z2)),
('file_prefix', lambda _: None),
('ax_callback', lambda _: None),
)
opts, plot_args = utils.split_dict([name for (name, _) in opt_actions], kwargs)
if 'ax_callback' in opts:
opts['ax_callback'](axes)
else:
axes.plot(*args, **plot_args)
for (name, action) in opt_actions:
if name in opts:
action(opts[name])
_render_to_output(figure, opts.get('file_prefix'))
def mod2pi(*args, **kwargs):
'''
Renders a 2D plot modulo 2*pi.
'''
plot_args = dict(kwargs)
plot_args.update({
'xbound': (0, 2*numpy.pi),
'xticks': (
0,
numpy.pi/2,
numpy.pi,
3*numpy.pi/2,
2*numpy.pi
),
'xticklabels': (
'0',
r'$\frac{\pi}{2}$',
r'$\pi$',
r'$\frac{3\pi}{2}$',
r'$2\pi$'
)
})
render(*args, **plot_args)
def embedded(vs, xdim, ydim, *args, **kwargs):
'''
Renders a delay coordinate embedded data set.
'''
xs = numpy.array([
utils.mod2pi(v) for v in vs[:,xdim]
], dtype=numpy.float64)
ys = numpy.array([
utils.mod2pi(v) for v in vs[:,ydim]
], dtype=numpy.float64)
rfunc = render if 'xbound' in kwargs else mod2pi
plot_args = (xs, ys) + tuple(args)
rfunc(*plot_args, **kwargs)
def loglog(*args, **kwargs):
'''
Renders a 2D log-log plot.
All the same options and kwargs as plot.render are supported.
'''
figure = matplotlib.pyplot.figure()
axes = figure.gca()
opt_actions = (
('title', axes.set_title),
('xlabel', axes.set_xlabel),
('ylabel', axes.set_ylabel),
('xticks', axes.set_xticks),
('yticks', axes.set_yticks),
('xticklabels', axes.set_xticklabels),
('yticklabels', axes.set_yticklabels),
('aspect', axes.set_aspect),
('xbound', lambda (x1, x2): axes.set_xbound(x1, x2)),
('ybound', lambda (y1, y2): axes.set_ybound(y1, y2)),
('file_prefix', lambda _: None),
('ax_callback', lambda _: None),
)
opts, plot_args = utils.split_dict([name for (name, _) in opt_actions], kwargs)
if 'ax_callback' in opts:
opts['ax_callback'](axes)
else:
axes.loglog(*args, **plot_args)
# Use tuples instead of a dict because order matters. Want to set xticks
# before xlabels, etc.
for (name, action) in opt_actions:
if name in opts:
action(opts[name])
_render_to_output(figure, opts.get('file_prefix'))