Px.sunburst color discrete map: change parent branch color

Hi everyone, happy new year!
I have the following initial code:

# import libraries
import pandas as pd
import plotly.express as px 

# import csv 

df = pd.read_csv('../PRODUCTS/legislativas_2022_CNE_listas_08JAN_1147.csv')

# plot sunburst 

fig = px.sunburst(df, path=['circulo', 'partido','tipo','candidato'],color='partido',
		# Colors were picked at each parties website 
		color_discrete_map={
                "PCTP/MRPP": "#E00E02",
				"PPD/PSD": "#FF6400",
				"CDS/PP":"#0B6AB1",
				"Ergue-te": "#BF311E",
				"MAS": "#2B2C2C",
				"PS": "#E31F26",
				"LIVRE": "#00CE8C",
				"ADN": "#204E84",
				"BE": "#C90635",
				"PCP - PEV - CDU": "#025298",
				"R.I.R.": "#00939D",
				"Volt Portugal": "#502376",
				"CHEGA": "#202056",
				"JPP": "#03AB83",
				"IL": "#00AEEF",
				"PAN": "#00798E",
				"MPT": "#66BB69",
				"PTP": "#00659B",
				"NĂłs, CidadĂŁos!": "#00659B",
				"ALIANÇA": "#0451CB",
				"PPD/PSD-CDS-PP - MADEIRA PRIMEIRO": "#F36D24",
				"PPM" : "#19476C",
 				"PPD/PSD - CDS-PP - PPM – AD/Aliança Democrática": "#0031A"
							}
            )

fig.show()

So far so good, everything works like it should work as you can see here

The dataset can be found here

However I want to control the color not only of each individual “partido” but also the color that is going to be used for “circulo” and “tipo” so there is more uniformity.

Even if for “tipo” is not possible, since it’s a child branch, to be able to put “circulo” in a neutral color would help a lot.

As usual thank you in advance!

Full Disclosure The final product will be used for an not for profit project that informs citizens regarding the political system in Portugal. It will not be used for commercial purposes.

1 Like

Hello,

if I got your right, I was dealing with something similar today. In order to have full control, you can modify directly the marker:

Example:

fig = px.sunburst(df, path=["overall_result", "result"])
color_mapping = {'FALSE_NEGATIVE': "darkred", 'TRUE_NEGATIVE': "green", 'FALSE_POSITIVE': "red", 'TRUE_POSITIVE': "lightgreen", 'TRUE': "green", "FALSE": "red"}
fig.update_traces(marker_colors=[color_mapping[cat] for cat in fig.data[-1].labels])

That way I can set color of every item in the chart including root nodes.

1 Like