🍿 2 min. read

Visualizing Data from the Spotify API

Monica Powell

Visualizing Data from the Spotify API

How to quickly visualize stats about Spotify artists with Infogr.am

This post will go over how to connect with the Spotify API to collect information about artists using Python 3 and then create an infographic from the data Spotify returned. Below is a preview of what we will be creating before we dive into the details.

Installing Requests

In order to play around with Spotify’s API in Python to gather data about artists, I installed Requests: HTTP for Humans via the terminal using: $ pip install requests. Note: Requests is a library that can be used to make API calls and is not limited to use only with the Spotify API.

spotify.py

Note: You can explore documentation for the Spotify web API here

1# importing Requests: HTTP for Humans - installed in previous step.
2import requests
1# making a search request to the Spotify API to return
2# any artist with 'lil' in their name. This request is
3# limited to 50 artists in the US market.
4response = requests.get('https://api.spotify.com/v1/search?query=lil&type=artist&limit=50&market=US')
1# converts the response from a string to a json object that can be parsed in Python.
2data = response.json()
1#visit this url: in order to see what the json object that is returned looks like:
2# [https://api.spotify.com/v1/search?query=lil&type=artist&limit=50&market=US](https://api.spotify.com/v1/search?query=lil&type=artist&limit=50&market=US)
1#looking at the 'items' returned for 'artists' in this request.

The Spotify API describes items returned from search as:

1lil_artists = data['artists']['items']
1# iterating through each artist and their items
2# items contains the information we would like to receive for each artist.

artist object (full) (from Spotify API)

1# I use two separate for loops because I want to return ALL of the artist names before returning popularity rankings.
2for artist in lil_artists:
3 # return the name of each artist found with 'search?query=lil&type=artist&limit=50&market=US'
1print(artist['name'])

Note: Spotify’s API will also return artists that had names that formerly matched query. For example: Boosie Badazz, G Herbo and Bow Wow no longer user ‘lil’ in their stage name however, they are included in this dataset.

returns:

1Lil Wayne
2Lil Yachty
3Lil Uzi Vert
4Lil Dicky
5Boosie Badazz
6Lil Jon
7King Lil G
8Lil Durk
9Lil Jon & The East Side Boyz
10Lil Bibby
11G Herbo
12Lil Rob
13Lil Reese
14Lil Keke
15Bow Wow
16Lil Scrappy
17Lil Wyte
18Lil Blood
19Lil Snupe
20Lil Mama
21Lil B
22Lil' Kim
23Lil Cuete
24Lil Phat
25Lil Debbie
26Lil Twist
27Lil Trill
28Lil AJ
29Lil Lonnie
30Lil Goofy
31Mr. Lil One
32Lil Flash
33Lil Kesh
34Lil Haiti
35Lil Silva
36Lil Rue
37Lil Cray
38Lil Eddie
39Lil Wayne, DJ Drama
40Lil Yase
41Lil Suzy
42Lil Mouse
43Lil C
44Lil Rick
45Lil Boom
46Lil June
47Lil E
48Lil Fate
49Lil' Flip
1# returns the popularity ranking associated with each of these artists
2 # view popularity definition above
1for artist in lil_artists:
2 print(artist['popularity'])

returns:

186
272
372
468
567
672
761
860
960
1054
1153
1250
1350
1448
1557
1649
1750
1845
1945
2045
2144
2262
2340
2439
2543
2639
2737
2838
2937
3036
3135
3236
3338
3439
3535
3643
3734
3835
3941
4035
4133
4234
4334
4433
4538
4633
4732
4834
4934
5049

Visualize Data

  1. Create a free Infogr.am account.
  2. Select ‘Chart or Graph’

3. Select the type of chart or graph you would like to create

4. The chart will be created with dummy data. Double click on the chart to pull up an black bar with an ‘edit’ button. Clicking edit will allow you to manipulate the data.

5. Here’s the dummy data that infogr.am automatically includes:

6. Copy and paste the output of:print(artist['name']) andprint(artist['popularity'])

7. The result is a beautiful graphic will all of the data that you pasted into infogr.am. Now you can play around with the visual aesthetic of the graphic.

Example of Infogr.ams I created from this data

This article was published on January 27, 2017.


Don't be a stranger! 👋🏾

Thanks for reading "Visualizing Data from the Spotify API". Join my mailing list to be the first to receive my newest web development content, my thoughts on the web and learn about exclusive opportunities.

     

    I won’t send you spam. Unsubscribe at any time.