Search code examples
pythonplotly

color discrete sequence in a plotly graph objects


how can i plot a pie chart with color sequence while using plotly.graph_objects instead of plotly.express. In plotly.express is it very easy to do with the property "color_discrete_sequence". Is there something similar? Thank you


Solution

  • You could use the colors attribute of the marker if I understand your problem correctly. Just get the color Sequence you want.

    import plotly.graph_objects as go
    import plotly.express as px
    
    colors = px.colors.sequential.RdBu
    
    fig = go.Figure(data=[go.Pie(labels=['Oxygen','Hydrogen','Carbon_Dioxide','Nitrogen'],
                                 values=[4500,2500,1053,500])])
    fig.update_traces(hoverinfo='label+percent', textinfo='value', textfont_size=20,
                      marker=dict(colors=colors, line=dict(color='#000000', width=2)))
    fig.show()