-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlineage-plotting-one.py
More file actions
143 lines (129 loc) · 3.51 KB
/
Copy pathlineage-plotting-one.py
File metadata and controls
143 lines (129 loc) · 3.51 KB
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
import plotly.express as px
import pandas as pd
from datetime import datetime as dt
import pandas as pd
from datetime import datetime as dt
# Load the processed weekly data
lineage1_perc = pd.read_csv("https://blobserver.dc.scilifelab.se/blob/lineage-cleaned-data.csv")
# Now need to format dates in a manner recognisable to plotly
lineage1_perc["year"] = (lineage1_perc["Year-Week"].str[:4]).astype(int)
lineage1_perc["week_no"] = lineage1_perc["Year-Week"].str[-3:]
lineage1_perc["week_no"] = (
lineage1_perc["week_no"].str.replace("-", "", regex=True)
).astype(int)
# set the date to the start of the week (Monday)
lineage1_perc["day"] = 1
lineage1_perc["date"] = lineage1_perc.apply(
lambda row: dt.fromisocalendar(row["year"], row["week_no"], row["day"]), axis=1
)
# sort values to ensure that the traces show in the desired order.
lineage1_perc.sort_values(by=["lineage_groups01"], ascending=False, inplace=True)
colours = [
"#D30000",
"#151B54",
"#FFF200",
"#B200ED",
"#8D021F",
"#0000FF",
"#FCD12A",
"#784B84",
"#FF2400",
"#1E90FF",
"#D5B85A",
"#81007F",
"#CD5C5C",
"#79BAEC",
"#CC7722",
"#DE73FF",
"#421310",
"#87AFC7",
"#EFFD5F",
"#7852A9",
"#BF0A30",
]
fig = px.area(
lineage1_perc,
x="date",
y="percentage_lineage1",
color="lineage_groups01",
line_group="lineage_groups01",
color_discrete_map={
lineage1_perc.lineage_groups01.unique()[i]: colours[i]
for i in range(len(lineage1_perc.lineage_groups01.unique()))
},
hover_data={
"percentage_lineage1": ":.2f",
},
)
fig.update_layout(
title=" ",
yaxis={
"title": "<b>Percentage of Lineages<br></b>",
"ticktext": [" ", "20%", "40%", "60%", "80%", "100%"],
"tickvals": ["0", "20", "40", "60", "80", "100"],
"range": [0, 100],
},
font={"size": 12},
autosize=True,
margin=dict(r=0, t=100, b=120, l=0),
# showlegend=False,
legend=dict(
yanchor="top",
y=1.0,
xanchor="left",
x=1.01,
font=dict(size=12),
title="<b>Lineage</b><br>",
traceorder="reversed",
),
hovermode="x unified",
xaxis={
"title": "<b>Date</b>",
"tickangle": 0,
"hoverformat": "%b %d, %Y (week %W)",
},
)
fig.update_traces(hovertemplate="%{y:.2f}%")
for i in range(len(fig["data"])):
fig["data"][i]["line"]["width"] = 0
# Buttons
fig.update_layout(
updatemenus=[
dict(
buttons=list(
[
dict(
label="Select all lineages",
method="update",
args=[
{
"visible": [
True,
]
},
],
),
dict(
label="Deselect all lineages",
method="update",
args=[
{"visible": "legendonly"},
],
),
]
),
type="buttons",
# direction="right",
pad={"r": 0, "t": 15},
showactive=True,
x=0.98,
xanchor="left",
y=1.23,
yanchor="top",
),
]
)
# fig.show()
# Prints as a json file
# fig.write_json("lineage_one_wholetime.json")
print(fig.to_json())