Splotify Tutorial and Examples
View this Jupyter Notebook on Colab with the interactive Plotly plots here.
Setup
Install Splotify:
[ ]:
!pip install splotify
Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/
Requirement already satisfied: splotify in /usr/local/lib/python3.9/dist-packages (0.1.0)
Requirement already satisfied: plotly>=5.13.1 in /usr/local/lib/python3.9/dist-packages (from splotify) (5.13.1)
Requirement already satisfied: pandas>=1.2.4 in /usr/local/lib/python3.9/dist-packages (from splotify) (1.4.4)
Requirement already satisfied: spotipy>=2.22.1 in /usr/local/lib/python3.9/dist-packages (from splotify) (2.22.1)
Requirement already satisfied: tabulate>=0.9.0 in /usr/local/lib/python3.9/dist-packages (from splotify) (0.9.0)
Requirement already satisfied: tqdm>=4.64.0 in /usr/local/lib/python3.9/dist-packages (from splotify) (4.65.0)
Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.9/dist-packages (from pandas>=1.2.4->splotify) (2022.7.1)
Requirement already satisfied: python-dateutil>=2.8.1 in /usr/local/lib/python3.9/dist-packages (from pandas>=1.2.4->splotify) (2.8.2)
Requirement already satisfied: numpy>=1.18.5 in /usr/local/lib/python3.9/dist-packages (from pandas>=1.2.4->splotify) (1.22.4)
Requirement already satisfied: tenacity>=6.2.0 in /usr/local/lib/python3.9/dist-packages (from plotly>=5.13.1->splotify) (8.2.2)
Requirement already satisfied: urllib3>=1.26.0 in /usr/local/lib/python3.9/dist-packages (from spotipy>=2.22.1->splotify) (1.26.15)
Requirement already satisfied: redis>=3.5.3 in /usr/local/lib/python3.9/dist-packages (from spotipy>=2.22.1->splotify) (4.5.4)
Requirement already satisfied: requests>=2.25.0 in /usr/local/lib/python3.9/dist-packages (from spotipy>=2.22.1->splotify) (2.27.1)
Requirement already satisfied: six>=1.15.0 in /usr/local/lib/python3.9/dist-packages (from spotipy>=2.22.1->splotify) (1.16.0)
Requirement already satisfied: async-timeout>=4.0.2 in /usr/local/lib/python3.9/dist-packages (from redis>=3.5.3->spotipy>=2.22.1->splotify) (4.0.2)
Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.9/dist-packages (from requests>=2.25.0->spotipy>=2.22.1->splotify) (3.4)
Requirement already satisfied: charset-normalizer~=2.0.0 in /usr/local/lib/python3.9/dist-packages (from requests>=2.25.0->spotipy>=2.22.1->splotify) (2.0.12)
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.9/dist-packages (from requests>=2.25.0->spotipy>=2.22.1->splotify) (2022.12.7)
Getting Spotify Authorization
To use Splotify you must first get your SPOTIPY_CLIENT_ID, SPOTIPY_CLIENT_SECRET, and redirect uri. Navigate to your Spotify devloper dashboard, log in with your Spotify account, and create your own app. Here is a tutorial created by Spotipy that explains how to do so.
[ ]:
import splotify.spotifyapi as spotifyapi
import spotipy
from spotipy.oauth2 import SpotifyOAuth
# replace these fields with your own client id, client secret, and redirect URI
sp = spotifyapi.SpotifyApi(
"YOUR SPOTIPY_CLIENT_ID",
"YOUR SPOTIPY_CLIENT_SECRET",
"YOUR REDIRECT URI",
)
# include this for browserless environments
sp.sp = spotipy.Spotify(
auth_manager=SpotifyOAuth(scope=scope, open_browser=False)
)
Find and add tracks to plot
Look up Spotify URIs
[ ]:
import splotify.helpers as helpers
# look up the id of a song called "All These Things That I've Done"
ids = helpers.search_id(sp, "All These Things That I've Done", limit=1)
# look up the ids of 5 Tchaikovsky Violin Concerto recordings
ids = helpers.search_id(sp, "Tchaikovsky Violin Concerto", limit=5, type="track")
# look up the ids of 3 artists called The Sundays
ids = helpers.search_id(sp, "The Sundays", limit=3, type="artist")
# look up the ids of 5 Fall Out Boy albums
ids = helpers.search_id(sp, "Fall Out Boy", type="album")
# look up the ids of 15 Queen playlists
ids = helpers.search_id(sp, "queen", limit=15, type="playlist")
Name Album Artists URI
------------------------------- -------- --------------- ------------------------------------
All These Things That I've Done Hot Fuss ['The Killers'] spotify:track:5vollujufHY0jMZxx77VWr
Name Album Artists URI
--------------------------------------------------------------------------------- ---------------------------------------------------- -------------------------------------------------------------------------------------------------------- ------------------------------------
Violin Concerto In D Major, Op. 35, TH.59: I. Allegro moderato Higdon / Tchaikovsky: Violin Concertos ['Pyotr Ilyich Tchaikovsky', 'Hilary Hahn', 'Royal Liverpool Philharmonic Orchestra', 'Vasily Petrenko'] spotify:track:5yyWkqPnm6pXSLmKXbW6dB
Tchaikovsky: Violin Concerto in D Major, Op. 35: I. Allegro moderato Tchaikovsky: Violin Concerto & Sérénade mélancolique ['Pyotr Ilyich Tchaikovsky', 'Itzhak Perlman', 'Eugene Ormandy', 'Philadelphia Orchestra'] spotify:track:0V6V6fwKxTHDrsLLmgW432
Tchaikovsky: Violin Concerto in D Major, Op. 35: II. Canzonetta. Andante Tchaikovsky: Violin Concerto & Sérénade mélancolique ['Pyotr Ilyich Tchaikovsky', 'Itzhak Perlman', 'Eugene Ormandy', 'Philadelphia Orchestra'] spotify:track:63hFiPa44P3r9nM7kerjhT
Tchaikovsky: Violin Concerto in D Major, Op. 35: III. Finale. Allegro vivacissimo Tchaikovsky: Violin Concerto & Sérénade mélancolique ['Pyotr Ilyich Tchaikovsky', 'Itzhak Perlman', 'Eugene Ormandy', 'Philadelphia Orchestra'] spotify:track:5cS7BXwXpBP0N4CQ5DmuLl
Tchaikovsky: Violin Concerto in D Major, Op. 35: I. Allegro moderato Violin Concerto/ Serenade Melancolique - Tchaikovsky ['Pyotr Ilyich Tchaikovsky', 'Itzhak Perlman', 'Eugene Ormandy', 'Philadelphia Orchestra'] spotify:track:6ppu8ECkNhZIqQAqbP7u5o
Name URI
--------------------- -------------------------------------
The Sundays spotify:artist:4nlqDmbzFzbNITaqjJv7D7
Sarah and the Sundays spotify:artist:1vnh2cDW2CgA5M1ya95mZG
The Sunday Shakes spotify:artist:3v8xBzqxJg4K2GkpiPUtqG
Name Artists URI
------------------------------- ---------------- ------------------------------------
So Much (For) Stardust ['Fall Out Boy'] spotify:album:5mWnMYLnfcnkDOCojHW6O1
American Beauty/American Psycho ['Fall Out Boy'] spotify:album:022DrG7Wp2PSCwzuD0bSzT
From Under The Cork Tree ['Fall Out Boy'] spotify:album:5nkUSlIhtoJZMOUlB0sNCp
Save Rock And Roll ['Fall Out Boy'] spotify:album:5jKMfS57mHTHzlSFGfPFxU
Save Rock And Roll ['Fall Out Boy'] spotify:album:0EVJX4RlYKuApsAN5CaDa3
MANIA ['Fall Out Boy'] spotify:album:16xtTZvPH6PXcwSqYqJHHG
Infinity On High ['Fall Out Boy'] spotify:album:0hHopYqXhuvYSHtVyrcb1g
Folie à Deux ['Fall Out Boy'] spotify:album:6KOWjVP0mh5rOqmzm4tkPD
Take This to Your Grave ['Fall Out Boy'] spotify:album:7l4nnHG7upOnUM4WvAjToY
Love From The Other Side ['Fall Out Boy'] spotify:album:6zhsE8Z0CfH68FSjxJn0dD
Name Owner URI
--------------------------------------------------------- ------------------ ---------------------------------------
This Is Queen Spotify spotify:playlist:37i9dQZF1DZ06evO0ENBD2
Queen: Best Of The Best Best Of The Best spotify:playlist:4khPeG4MVX8uIU4rSHHCy5
Queen: Greatest Hits I II & III - The Platinum Collection mechag2018 spotify:playlist:1A7vQbLARTURqeJSuf36vp
Queen/Elton john/ Billy Joel and such Gabby Harmon-Welch spotify:playlist:4wtr200vqdiEP0vsvdpSeI
Queen Best Of Queen spotify:playlist:595cY5S1FpMpiEIWsk6dG7
QUEEN Seth Ginsberg spotify:playlist:1Hvk32EnBpZ71BwlyhlmCM
Queen Radio Spotify spotify:playlist:37i9dQZF1E4vJDb4S6o3EK
Queen of the Damned (Soundtrack) Ricardo Sanfelice spotify:playlist:6qi3cwYhrp1IHjDfH8lYiY
Queen and Adam Lambert: The Complete Collection Bedwellj101 spotify:playlist:31gjkI5DAEIfukUmhxeZpb
Queens of the Stone Age Radio Spotify spotify:playlist:37i9dQZF1E4ufUjPgo929F
Queen Live Spotify spotify:playlist:37i9dQZF1DWYl8YgN0qYsf
This Is Queens of the Stone Age Spotify spotify:playlist:37i9dQZF1DZ06evO2zVDhu
Queens of Disco Spotify spotify:playlist:37i9dQZF1DXdJyxIBq7Xdy
Lana Del Rey - Say Yes to Heaven Hyder spotify:playlist:3dnSFxqe8YUI3B5EvLYFCV
Queen Spotify spotify:playlist:37i9dQZF1DWSIO2QWRavWZ
If you want to view the Spotify URIs of your personal playlists, use the my_id() function instead.
Add the tracks you want to a Data object
In addition to using URIs, you can also use Spotify URLs and Spotify IDs. According to the Spotipy doccumentation here:
Spotify URI - The resource identifier that you can enter, for example, in the Spotify Desktop client’s search box to locate an artist, album, or track. Example:
spotify:track:6rqhFgbbKwnb9MLmUQDhG6Spotify URL - An HTML link that opens a track, album, app, playlist or other Spotify resource in a Spotify client. Example:
http://open.spotify.com/track/6rqhFgbbKwnb9MLmUQDhG6Spotify ID - A base-62 number that you can find at the end of the Spotify URI (see above) for an artist, track, album, etc. Example:
6rqhFgbbKwnb9MLmUQDhG6
[ ]:
import splotify.data as data
d = data.Data(sp)
# add "Changing of the Seasons" by Two Door Cinema using Spotify URLs
d.add_track("https://open.spotify.com/track/3mrlQc1K6sPmoBY1Pv46Ab?si=07b2e536c7f047a9")
# add "All These Things That I've Done" by The Killers and Hilary Hahn's recording of Tchaikovsky's violin concerto using Spotify IDs
d.add_tracks(["5vollujufHY0jMZxx77VWr", "5yyWkqPnm6pXSLmKXbW6dB"])
# add "Comic and Cosmic" by PinocchioP using Spotify URLs
d.add_album("https://open.spotify.com/album/3Ydz6UhhXxgsoj36DKSv9L?si=Cd6jjopmQZeb_niwjvACow")
# add "So Much (For) Stardust", "From Under The Cork Tree", and "Infinity On High" by Fall Out Boy using Spotify URIs
d.add_albums(["spotify:album:5mWnMYLnfcnkDOCojHW6O1", "spotify:album:5nkUSlIhtoJZMOUlB0sNCp","spotify:album:0hHopYqXhuvYSHtVyrcb1g"])
# add "This is Queen" playlist by Spotify using Spotify URIs
d.add_playlist("spotify:playlist:37i9dQZF1DZ06evO0ENBD2")
# add "This is Day6" and "This is Radiohead" playlists by Spotify using Spotify URLs
d.add_playlists(["https://open.spotify.com/playlist/37i9dQZF1DX3Ucx6LaTP5S?si=a8e21e0cc2284f4b", "https://open.spotify.com/playlist/37i9dQZF1DZ06evO2VxlyE?si=455ba91f17504c8f"])
Plotting audio features
[ ]:
import splotify.plots.audiofeatures as af
afp = af.AudioFeaturesPlot(sp, d, ["energy", "loudness"])
# plot a simple scatter plot of the data by their energy and loudness features
plt = afp.scatter_plot_2d()
Expected output (2D scatter plots)
[ ]:
# select danceability as a 3rd feature to plot by
afp.select_features(["energy", "loudness", "danceability"])
# plot a 3d scatter plot of the data, colored by their albums
plt = afp.scatter_plot_3d(color="album")
Expected output (3D scatter plots)
[ ]:
# plot the average audio features of the tracks by artist
plt = afp.scatter_plot_2d_average(groupby="artist")
plt = afp.scatter_plot_3d_average(groupby="artist")
Expected output (average scatter plots)
[ ]:
# plot a histogram of the tracks' energy, both as a whole and by artist
plt = afp.histogram("energy")
plt = afp.histogram("energy", color="artist")
Expected output (histograms)
[ ]:
plt = afp.box_plot("energy")
plt = afp.box_plot("energy", groupby="artist")
Expected output (box plots)
Plotting categories
[ ]:
import splotify.plots.category as c
cp = c.CategoryPlot(d)
plt = cp.bar_chart(groupby="artist")
plt = cp.pie_chart(groupby="artist")