# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Build Week 4 (Python3)
#     language: python
#     name: bw4
# ---

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

df = pd.read_csv('../data/merged_data.csv')

df.head()

# +
id_a = '06w9JimcZu16KyO3WXR459'
id_b = '6XzyAQs2jU4PMWmnFlEQLW'

track_a = df[df['track_id'] == id_a]
track_b = df[df['track_id'] == id_b]

artist_a = track_a['artist_name'].values[0]
artist_b = track_b['artist_name'].values[0]

name_a = track_a['track_name'].values[0]
name_b = track_b['track_name'].values[0]

label_a = f'{artist_a} - {name_a}'[:30]
label_b = f'{artist_b} - {name_b}'[:30]
# -

track_df = pd.concat([track_a, track_b]).drop(columns=['duration_ms', 'popularity'])

vis_labels = [label_a, label_b]

# +
labels = [
    'acousticness',
    'danceability',
    'energy',
    'instrumentalness',
    'liveness',
    'valence'
]

num_vals = len(labels)
angles = [n / float(num_vals) * 2 * np.pi for n in range(num_vals)]
angles += angles[:1]  # make cyclic to connect vertices in polygon

# Set figure settings
fig, ax = plt.subplots(figsize=(6, 6), subplot_kw=dict(polar=True))
ax.set_theta_offset(np.pi / 2)
ax.set_theta_direction(-1)
ax.set_thetagrids(np.degrees(angles), labels)
ax.set_rlabel_position(0)
ax.set_yticks([0.20, 0.40, 0.60, 0.80])
ax.set_yticklabels(['0.20', '0.40', '0.60', '0.80'])
ax.set_ylim(0, 1)

# Plot and fill the radar polygons
feature_df = track_df[labels].reset_index().drop(columns=['index'])
colors = ['#EF019F', '#780150']
for i, color in enumerate(colors):
    values = feature_df.loc[i].values.flatten().tolist()
    values += values[:1]  # make cyclic to connect vertices in polygon
    ax.plot(
        angles,
        values,
        color=color,
        linewidth=1,
        linestyle='solid',
        label=vis_labels[i]
    )
    ax.fill(angles, values, color=color, alpha=0.25)

    # Set feature labels so they don't overlap the chart
    for label, angle in zip(ax.get_xticklabels(), angles):
        if angle in [0, np.pi]:
            label.set_horizontalalignment('center')
        elif 0 < angle < np.pi:
            label.set_horizontalalignment('left')
        else:
            label.set_horizontalalignment('right')
ax.legend(loc='best')
plt.show()
# -



StarcoderdataJupyterScriptsDedupFiltered