Search code examples
pythonplotlysunburst-diagram

It is possible to edit single categories fontsize and color with px.sunburst?


I created a nice px.sunburst graph. Now I want to format the single "nodes" of the sunburst. For instance I want to change fontsize of the main "node" and make it bold or change the color of a specific "node".

Unfrotunately I did not find such features in the Sunburst documentation.

uniformtext only formats the whole text and with color and color_discrete_map I cannot control single nodes:

Pseude Code:

import plotly.express as px 

data = dict(
    character=["Eve", "Cain", "Seth", "Enos", "Noam", "Abel", "Awan", "Enoch", "Azura"],
    parent=["", "Eve", "Eve", "Seth", "Seth", "Eve", "Eve", "Awan", "Eve" ],
    value=[10, 14, 12, 10, 2, 6, 6, 4, 4],
)
        
fig = px.sunburst(
    data,
    names='character',
    parents='parent',
    values='value',
    color='parent', 
    color_discrete_map={
        '': 'rgb(56, 75, 126)', 
        'Eve': 'lightgreen', 
        'Cain': 'black', 
        'Enos': 'black',
     },
)
     
fig.show()

enter image description here Is it possible, i.e.

  • to change colors of Eve´s "childnodes" seperately?
  • to change "main node" Eves fontsize to 15 and bold?

I did not find such features in the documentation.


Solution

  • To change the color of these sunburst's nodes separately you need to set color='character'. Keeping your discrete map this would result in:

    enter image description here

    For the font size, I'd let plotly work this out, because behind the scenes it's adjusting font size so that it can fit in its area. That being said, you can set a font size several ways to maybe achieve what your looking for:

    Using fig.update_traces(textfont=dict(size=[20])) will set the first item's font size.

    enter image description here

    To adjust more nodes expand the array as necessary to align with items in character, for instance:

    fig.update_traces(textfont=dict(family=['Arial','Courier New'],size=[8,20,8]))

    will set the second item in character to Courier New it's font to 20 and the first and third elements font size to 8.

    font and size

    Finally, to bring it all together (and this may depend on what fonts you have available) to set Eve's font size and make it bold:

    fig.update_traces(textfont=dict(family=['Arial Black', 'Arial'],size=[15])) all of it