Tom
Tom

Reputation: 1063

Plotly Sunburst chart, draw thicker borders on certain markers

I am building a sunburst chart in plotly.graph_objects. The MRE below approximates the real data I have. What I'd like to do is draw thicker borders around the inner-most level. E.g., in the toy example, there would be thicker borders around "Magic Kingdom" and "Epcot" extending all the way to the outer levels. Can this be done? enter image description here

import plotly.graph_objects as go
import pandas as pd

data = {
    'category': ['Epcot', 'Magic Kingdom', 
                'Future World', 'World Showcase', 
                'Adventure Land', 'Fantasy Land', 'Frontier Land',
                'Spaceship Earth', 'Imagination', 'Test Track',
                'Remy', 'Frozen',
                'Jungle Cruise', 'Pirates',
                'Mine Train', 'Small World', 'Tea Cups', 'Peter Pan',
                'Big Thunder Mountain'],
    'parents': ['', '', 'Epcot', 'Epcot', 
                'Magic Kingdom', 'Magic Kingdom', 'Magic Kingdom',
                'Future World', 'Future World', 'Future World',
                'World Showcase', 'World Showcase', 
                'Adventure Land', 'Adventure Land',
                'Fantasy Land', 'Fantasy Land', 'Fantasy Land', 'Fantasy Land',
                'Frontier Land'],
    'value': [0, 0, 0, 0, 0, 0, 0, 20, 5, 25, 8, 12, 20, 10, 12, 25, 19, 5, 25]}

df = pd.DataFrame(data)

fig = go.Figure(go.Sunburst(ids=df['category'],
                            labels=df['category'],
                            parents=df['parents'],
                            values=df['value']))
fig.update_traces(marker_line_width=4) #updates borders for all markers

# Show the plot
fig.show()

Upvotes: 0

Views: 301

Answers (1)

r-beginners
r-beginners

Reputation: 35230

Currently, only one line color can be set with the current feature. So it is not a workaround, but it can be achieved by adding a circle shape. In this case there are three layers of circles, so we add three circles and specify a line color for each. My code set the two outer circles to white and the innermost to black. Depending on the size and resolution of the graph, setting the size of the circles may require some trial and error.

fig = go.Figure(go.Sunburst(ids=df['category'],
                            labels=df['category'],
                            parents=df['parents'],
                            values=df['value']))
#fig.update_traces(marker_line_width=1) #updates borders for all markers
fig.update_layout(height=450, width=450)

fig.add_shape(type='circle',
              x0=0, y0=0,
              x1=16, y1=16,
              xref='x',yref='y',
              line_color='#ffffff',
              line_width=2,
              opacity=0.7
             )

fig.add_shape(type='circle',
              x0=2.66, y0=2.66,
              x1=13.34, y1=13.34,
              xref='x',yref='y',
              line_color='#ffffff',
              line_width=2,
              opacity=0.7
             )

fig.add_shape(type='circle',
              x0=5.32, y0=5.32,
              x1=10.68, y1=10.68,
              xref='x',yref='y',
              line_color='black',
              line_width=2
             )

fig.update_yaxes(
    scaleanchor = "x",
    scaleratio = 1,
  )
fig.update_layout(template='plotly_white',
                  xaxis=dict(showgrid=False, showticklabels=False, visible=False),
                  yaxis=dict(showgrid=False, showticklabels=False, visible=False))
# Show the plot
fig.show()

enter image description here

Upvotes: 0

Related Questions