How to Create a Treemap and Sunburst Chart in Python With Plotly
Treemaps and sunburst charts show a hierarchy by area. With Plotly Express, one call builds either chart from an Excel table: here continent, region and country, sized by sales and colored from red to green by profit margin. The chart is saved as an interactive HTML file, and rerunning the script picks up any change in the workbook.

How the chart reads
Each rectangle is a country, nested inside its region and continent. The size of a rectangle is the country’s sales, and its color is the profit margin: red for low, green for high. Click a rectangle to zoom into that level, and hover over it to see the values and the remark from the workbook.
Install the packages
You need plotly for the chart, pandas to read the workbook, and openpyxl, which pandas uses for .xlsx files. Plotly Express ships inside the plotly package, so there is nothing else to install.
pip install plotly pandas openpyxl
The Excel data
data.xlsx has one row per country with the columns Country, Continent, Region, Sales, Profit Margin % and an optional Remark. The three text columns form the hierarchy, Sales sets the size and Profit Margin % the color.

Build the treemap
In a new file, treemap.py, import the libraries and read the workbook. The last import is only needed for the HTML export at the end. The script runs with pandas 3.1 and Plotly 7.2.
import plotly.express as px
import pandas as pd
import plotly
df = pd.read_excel('data.xlsx')
Then put each column into its own variable, which keeps the chart call readable:
continent = df['Continent']
country = df['Country']
region = df['Region']
sales = df['Sales']
margin = df['Profit Margin %']
remark = df['Remark']
path sets the hierarchy from the outside in, values the size and color the color scale. hover_name puts the remark at the top of the tooltip.
fig = px.treemap(df, path=[continent, region, country], values=sales,
color=margin, color_continuous_scale=['red', 'yellow', 'green'],
title='Sales/Profit Overview', hover_name=remark)
Layout and export
A larger title font is optional. plotly.offline.plot saves the chart as chart.html and opens it in the browser, so you can share it as a single file that keeps its zoom and tooltips.
fig.update_layout(title_font_size=42, title_font_family='Arial')
plotly.offline.plot(fig, filename='chart.html')
Switch to a sunburst
A sunburst shows the same hierarchy as rings around a center. Replace treemap with sunburst and keep every argument:
fig = px.sunburst(df, path=[continent, region, country], values=sales,
color=margin, color_continuous_scale=['red', 'yellow', 'green'],
title='Sales/Profit Overview', hover_name=remark)
Update the chart from Excel
The chart has no data of its own. Change a margin or add a country in the workbook, run the script again, and the HTML file shows the new numbers.
To run it from a button in the workbook, see executing a Python script from Excel with VBA.
For data without a hierarchy, the same steps give you a pie chart in Plotly.
