This report looks at a high popularity Spotify dataset in order to examine patterns involving song characteristics, artists, genres, and release dates. The goal is to better understand how these song characteristics, such as danceability, energy, and popularity relate to one another, which artists and genres appear most often in the dataset, and how the songs are distributed throughout the year.
The dataset contains 1,686 popular Spotify songs and includes variables such as track artist, playlist genre, track popularity, danceability, energy, tempo, and album release date. This allows for comparisons between song characteristics, artists, genres, and release timing.
This section presents five visualizations examining different patterns in the Spotify dataset.
This visualization compares the danceability and energy of the songs in the dataset. Each point represents one song. The color of each point represents track popularity, while the size of the point also increases with popularity. This shows that most of the popular songs are concentrated in the medium-high range for both danceability and energy.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
path = "/Users/rohan/Downloads/"
filename = "high_popularity_spotify_data.csv"
spotify = pd.read_csv(path + filename)
spotify['track_album_release_date'] = pd.to_datetime(spotify['track_album_release_date'], format = 'mixed')
spotify['Year'] = spotify['track_album_release_date'].dt.year
spotify['Month'] = spotify['track_album_release_date'].dt.month
spotify['MonthName'] = spotify['track_album_release_date'].dt.strftime('%B')
scatter_df = spotify[['danceability', 'energy', 'track_popularity']]
scatter_df['popularity_size'] = scatter_df['track_popularity'] * 2
scatter_df = scatter_df.reset_index(drop = True)
plt.figure(figsize = (18,10))
plt.scatter(scatter_df['danceability'], scatter_df['energy'], marker='8', cmap = 'viridis',
c = scatter_df['track_popularity'], s = scatter_df['popularity_size'], edgecolors = 'black')
plt.title('Danceability and Energy of Popular Spotify Songs', fontsize=18)
plt.xlabel('Danceability', fontsize = 14)
plt.ylabel('Energy', fontsize = 14)
cbar = plt.colorbar()
cbar.set_label('Track Popularity', rotation = 270, fontsize = 14, color = 'black', labelpad = 30)
plt.show()
This bar chart shows the top ten most common playlist genres in the high popularity Spotify dataset. The bars are color coded based on whether the song counts are above, below, or within 1% of the average count of the top 10 genres. Pop, rock, hip-hop, latin, and electronic were above this average, while the rest remained below it, showing that the songs in the dataset are concentrated more heavily in a few major genres, which could be a product of the dataset only including 1,686 popular songs.
genre_counts = spotify.groupby('playlist_genre').size().reset_index(name = 'Count')
genre_counts = genre_counts.sort_values('Count', ascending = False)
genre_counts = genre_counts.reset_index(drop = True)
def pick_colors_according_to_mean_count(this_data):
colors = []
avg = this_data.Count.mean()
for each in this_data.Count:
if each > avg*1.01:
colors.append('lightcoral')
elif each < avg*0.99:
colors.append('green')
else:
colors.append('black')
return colors
bottom1 = 0
top1 = 9
d1 = genre_counts.loc[bottom1:top1]
my_colors1 = pick_colors_according_to_mean_count(d1)
d1
Above = mpatches.Patch(color = 'lightcoral', label = 'Above Average')
At = mpatches.Patch(color = 'black', label = 'Within 1% of the Average')
Below = mpatches.Patch(color = 'green', label = 'Below Average')
plt.figure(figsize = (18,10))
plt.bar(d1.playlist_genre, d1.Count, color = my_colors1)
plt.legend(handles = [Above, At, Below], fontsize = 14)
plt.axhline(d1.Count.mean(), color = 'black', linestyle = 'dashed')
plt.title('Top 10 Playlist Genres in the High Popularity Spotify Dataset', fontsize = 18)
plt.ylabel('Song Count', fontsize = 14)
plt.xticks(rotation = 45, ha = 'right')
plt.show()
This horizontal bar chart shows the ten artists that appear most frequently in the dataset. Bad Bunny appears the most with 20 songs, followed by Linkin Park with 14. The horizontal format makes it easier to read the artist names while comparing their song counts.
artist_counts = spotify.groupby('track_artist').size().reset_index(name = 'Count')
artist_counts = artist_counts.sort_values('Count', ascending = False)
artist_counts = artist_counts.reset_index(drop = True)
artist_counts
bottom3 = 0
top3 = 9
d3 = artist_counts.loc[bottom3:top3]
d3 = d3.sort_values('Count', ascending = True)
d3.reset_index(inplace = True, drop = True)
my_colors3 = pick_colors_according_to_mean_count(d3)
d3
fig = plt.figure(figsize = (18,12))
ax1 = fig.add_subplot(1, 1, 1)
ax1.barh(d3.track_artist, d3.Count, color = my_colors3)
for row_counter, value_at_row_counter in enumerate(d3.Count):
ax1.text(value_at_row_counter+0.5, row_counter, str(value_at_row_counter), color = 'black', size = 12, fontweight = 'bold')
plt.xlim(0, d3.Count.max()*1.1)
ax1.set_title('Top 10 Artists', size = 20)
ax1.set_xlabel('Song Count', fontsize = 16)
ax1.set_ylabel('Artist', fontsize = 16)
plt.xticks(fontsize = 14)
plt.yticks(fontsize = 14)
plt.show()
This stacked bar chart compares the number of songs released each month across different playlist genres. January has the largest overall number of songs in the dataset, while February has one of the smaller totals. The chart also shows that the overall genre composition varies from month to month, with pop, rock, and hip-hop contributing different amounts throughout the year.
stacked_df = spotify.groupby(['Month', 'playlist_genre']).size().reset_index(name = 'Count')
stacked_df
stacked_df = stacked_df.pivot(index = 'Month', columns = 'playlist_genre', values = 'Count')
stacked_df
stacked_df = stacked_df.fillna(0)
stacked_df
fig = plt.figure(figsize = (18,10))
ax = fig.add_subplot(1, 1, 1)
stacked_df.plot(kind = 'bar', stacked = True, ax=ax)
plt.ylabel('Song Count', fontsize = 18, labelpad = 10)
plt.title('Spotify Songs by Month and Genre\nStacked Bar Plot', fontsize = 18)
plt.xticks(rotation = 0, horizontalalignment = 'center', fontsize = 14)
plt.yticks(fontsize = 14)
ax.set_xlabel('Month', fontsize = 18)
plt.show()
This donut chart shows how the songs in the dataset are distributed across the four quarters of the year. The distribution is very even, with each quarter representing about one quarter of the songs. Quarter 3 has the highest number of songs with 435, whereas Quarter 2 has the lowest with 411 songs.
spotify['Quarter'] = 'Quarter ' + spotify.track_album_release_date.dt.quarter.astype('string')
pie_df = spotify.groupby(['Quarter', 'MonthName']).size().reset_index(name = 'SongCount')
pie_df
number_outside_colors = len(pie_df.Quarter.unique())
outside_color_ref_number = np.arange(number_outside_colors)*4
print(outside_color_ref_number)
fig = plt.figure(figsize = (10,10))
ax = fig.add_subplot(1, 1, 1)
colormap = plt.get_cmap("tab20c")
outer_colors = colormap(outside_color_ref_number)
all_songs = pie_df.SongCount.sum()
pie_df.groupby(['Quarter'])['SongCount'].sum().plot(
kind = 'pie', radius = 1, colors = outer_colors, pctdistance = 0.85, labeldistance = 1.1,
wedgeprops = dict(edgecolor = 'w'), textprops = {'fontsize':18},
autopct = lambda p: '{:.2f}%\n({:.0f} songs)'.format(p, (p/100)*all_songs),
startangle = 90)
hole = plt.Circle((0,0), 0.3, fc = 'white')
fig1 = plt.gcf()
fig1.gca().add_artist(hole)
ax.yaxis.set_visible(False)
plt.title('Spotify Songs by Quarter', fontsize = 18)
ax.text(0, 0, 'Total Songs\n' + str(all_songs), size = 18, ha = 'center')
ax.axis('equal')
plt.tight_layout()
plt.show()
Overall, the dataset shows many clear patterns among popular Spotify songs. Many songs have high danceability and energy, while a small group of genres such as pop, rock, and hip-hop appear most frequently. A few artists also repeatedly appear in the dataset, such as Linkin Park and Juice WRLD. Release timing is fairly balanced across the four quarters of the year, however, the number and genre composition of songs vary by each month. In conclusion, these five visualizations show that popular Spotify songs share common audio characteristics while still covering a wide range of artists, genres, and release dates.