mirror of
https://github.com/spotipy-dev/spotipy.git
synced 2026-06-19 01:03:53 +00:00
34 lines
1.1 KiB
Python
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()
|