-
Notifications
You must be signed in to change notification settings - Fork 10
/
graph.py
41 lines (32 loc) · 1.62 KB
/
graph.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
import plotly
from plotly.offline import iplot, init_notebook_mode
init_notebook_mode(connected=True)
def trace(data, mode = 'markers', name="data"):
x_values = list(map(lambda point: point['x'],data))
y_values = list(map(lambda point: point['y'],data))
return {'x': x_values, 'y': y_values, 'mode': mode, 'name': name}
def line_function_trace(line_function, x_values, mode = 'line', name = 'line function'):
values = line_function_data(line_function, x_values)
values.update({'mode': mode, 'name': name})
return values
def line_function_data(line_function, x_values):
y_values = list(map(lambda x: line_function(x), x_values))
return {'x': x_values, 'y': y_values}
def m_b_data(m, b, x_values):
y_values = list(map(lambda x: m*x + b, x_values))
return {'x': x_values, 'y': y_values}
def m_b_trace(m, b, x_values, mode = 'line', name = 'line function'):
values = m_b_data(m, b, x_values)
values.update({'mode': mode, 'name': name})
return values
def plot(traces, layout = {}):
if not isinstance(traces, list): raise TypeError('first argument must be a list. Instead is', traces)
plotly.offline.iplot({'data': traces, 'layout': layout})
def build_layout(x_range = None, y_range = None, options = {}):
layout = {}
if isinstance(x_range, list): layout.update({'xaxis': {'range': x_range}})
if isinstance(y_range, list): layout.update({'yaxis': {'range': y_range}})
layout.update(options)
return layout
def trace_values(x_values, y_values, mode = 'markers', name="data", text = []):
return {'x': x_values, 'y': y_values, 'mode': mode, 'name': name, 'text': text}