✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
🧬 Learn how to build RNA-Seq data apps with Python & Dash. Register for the May 20 Webinar!

Sunburst properties

After I built a sunburst graph, I would like to select a parent to get the parent name and its children. There are properties for sunburst graph:
[‘id’, ‘clickData’, ‘clickAnnotationData’, ‘hoverData’, ‘clear_on_unhover’, ‘selectedData’, ‘relayoutData’, ‘extendData’, ‘restyleData’, ‘figure’, ‘style’, ‘className’, ‘animate’, ‘animation_opt
ions’, ‘config’, ‘loading_state’]

Where I can find the descriptions for the properties. I checked ‘https://plot.ly/python/reference/#sunburst’ and there are not property description for above properties.

I tried ‘clickData’, ‘selectedData’ and ‘extendData’, none of them returned information when I click the suuburst graph.
‘hoverData’ works but it is so sensitive, it is hard to use mouse to focus on one node. It is easily hover over to another node.

Do you think you can achieve your goal with Plotly Express Sunburst?

Just tried, what I got:

AttributeError: module ‘plotly.express’ has no attribute ‘sunburst’

No graph obtained.

Can I see your code?
Maybe sunburst is a new graph that came out with a more recent plotly Express version. What version are you using?

here are what I am using:
dash 1.7.0
dash-core-components 1.6.0
dash-cytoscape 0.1.1
dash-html-components 1.0.2
dash-renderer 1.2.2
dash-table 4.5.1
Flask 1.1.1
Flask-Compress 1.4.0
future 0.18.2
itsdangerous 1.1.0
Jinja2 2.10.3
MarkupSafe 1.1.1
numpy 1.17.4
pandas 0.25.3
pip 19.0.3
plotly 4.3.0

Codes will be followed.

Here is the codes I have, built for dash:

def sundata():
return go.Sunburst(
labels = df[‘label’].tolist(),
parents= df[‘parent’].tolist(),
)
def sub_sundata(hoverParent):
parent=hoverParent.get(‘points’)[0].get(‘label’)
subtree = df.loc[df[‘parent’]==parent]
return go.Sunburst(
labels=subtree[‘label’].tolist(),
parents=subtree[‘parent’].tolist()

I would like to get info from the clickData or SelectedData (such as ‘parent’ to update the sunburst with different format. ‘hoverdata’ is so sensitive and provides unwanted data when hovering over the graph.

@app.callback(Output(‘sun’, ‘figure’), [Input(‘sun’, ‘hoverData’)]) #[Input(‘sun’, ‘clickData’)]) #[Input(‘sun’, ‘selectedData’)])
def updatesun(data):
print(data)
if data is None:
trace = sundata()
else:
trace = sub_sundata(data)

layout = go.Layout(hovermode='closest', height=600, width=450,
                    margin=go.layout.Margin(t=0, l=0, r=0, b=0))
return {'data': [trace], 'layout': layout}

@pengchuzhang
it’s hard to read your code when it’s not in preformatted text. In addition, without context about your dataframe, it’s hard for me to help. I would say start from the basics.
Try to run this code below on a new .py file. If it works, then try to recreate the same sunburst with your own data, but without callbacks. Then, if you succeed with that, try to add callbacks. Hope that helps.

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',
)
fig.show()

Thanks for the comments. The problem is that the version of my plotly was 4.3, after upgrading to 4.4.1, it does work now. I am still testing the properties.
Pengchu

Hi - were you able to make clickable sunbursts work? I need them to work on plotly.express if possible

click-leaf

Yes, it works. Not with plotly.Express but the dashboard with plotly objects, go.Sunburst.

I am still looking for the document for Sunburst’s properties:

[‘id’, ‘clickData’, ‘clickAnnotationData’, ‘hoverData’, ‘clear_on_unhover’, ‘selectedData’, ‘relayoutData’, ‘extendData’, ‘restyleData’, ‘figure’, ‘style’, ‘className’, ‘animate’, ‘animation_opt
ions’, ‘config’, ‘loading_state’]

Please help. Thanks.

Pengchu

I can’t get the chart studio to work with plotly.objects and go. … Can you take a quick look and suggestion anything obvious?

import plotly.graph_objects as go
import pandas as pd

#df = pd.read_csv(’…/CSV/Indicators.csv’)
df = df[[“code”, “desc”, “score”, “topic”, “subtopic1”, “subtopic2”, “subtopic3”]].drop_duplicates()

this code is from Data frame and codes needed to get a plotly sunburst chart

#df.loc[df[‘parents’].isna(), ‘parents’] = ‘’
df.loc[df[‘topic’].isna(), ‘topic’] = ‘’
df[‘parents’] = df.topic.str.strip() + ’ - ’ + df.subtopic1.str.strip()
df[‘ids’] = df.parents + ’ - ’ + df.code.str.strip()
df.code = df.code.str.strip()
df.desc = df.desc.str.strip()

#fig = go.Figure(go.Sunburst(ids=df.ids, labels=df.labels, parents=df.parents))
fig = go.Figure(go.Sunburst(ids=df.ids, labels=df.code, parents=df.parents))
fig.show()

Here is the data with the joins for parents and ids …

Difference I have from yours is that the labels and parents should be specified as list (array):

   labels=df['label'].tolist(),
    parents=df['parent'].tolist(),

Please try it.

FWIW, the latest plotly release included a much simplified sunburst API, see this announcement: 📣 Announcing Plotly.py 4.5 for Dash and the new docs at https://plot.ly/python/sunburst-charts/

Thanks,

Yes - The Path command works great, but its not yet implemented in chart studio. I went to the trouble of coding the needed navigation lines into the chart studio sunburst - and got it to work. I was hoping to use the click option on leafs, but I can’t see a solution to that problem as yet.

I posted the image above that shows the link that I want to click and open (a url page)

For clicking and links, I think the only way you can do this right now is by embedding the <a> inside the text rather than the hover text. The, you’d click on the text directly rather than trying to click on the hover label. So in your picture above, “Purchasing power parity” would be a link, you’d format it like [..., <a href="http://csq1.org/info/NY.GDP...">Purcashing power parity</a>', ...]

1 Like