-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2_altair_dashboard.py
132 lines (112 loc) · 3.72 KB
/
2_altair_dashboard.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
# ---
# jupyter:
# jupytext:
# text_representation:
# extension: .py
# format_name: light
# format_version: '1.4'
# jupytext_version: 1.2.1
# kernelspec:
# display_name: Python 3
# language: python
# name: python3
# ---
# # Dashboard with Altair
#
# Resources:
# - https://altair-viz.github.io/gallery/scatter_linked_brush.html
# - https://altair-viz.github.io/gallery/multiple_interactions.html
# - https://altair-viz.github.io/gallery/select_detail.html
# - https://altair-viz.github.io/user_guide/interactions.html
# - https://altair-viz.github.io/user_guide/customization.html
# - https://github.com/altair-viz/altair/issues/1552
# - https://stackoverflow.com/questions/57244390/has-anyone-figured-out-a-workaround-to-add-a-subtitle-to-an-altair-generated-cha
# +
from pathlib import Path
import altair as alt
import pandas as pd
# -
# ## Get and preprocess datasets
#
# - Converting some column types.
# - Clipping some columns to avoid irrelevant outliers.
# - Creating columns for direct plotting.
DATA_DIR = Path("data/")
albums = pd.read_json(DATA_DIR / "albums_features.json")
artists = pd.read_json(DATA_DIR / "artists_features.json")
albums["name"] = albums["name"].astype(str)
albums["release_date"] = pd.to_datetime(albums.release_date)
albums["year"] = albums.release_date.dt.year
albums["decade"] = albums.year.astype(str).str[:3] + "0"
albums["loudness"] = albums.loudness.clip(albums.loudness.quantile(0.05), albums.loudness.quantile(0.95))
# ## Adapt data for simple charts
#
# Join artists to albums to have one unique dataset. This makes Altair interactions much easier.
artists_columns = ["genre_cluster", "genre_specific", "genre_x", "genre_y"]
albums_columns = ["name", "release_date", "popularity", "loudness", "artist_uri", "artist_name"]
data = albums[albums_columns].join(
artists.set_index("uri")[artists_columns],
on="artist_uri", how="inner"
).drop("artist_uri", axis=1)
# ## Charts and interactions
# +
genres_selector = alt.selection_multi(fields=["genre_cluster"])
artists_selector = alt.selection(type="interval")
base_chart = alt.Chart(data)
genres_color = alt.condition(genres_selector, alt.Color("genre_cluster:N", legend=None), alt.value("lightgray"))
genres_points = base_chart.mark_point().encode(
y="genre_cluster:N",
color=genres_color,
).add_selection(genres_selector)
artists_points = base_chart.mark_point().encode(
x=alt.X("mean(genre_x)", axis=None),
y=alt.Y("mean(genre_y)", axis=None),
color=genres_color,
tooltip=["artist_name", "genre_cluster", "genre_specific"],
).add_selection(
artists_selector
)
base_albums_points = base_chart.mark_point().encode(
x="release_date",
y="popularity",
)
albums_points = base_albums_points.encode(
color=alt.condition(
artists_selector,
alt.Color("loudness:Q", scale=alt.Scale(scheme="viridis")),
alt.value("lightgray")
),
)
albums_tooltips = base_albums_points.encode(
opacity=alt.value(0),
tooltip=["artist_name", "name", "release_date"]
).transform_filter(
artists_selector
)
# +
title = alt.Chart(
{"values": [{"text": "Spotify user library explorer"}]}
).mark_text(size=20).encode(
text="text:N",
)
subtitle = alt.Chart(
{"values": [{"text": "Click on a genre to filter artists, select artists to filter albums, albums view is zoomable"}]}
).mark_text(size=14).encode(
text="text:N",
)
# -
chart = alt.vconcat(
title,
subtitle,
alt.hconcat(
genres_points.properties(title="Genres"),
artists_points.properties(title="Artists")
),
(albums_points + albums_tooltips).interactive().properties(title="Albums")
).configure_view(
stroke=None
).configure_concat(
spacing=10
)
chart
chart.save("altair_dashboard.html")