Search code examples
pythonplotlyplotly-python

Adjusting plot.graph_objects.Sunburst colorbar


Although I can create a colorbar for Sunburst, I fail to manipulate the fontsize, ntick, tickvals and other parameters in the colorbar.

import plotly.graph_objects as go

data = dict(
    labels=["Eve", "Cain", "Seth", "Enos", "Noam", "Abel", "Awan", "Enoch", "Azura"],
    parents=["", "Eve", "Eve", "Seth", "Seth", "Eve", "Eve", "Awan", "Eve" ],
    values=[10, 14, 12, 10, 2, 6, 6, 4, 4],
    colors=[1, 2, 3, 4, 5, 6, 7, 8, 9],
)

fig = go.Figure(go.Sunburst(
    labels=data['labels'],
    parents=data['parents'],
    values=data['values'],
))

marker = fig.data[0].marker
marker.colors = data['color']
marker.colorscale = 'Blues'
marker.cmin = 0
marker.cmax = 10
marker.showscale = True

fig.update_layout(
    margin=dict(t=0, l=0, r=0, b=0),
    coloraxis_colorbar=dict(  # no response
        title='color',
        tickvals=[0, 5, 10],
        ticktext=['low', 'middle', 'high'],
        ntick=3
    ),
)

Is it possible to create a colorbar object and adjust the parameters from there?


Solution

  • You can continue with the same way with marker, have a look at the code below:

    import plotly.graph_objects as go
    
    data = dict(
        labels=["Eve", "Cain", "Seth", "Enos", "Noam", "Abel", "Awan", "Enoch", "Azura"],
        parents=["", "Eve", "Eve", "Seth", "Seth", "Eve", "Eve", "Awan", "Eve" ],
        values=[10, 14, 12, 10, 2, 6, 6, 4, 4],
        colors=[1, 2, 3, 4, 5, 6, 7, 8, 9],
    )
    
    fig = go.Figure(go.Sunburst(
        labels=data['labels'],
        parents=data['parents'],
        values=data['values'],
    ))
    
    marker = fig.data[0].marker
    marker.colors = data['colors']
    marker.colorscale = 'Blues'
    marker.cmin = 0
    marker.cmax = 10
    marker.showscale = True
    
    # Add the next 4 lines
    marker.colorbar.title = 'color'
    marker.colorbar.tickvals = [0, 5, 10]
    marker.colorbar.ticktext = ['low', 'middle', 'high']
    marker.colorbar.nticks = 3 
    
    fig.update_layout(
        margin=dict(t=0, l=0, r=0, b=0),
    )
    
    
    fig.show()
    

    Output enter image description here

    If you want to know more about the options that you have, please look at plotly.graph_objects.sunburst.marker package