-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_cumulative_change.py
64 lines (53 loc) · 1.78 KB
/
get_cumulative_change.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
import pandas as pd
import plotly.express as px
def get_updated_dataframe(sector):
df = pd.read_json("data/cumulative/" + sector + ".json", typ="dictionary")
df_mod = pd.DataFrame({"date": df.index, "price change": df.values})
df_mod["sector"] = sector
return df_mod
df_healthcare = get_updated_dataframe("Healthcare")
df_technology = get_updated_dataframe("Technology")
df_real_estate = get_updated_dataframe("Real Estate")
df_energy = get_updated_dataframe("Energy")
df_communication_services = get_updated_dataframe("Communication Services")
df_utilities = get_updated_dataframe("Utilities")
df_basic_materials = get_updated_dataframe("Basic Materials")
df_consumer_defensive = get_updated_dataframe("Consumer Defensive")
df_financial = get_updated_dataframe("Financial")
df_industrials = get_updated_dataframe("Industrials")
df_consumer_cyclical = get_updated_dataframe("Consumer Cyclical")
final_df = pd.concat(
[
df_healthcare,
df_technology,
df_real_estate,
df_energy,
df_communication_services,
df_utilities,
df_basic_materials,
df_consumer_defensive,
df_financial,
df_industrials,
df_consumer_cyclical,
]
)
final_df["month"] = final_df["date"].dt.strftime("%b %Y")
print(final_df)
# generate the plot
fig = px.bar(
final_df,
x="price change",
y="sector",
text="price change",
orientation="h",
animation_frame="month",
)
# a bit of formatting...
fig = fig.update_traces(texttemplate="%{text:4s}")
for f in fig.frames:
for t in f["data"]:
t["texttemplate"] = "%{text:4s}"
fig.update_layout(xaxis={"range": [-10, final_df["price change"].max() * 1.25]})
fig.update_layout(font_size=14)
# fig.update_layout(yaxis={'categoryorder':'total ascending'})
fig.show()