Set treemap colour by categories in Plotly in Python

codedancer

I am trying to set colour by different continent in Plotly. I am wondering on how to achieve this.

import plotly.express as px
import numpy as np
df = px.data.gapminder().query("year == 2007")
fig = px.treemap(df, path=[px.Constant("world"), 'continent', 'country'], values='pop',
                  color='lifeExp', hover_data=['iso_alpha'],
                  color_continuous_scale=['#32a852', '#3261a8', '#a83259'],
                  color_continuous_midpoint=np.average(df['lifeExp'], weights=df['pop']))
fig.update_layout(margin = dict(t=50, l=25, r=25, b=25))
fig.show()
Rob Raymond

Leaving heavy lifting to Plotly Express, once figure is built modify marker_color list

import plotly.express as px
import numpy as np

df = px.data.gapminder().query("year == 2007")
fig = px.treemap(
    df,
    path=[px.Constant("world"), "continent", "country"],
    values="pop",
    color="lifeExp",
    hover_data=["iso_alpha"],
    color_continuous_scale=["#32a852", "#3261a8", "#a83259"],
    color_continuous_midpoint=np.average(df["lifeExp"], weights=df["pop"]),
)
fig.update_layout(margin=dict(t=50, l=25, r=25, b=25))
cm = {
    "Asia": "red",
    "Africa": "yellow",
    "Americas": "white",
    "Europe": "blue",
    "Oceania": "black",
}
fig.for_each_trace(
    lambda t: t.update(
        marker_colors=[
            cm[id.split("/")[1]] if len(id.split("/")) == 2 else c
            for c, id in zip(t.marker.colors, t.ids)
        ]
    )
)

enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Treemap with variable # of categories (Plotly)

Change plotly colour with hex code in Python

How to set a colour legend with the days of the week in Plotly bar chart

Make plotly treemap also show negative values (Python)

Python Dash Plotly : Flip/reverse colour legend when formatting table

Python Dash Plotly : Customise colour legend when formatting table

Change the colour scheme of the 3D surface plot in Plotly Python

python plotly Treemap ids format and how to display multiple duplicated labels in a treemap?

Plotly Treemap using Dash

How do I manually set point colour in dash plotly scatter plots?

Plotly: changing the colour scale (colour gradient) to a specific range of colour

Changing the width of the borders in a Plotly treemap

Plotly: treemap element with "href" not working

Plotly express treemap background color

Colour scatter plot by column Plotly

Plotly box plot with multiple categories

OpenCV&Python - Set specific colour to region of image

Python in Power BI - Set background colour for matplotlib box plot

Set Header colour and border for all sheets in an excel file with python

Using percentiles of a timeseries to set colour gradient in Python's matplotlib

Python PyQt5 QTreeView set row Background Colour

how to set colorscale with reference to dates in plotly python

How to colour part of ggplotly/plotly hover text a different colour in R

How to arrange categories on y axis based on values in descending order on x axis in Python plotly express?

Set Nan Colour In Mayavi

How to add counter and sum in plotly express treemap

Change hover text of a plotly express treemap

Plotly: Show hoverinfo in treemap only for child nodes

Plotly's treemap and sunburst show wrong sizes