-
Notifications
You must be signed in to change notification settings - Fork 1
/
plotly_gauge.py
143 lines (139 loc) · 4.08 KB
/
plotly_gauge.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
import plotly.graph_objects as go
import math
def plotly_bullet(probability):
if probability < 0.00001:
risk_text = 'Very Low Risk'
graph_start = 0
upper_cutoff = 0.00001
lower_cutoff = 0
elif probability < 0.01:
risk_text = 'Low Risk'
graph_start = 0.2
upper_cutoff = 0.01
lower_cutoff = 0.00001
elif probability < 0.1:
risk_text = 'Medium Risk'
graph_start = 0.4
upper_cutoff = 0.1
lower_cutoff = 0.01
elif probability < 5:
risk_text = 'High Risk'
graph_start = 0.6
upper_cutoff = 5
lower_cutoff = 0.1
else:
risk_text = 'Very High Risk'
graph_start = 0.8
upper_cutoff = 100
lower_cutoff = 5
scaled = (probability - lower_cutoff) / (upper_cutoff - lower_cutoff)
log_scaled = math.log((1+scaled), 2)
bullet_value = graph_start + (0.2 * log_scaled)
fig = go.Figure(go.Indicator(
mode = "gauge",
value = bullet_value,
#title = {'text': "Risk Level", 'font': {"size": 12,'color':'black'}},
domain = {'x': [0, 1], 'y': [0, 1]},
gauge = {'shape': "bullet",
'bgcolor': 'white',
'bar': {'color': "black"},
'axis':{'range': [None, 1],
'tickmode':'array',
'tickvals':[0.2,0.4,0.6, 0.8,1],
'ticktext':['0.00001%','0.01%','0.1%','5%','100%'],
'tickfont':{'color':'black'}},
'steps': [
{'range': [0, 0.2], 'color': '#2FCC71', 'name':'Very Low Risk'},
{'range': [0.2, 0.4], 'color': '#1F8449', 'name':'Low Risk'},
{'range': [0.4, 0.6], 'color': '#F4D03F', 'name':'Medium Risk'},
{'range': [0.6, 0.8], 'color': '#F5B041', 'name':'High Risk'},
{'range': [0.8, 1], 'color': '#C03A2B', 'name':'Very High Risk'}],}
))
fig.update_layout({'plot_bgcolor':'white',
'paper_bgcolor':'white'})
fig.add_annotation(
x=0.9,
y=0.9,
xanchor='center',
yanchor='middle',
text="Very High Risk",
hovertext='Hover',
showarrow=False,
font=dict(
color="black",
size=14
)
)
fig.add_annotation(
x=0.7,
y=0.9,
xanchor='center',
yanchor='middle',
text="High Risk",
showarrow=False,
font=dict(
color="black",
size=14
)
)
fig.add_annotation(
x=0.5,
y=0.9,
xanchor='center',
yanchor='middle',
text="Medium Risk",
showarrow=False,
font=dict(
color="black",
size=14
)
)
fig.add_annotation(
x=0.5,
y=2,
xanchor='center',
yanchor='middle',
text=f"Individual Risk Level: {probability:.4f}%",
showarrow=False,
font=dict(
color="black",
size=20
)
)
fig.add_annotation(
x=0.5,
y=1.6,
xanchor='center',
yanchor='middle',
text=f"<i><b>{risk_text}</b> according to <a href='https://digitalhealth.med.brown.edu/news/2020-10-01/mycovidrisk'>Brown University Center for Digital Health</a></i>",
showarrow=False,
font=dict(
color="black",
size=12
)
)
fig.add_annotation(
x=0.3,
y=0.9,
xanchor='center',
yanchor='middle',
text="Low Risk",
showarrow=False,
font=dict(
color="black",
size=14
)
)
fig.add_annotation(
x=0.1,
y=0.9,
xanchor='center',
yanchor='middle',
text="Very Low Risk",
showarrow=False,
font=dict(
color="black",
size=14
)
)
return fig