Lesson

Treemaps and sunburst

Learn Treemaps and sunburst in SQLPad's Data Science in Action course with practical examples and guided lessons.

Welcome to the lesson on Treemaps and Sunburst in the chapter Advanced Charts with Plotly. In this lesson, we will dive deep into the world of hierarchical data visualization using Plotly's Treemap and Sunburst charts. These charts are excellent for visualizing large datasets with multiple levels of hierarchy, helping you to easily identify patterns, trends, and outliers. We will also cover customization options, such as color scale, labels, and interactions, to make your charts more insightful and engaging.

Creating a Basic Treemap with Plotly

In this code example, you will learn how to create a basic treemap using Plotly. We will use the built-in dataset from Plotly to create a pandas dataframe and then construct a treemap chart.

import plotly.express as px

df = px.data.gapminder().query("year == 2007")
print(df.head())

Constructing a Basic Treemap Chart with Plotly

fig = px.treemap(
    df,
    path=['continent', 'country'],
    values='pop',
    color='lifeExp',
    hover_data=['iso_alpha'],
    color_continuous_scale='RdBu',
    title='Treemap of Population by Continent and Country (2007)',
)

fig.show()

Customizing Treemap Colors and Labels

In this code example, we will customize the colors and labels of a treemap using Plotly and a built-in dataset from Plotly.

First, let's import the required libraries and prepare the data using a built-in dataset from Plotly.

import plotly.express as px
import pandas as pd

# Load built-in dataset
df = px.data.gapminder()

# Filter the data to year 2007
df = df[df['year'] == 2007]

# Print the first 5 rows of the dataframe
df.head()

Constructing the Treemap

Now that we have our data, let's create a treemap and customize its colors and labels.

# Create a treemap
fig = px.treemap(df, 
                 path=['continent', 'country'], 
                 values='pop',
                 color='gdpPercap',
                 color_continuous_scale='RdYlGn',
                 title='Treemap of Population and GDP per Capita in 2007',
                 labels={'continent':'Continent', 'country':'Country', 'pop':'Population', 'gdpPercap':'GDP per Capita'})

# Update treemap labels
fig.update_traces(textinfo='label+value')

# Show the treemap
fig.show()

Try running these code blocks one by one to see the customized treemap with colors and labels.

Adding Interactivity to Treemaps

In this code example, we will add interactivity to a Treemap using Plotly and a built-in dataset from Plotly. We will break down the code into two blocks: creating the DataFrame and constructing the interactive Treemap.

First, we will create a DataFrame using the built-in dataset from Plotly.

import plotly.express as px

# Load the built-in dataset and create a DataFrame
data = px.data.tips()
df = data.copy()

# Display the first few rows of the DataFrame
df.head()

Constructing the Interactive Treemap

Now that we have our DataFrame, we will construct the interactive Treemap using Plotly.

# Create an interactive Treemap
fig = px.treemap(df, 
                 path=['day', 'time', 'sex'], 
                 values='total_bill', 
                 color='total_bill',
                 hover_data=['tip'],
                 color_continuous_scale='Viridis',
                 title="Treemap of Tips Dataset")

# Display the Treemap
fig.show()

By executing these two code blocks, you will create an interactive Treemap visualization of the tips dataset, where you can hover over each section to see the tip amount and other details.

Creating a Basic Sunburst Chart with Plotly

In this code example, we will create a basic sunburst chart using Plotly. We will use the built-in dataset px.data.gapminder() from Plotly Express for creating the chart.

First, let's import the required libraries and load the dataset into a pandas dataframe.

import plotly.express as px
import pandas as pd

data = px.data.gapminder()
df = data.query("year == 2007")
print(df.head())

Creating the sunburst chart

Now that we have loaded the dataset, we can create a sunburst chart using the px.sunburst() function from Plotly Express.

fig = px.sunburst(df, 
                  path=['continent', 'country'], 
                  values='pop', 
                  color='lifeExp', 
                  color_continuous_scale='RdBu', 
                  title='Sunburst Chart of Population by Continent and Country in 2007',
                  template='plotly_dark')

fig.show()

This will create a basic sunburst chart with the population of each country grouped by continent, and the color of each segment representing the life expectancy in that country.

Adding Interactivity to Sunburst Charts

In this code example, we will be adding interactivity to a sunburst chart using Plotly. We will be using Plotly's built-in dataset of tips.

First, let's create a pandas dataframe.

import pandas as pd
import plotly.express as px

# Load the built-in tips dataset
df = px.data.tips()

# Print the first 5 rows of the dataset
print(df.head())

Now that we have our data, let's create an interactive sunburst chart.

# Create the sunburst chart
fig = px.sunburst(df, path=['day', 'time', 'sex'], values='total_bill', color='size', hover_data=['tip'],
                 color_continuous_scale='Viridis', title='Sunburst Chart of Tips')

# Add interactivity to the chart
fig.update_layout(hovermode='closest', margin=dict(t=50, b=0, l=0, r=0))

# Show the chart
fig.show()

Comparing Treemaps and Sunburst Charts

In this code example, we will be comparing Treemaps and Sunburst Charts using the plotly library and a built-in dataset from plotly.express.

Step 1: Import libraries and load the dataset

First, we will import the required libraries and load the built-in dataset for our charts.

import plotly.express as px
import pandas as pd

# Load the built-in dataset
df = px.data.gapminder().query("year == 2007")
print(df.head())

Step 2: Create a Treemap chart

Next, we will create a Treemap chart using the loaded dataset and display it.

fig = px.treemap(df, path=['continent', 'country'], values='pop',
                 color='lifeExp', hover_data=['iso_alpha'],
                 color_continuous_scale='RdBu', title="Treemap Chart")
fig.show()

Step 3: Create a Sunburst chart

Finally, we will create a Sunburst chart using the same dataset and display it.

fig = px.sunburst(df, path=['continent', 'country'], values='pop',
                  color='lifeExp', hover_data=['iso_alpha'],
                  color_continuous_scale='RdBu', title="Sunburst Chart")
fig.show()

Exercises

1. Treemaps and Sunburst

Instruction

Create a Sunburst chart using the gapminder dataset to visualize the GDP per Capita and Population in 2007. The chart should have the following specifications:

  1. Use the px.sunburst function to create the chart.
  2. Set the path argument to specify the hierarchy of the data (continent, then country).
  3. Set the values argument to define the size of the segments based on the population.
  4. Set the color of the segments based on the GDP per capita using the color argument.
  5. Add life expectancy as hover data using the hover_data argument.
  6. Use the color_continuous_scale argument to set the color scale to 'RdYlGn'.
  7. Add a title to the chart using the title argument.
  8. Finally, display the chart using the fig.show() method.

My Solution

# Your solution goes here

Hint

Start by importing the plotly.express module as px. Then, filter the gapminder dataset to only include data from the year 2007. Use the px.sunburst function to create the chart with the specified arguments, and finally display the chart using the fig.show() method.

Solution

import plotly.express as px

data = px.data.gapminder().query("year == 2007")
fig = px.sunburst(data, path=['continent', 'country'], values='pop',
                  color='gdpPercap', hover_data=['lifeExp'],
                  color_continuous_scale='RdYlGn',
                  title='Sunburst of GDP per Capita and Population in 2007')
fig.show()