Customizing and Enhancing Interactivity with Plotly

Topic Overview

Hello! In today's lesson, we're going to explore how to customize charts and enhance interactivity using Plotly. Our goal is to create a detailed line chart that provides insights into Christmas song trends over the years. You'll learn how to add custom hover data, adjust layouts, and export your visualizations in HTML format. By the end of this lesson, you'll have the skills to transform raw data into engaging, interactive charts that communicate trends effectively.

Understanding and Preparing the Data

Before jumping into the visualization, it's essential to understand our dataset — Billboard Christmas Songs. This dataset contains information about songs, their peak positions, and weekly positions throughout the years. We'll use Pandas to prepare this data.

First, we'll aggregate our data to show the number of unique songs, minimum peak position, and average weekly position per year.

import pandas as pd

# Read the dataset
df = pd.read_csv('billboard_christmas.csv')
 
# Group by year and aggregate
yearly_stats = df.groupby('year').agg({
    'song': 'nunique',
    'peak_position': 'min',
    'week_position': 'mean'
}).reset_index()

# Print prepared data
print(yearly_stats)

The output of the above code will be:

    year  song  peak_position  week_position
0   1958     4             12      64.125000
1   1959     6             12      67.000000
2   1960    12             12      60.636364
3   1961     7             12      53.935484
4   1962    10             12      58.384615
5   1963     3             15      53.900000
6   1964     3              7      39.500000
7   1965     1              7      17.200000
8   1966     1             97      98.000000

This output shows that in 1958, there were four unique songs with a minimum peak position of 12 and an average weekly position of 64.1. In 1960, there were 12 unique songs and an average weekly position of 60.6. This code groups our data by year and computes the desired statistics to prepare it for visualization. Aggregating data is vital to show trends clearly in our charts.

Creating the Line Chart with Plotly Express

Next, let's create a simple line chart using Plotly Express, which simplifies the process of making interactive visualizations. We'll plot the number of unique songs per year on the y-axis.

# Create a line chart using Plotly Express
fig = px.line(yearly_stats,
              x='year',
              y='song',
              custom_data=['peak_position', 'week_position'])

With just a few lines of code, we've created a basic line chart. The custom_data parameter allows us to pass additional information to each point in the chart, setting the stage for custom interactions.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal