-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot.py
More file actions
executable file
·78 lines (65 loc) · 1.79 KB
/
Copy pathplot.py
File metadata and controls
executable file
·78 lines (65 loc) · 1.79 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
#!/usr/bin/python3
import pandas as pd
import plotly.express as px
COL_YEAR = "Year"
COL_SPEED_ORIG = "Speed [Gbps]"
COL_SPEED = "Speed (symmetric)"
COL_PRICE = "Price per month [Fr.]"
COL_PRODUCT = "Product"
COL_LINK = "Link"
def main():
data = pd.read_csv("internet-prices.csv")
# Convert to string so that plotly treats it as categories (instead of continuous numerical data)
data[COL_SPEED] = data[COL_SPEED_ORIG].astype(str) + " Gbps"
print(data)
# Plot
fig = px.scatter(
data,
x=COL_YEAR,
y=COL_PRICE,
color=COL_SPEED,
text=COL_PRODUCT,
hover_data=[COL_PRODUCT, COL_SPEED, COL_LINK],
title="Internet Prices over Time (Switzerland)",
width=1000,
height=600,
)
fig.update_traces(
textposition="top center",
marker_size=12,
)
fig.update_layout(
plot_bgcolor="white",
)
fig.update_xaxes(
gridcolor="lightgrey",
)
fig.update_yaxes(
gridcolor="lightgrey",
)
# Add prices below data points
for i, row in data.iterrows():
fig.add_annotation(
x=row[COL_YEAR],
y=row[COL_PRICE],
text=f'<a href="{row[COL_LINK]}" target="_blank">{row[COL_PRICE]} Fr.</a>',
yshift=-20,
showarrow=False,
)
# Add source link
fig.add_annotation(
text='<a href="https://github.com/thgoebel/internet-prices" target="_blank">github.com/thgoebel/internet-prices</a>',
xref="paper",
yref="paper",
x=1.05,
y=-0.11,
showarrow=False,
xanchor="center",
yanchor="bottom",
)
# Export
fig.write_html("internet-prices.html")
fig.write_image("internet-prices.png")
fig.show()
if __name__ == "__main__":
main()