spotipy/examples/audio_features_analysis.py
2025-01-18 15:21:51 +00:00

34 lines
1.1 KiB
Python

import matplotlib.pyplot as plt
# Import the extra necessary libraries for this example
# These libraries are not included in the default packages
import pandas as pd
import seaborn as sns
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
# Set up Spotify credentials
client_credentials_manager = SpotifyClientCredentials()
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
# Fetch audio features of tracks from any playlist
playlist_id = '37i9dQZEVXbMDoHDwVN2tF'
results = sp.playlist_tracks(playlist_id)
tracks = results['items']
track_ids = [track['track']['id'] for track in tracks]
audio_features = sp.audio_features(track_ids)
# Create a DataFrame of audio features
df = pd.DataFrame(audio_features)
df = df[['danceability', 'energy', 'speechiness', 'acousticness',
'instrumentalness', 'liveness', 'valence', 'tempo']]
# Generate a correlation matrix
correlation_matrix = df.corr()
# Plot the correlation matrix using seaborn
sns.heatmap(correlation_matrix, annot=True, cmap='coolwarm')
# Show the plot
plt.show()