-
Notifications
You must be signed in to change notification settings - Fork 0
/
mplot.py
206 lines (172 loc) · 6.26 KB
/
mplot.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/usr/bin/env python
###############################################
#
# mplot - manipulate parameters of function
# writen by Xusheng Xu (thuxuxs@gmail.com)
#
###############################################
import numpy as np
from scipy.integrate import ode
from matplotlib.widgets import Slider
import matplotlib.pyplot as plt
import pandas as pd
import sys
v=3 if sys.version>'2' else 2
class mplot:
"""
Xusheng Xu, thuxuxs@gmail.com
totally control the function you want to show by the parameters
usage
------
def generate(x, a, phi):
out = np.array([x, a * np.sin(x + phi), a * np.cos(x + phi)]).T
return pd.DataFrame(out, columns=['x', 'sin', 'cos'])
fig = mplot(generate, np.linspace(0, 10, 100), a=(1, 2), phi=(0, 2 * np.pi))
# usage 1
# sub1 = fig.add_subplot(121)
# fig.add_line(sub1, 'x', 'sin', 'r', linewidth=2, label='sin')
# fig.add_line(sub1, 'x', 'cos', 'k-.', lw=4, label='cos')
# sub2 = fig.add_subplot(122)
# fig.add_line(sub2, 'sin', 'cos')
# usage 2
fig.add_subplot()
fig.add_all()
fig.show()
"""
def __init__(self, func, x, **kwargs):
self.bottom = 0.08
self.slider_width = 0.8
self.slider_height = 0.04
self.func = func
self.x = x
self.kwargs = kwargs
self.arg_num = len(self.kwargs)
if v==2:
self.parameter = {name: (high + low) / 2.0 for name, (low, high) in self.kwargs.iteritems()}
else:
self.parameter = {name: (high + low) / 2.0 for name, (low, high) in self.kwargs.items()}
self.func_init = self.func(self.x, **self.parameter)
self.fig = plt.figure()
self.sub = []
self.lines = []
self.sliders = {}
def add_subplot(self, poi=111):
"""
:param poi: position of the figure
"""
self.sub.append(self.fig.add_subplot(poi))
return self.sub[-1]
def add_line(self, sub, x_axis, y_axis, *args, **kwargs):
"""
add a line to a certain subplot
:param sub: subplot
:param x_axis: x axis name
:param y_axis: y axis name
:param args: line style for plot
:param kwargs: line style for plot
"""
line = sub.plot(self.func_init[x_axis], self.func_init[y_axis], *args, **kwargs)[0]
self.lines.append({'x': x_axis, 'y': y_axis, 'line': line})
plt.legend()
def add_all(self, sub=None):
"""
add all lines to one subplot
:param sub: subplot
"""
if sub is None:
sub = self.sub[-1]
x_axis = self.func_init.columns[0]
for y_axis in self.func_init.columns[1:]:
line = sub.plot(self.func_init[x_axis], self.func_init[y_axis])[0]
self.lines.append({'x': x_axis, 'y': y_axis, 'line': line})
plt.legend()
def show(self):
self.fig.subplots_adjust(bottom=self.bottom + self.arg_num * self.slider_height)
if v==2:
for index, (name, (low, high)) in enumerate(self.kwargs.iteritems()):
self.sliders[name] = Slider(plt.axes(
[0.1, index * self.slider_height + self.bottom / 2.0, self.slider_width, self.slider_height * 0.8]),
name, low, high, valinit=self.parameter[name])
self.sliders[name].on_changed(self.update(name))
else:
for index, (name, (low, high)) in enumerate(self.kwargs.items()):
self.sliders[name] = Slider(plt.axes(
[0.1, index * self.slider_height + self.bottom / 2.0, self.slider_width, self.slider_height * 0.8]),
name, low, high, valinit=self.parameter[name])
self.sliders[name].on_changed(self.update(name))
plt.show()
def update(self, *name):
name, = name
def update_real(val):
self.parameter[name] = val
self.func_on_changed = self.func(self.x, **self.parameter)
for line in self.lines:
line['line'].set_xdata(self.func_on_changed[line['x']])
line['line'].set_ydata(self.func_on_changed[line['y']])
for i in self.sub:
i.relim()
i.autoscale_view()
self.fig.canvas.draw()
return update_real
def dsolve(f, t, parameters, init, result_handle=lambda x: x,integrator='zvode'):
"""
Xusheng Xu, thuxuxs@gmail.com
Solve differential equations!
usage
------
def f(t,y,args):
omega,kappa=[args[i] for i in ['omega','kappa']]
a=y[0]
return [np.cos(omega*t)*np.exp(-kappa*t)*omega]
t=np.arange(0,100,0.1).reshape((-1,1))
parameters={'omega':(0,10),
'kappa':(0,0.1)}
init=[[['y'],[0]],['t',0]]
def result_handle(out):
out['y']=out['y']**2
return out
dsolve(f,t,parameters,init,result_handle)
"""
dt = t[1, 0] - t[0, 0]
t1 = t[-1, 0]
y0 = init[0][1]
t0 = init[1][1]
col = [init[1][0]]
col.extend(init[0][0])
def generate(t, **parameters):
r = ode(f).set_integrator(integrator)
r.set_initial_value(y0, t0).set_f_params(parameters)
out = []
while r.successful() and r.t <t1:
r.integrate(r.t + dt)
out.append(r.y)
out = np.concatenate((t[:len(out)], np.array(out)), axis=1)
out = pd.DataFrame(out, columns=col)
return result_handle(out)
fig = mplot(generate, t, **parameters)
fig.add_subplot()
fig.add_all()
fig.show()
if __name__ == '__main__':
def generate(x, a, phi):
"""
generate sin and cos waves
:param x: x scale
:param a: amplitude
:param phi: phase
:return: pandas DataFrame with columns well defined
"""
out = np.array([x, a * np.sin(x + phi), a * np.cos(x + phi)]).T
return pd.DataFrame(out, columns=['x', 'sin', 'cos'])
fig = mplot(generate, np.linspace(0, 10, 100), a=(1, 2), phi=(0, 2 * np.pi))
# usage 1
sub1 = fig.add_subplot(121)
fig.add_line(sub1, 'x', 'sin', 'r', linewidth=2, label='sin')
fig.add_line(sub1, 'x', 'cos', 'k-.', lw=4, label='cos')
sub2 = fig.add_subplot(122)
fig.add_line(sub2, 'sin', 'cos')
fig.show()
# usage 2
# fig.add_subplot()
# fig.add_all()
# fig.show()