forked from mikedewar/d3py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
d3py.py
158 lines (118 loc) · 3.62 KB
/
d3py.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
import os
import webbrowser
import json
import numpy as np
import time
import urllib
import server
path_to_this_file = os.path.abspath( __file__ )
this_path = os.path.dirname(path_to_this_file)
temp_json = os.path.join(this_path, "static/temp_%s.json")
def clear():
urllib.urlopen("http://localhost:7666/clear")
def draw(mode,refresh="new"):
if refresh == "new":
webbrowser.open("http://localhost:7666/%s"%mode,new=True)
elif refresh == "manual":
pass
def line(x, y, xlabel="x", ylabel="y", color="crimson", refresh="new"):
data = {
"values":[{"x":xi, "y":yi} for xi, yi in zip(x, y)],
"labels":{
"x": xlabel,
"y": ylabel
},
"color":color
}
fname = temp_json%time.time()
fh = open(fname,'w')
json.dump(data,fh)
draw("line")
def timeseries(x, y, xlabel="x", ylabel="y", color="crimson", refresh="new"):
data = {
"values":[{"x":xi, "y":yi} for xi, yi in zip(x, y)],
"labels":{
"x": xlabel,
"y": ylabel
},
"color":color
}
fname = temp_json%time.time()
fh = open(fname,'w')
json.dump(data,fh)
draw("timeseries")
def histogram(x, xlabel="x", ylabel="p(x)", refresh="new", **kwargs):
values, edges = np.histogram(x, **kwargs)
data = {
"values":[{"x":xi, "y":yi} for xi, yi in zip(edges, values)],
"labels":{
"x": xlabel,
"y": ylabel
}
}
fh = open(temp_json%time.time(),'w')
json.dump(data,fh)
fh.close()
draw("histogram")
def scatter(x, y, c, xlabel="x", ylabel="y", refresh="new"):
data = {
"values":[{"x":xi, "y":yi, "c":ci} for (xi, yi, ci) in zip(x,y,c)],
"labels":{
"x": xlabel,
"y": ylabel
}
}
fh = open(temp_json%time.time(),'w')
json.dump(data,fh)
fh.close()
if refresh == "new":
webbrowser.open("http://localhost:7666/scatter",new=True)
elif refresh == "manual":
pass
draw("scatter")
def bar(values, labels, ylabel="count", refresh="new"):
data = {
"values":[
{"x":xi, "y":yi}
for xi, yi in zip(labels, values)
],
"labels":{
"x": None,
"y": ylabel
}
}
fh = open(temp_json%time.time(),'w')
json.dump(data,fh)
fh.close()
draw("bar")
### on import clear the canvas and start the server
# this needs to start in a separte process or something
#server.main()
clear()
if __name__ == "__main__":
import d3py
if True:
# line plot example
T = 5*np.pi
x = np.linspace(-T,T,100)
colours = ["red","blue","green"]
for a,c in zip([0.1, 0.2, 0.3], colours):
y = np.exp(-a*x) * np.sin(x)
d3py.line(x, y, xlabel="time", ylabel="value", color=c)
if False:
# histogram example
d3py.histogram(np.random.standard_normal(1000), density=True)
if False:
# scatter example
n = 400
d1 = np.random.multivariate_normal([1,1], 0.5*np.eye(2), size=n)
d2 = list(np.random.multivariate_normal([-1,-1], 2*np.eye(2), size=n))
x = [d[0] for d in d1] + [d[0] for d in d2]
y = [d[1] for d in d1] + [d[1] for d in d2]
c = ["crimson" for i in range(n)] + ["green" for i in range(n)]
d3py.scatter(x, y, c, xlabel="pigs", ylabel="cows")
if False:
# bar example
values = [1,4,7,3,2,9]
labels = ["a", "b", "c", "d", "e", "f"]
d3py.bar(values, labels)