-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatplotlib_figure.py
118 lines (102 loc) · 3.28 KB
/
matplotlib_figure.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
"""
Coin-flip stock market price simulator
This example requires matplotlib and mpld3
"""
import math
import random
import time
# noinspection PyPackageRequirements, PyUnresolvedReferences
import matplotlib.pyplot as plt
from quasargui import *
from quasargui.plot import Plot
loading = Model[bool](True)
calculation_time = Model(0.0)
n = Model(1000)
n_processes = Model(10)
start = Model(10.0)
drift = Model(0.001)
variance = Model(0.02)
interactive = Model(False)
animated = Model(False)
fig, ax = plt.subplots(1, 1)
plot = Plot(renderer=Computed(lambda i: 'mpld3' if bool(i) else 'png', interactive))
def add_scenario(update_plot=True):
global ax, plot, animated
def create_random_process():
coinflips = random.choices([-1, 1], k=int(n.value))
random_process: list = [0]*(len(coinflips)+1)
random_process[0] = float(start.value)
mu = float(drift.value)
var = float(variance.value)
for i, flip in enumerate(coinflips, start=1):
s_0 = random_process[i-1]
s_1 = s_0 * math.e ** (mu - 0.5 * var**2 + var * flip)
random_process[i] = s_1
return random_process
ax.plot(create_random_process())
if update_plot:
plot.update()
def redraw_plot():
global fig, ax, plot, animated
loading.value = True
t1 = time.time()
fig, ax = plt.subplots(1, 1)
plot.set_figure(fig)
for _ in range(int(n_processes.value)):
add_scenario(update_plot=bool(animated.value))
ax.grid(True, alpha=0.3)
ax.set_xlabel('x')
ax.set_ylabel('y')
if not animated.value:
plot.update()
t2 = time.time()
calculation_time.value = round(t2 - t1, 2)
loading.value = False
# This example loads the plot on load.
QToggle.defaults['props'] = {
'color': 'white',
'keep-color': True,
'left-label': True
}
layout = QLayout(events={'load': redraw_plot}, children=[
QHeader([
QToolbar([
QToolbarTitle([
QIcon('insights', 'lg', classes='q-mx-md'),
'Plot demo <small>- Stock simulation by coin flip</small>'
]),
QToggle(label='Interactive', model=interactive),
QToggle(label='Animated', model=animated)
])
]),
QPage([
plot,
v_if(
interactive,
Div(classes='text-center', children=[
"Interactive plot is rendered by ",
Link('mpld3', 'https://mpld3.github.io/')
])
),
v_if(
Not(Or(interactive, loading)),
Div(classes='text-center', children=[
'Non-interactive plot is rendered by matplotlib as png.'
])
)
]),
QDrawer([Rows([
QInput(label='Number of coinflips', model=n),
QInput(label='Number of screnarios', model=n_processes),
QInput(label='Start value (S_0)', model=start),
QInput(label='Drift (μ)', model=drift),
QInput(label='Variance (σ)', model=variance),
QButton(label='Redraw', events={'click': redraw_plot}, props={'loading': loading}),
QButton(label='Add scenario', events={'click': add_scenario}, props={'disable': loading}),
])]),
v_if(
calculation_time,
QFooter(['Plotting took ', calculation_time, ' seconds.'])
)
])
run(layout)