With plotly.express you can easily create fancy treemaps. For example, let’s say that we want to represent the life expectancy by country taking into consideration the continent and the population of the country. So, in the treemap below, the size of the rectangular is referred to the population and the color is referred to the life expectancy, the higher the life expectancy the more blue the color is and the lower the life expectancy the redder the color is.
import plotly.express as px import numpy as np df = px.data.gapminder().query("year == 2007") df["world"] = "world" # in order to have a single root node fig = px.treemap(df, path=['world', 'continent', 'country'], values='pop', color='lifeExp', hover_data=['iso_alpha'], color_continuous_scale='RdBu', color_continuous_midpoint=np.average(df['lifeExp'], weights=df['pop'])) fig.show()
You can find more details at plotly documentation