Sunburst graphical object does not show with duplicate IDs

Hi guys,

I came across the following problem. I would like to create a sunburst chart with a hole in the middle. I wanted to do so using a single root, which I could color white in the end.
Furthermore, I would like to do it with the grapical_objects sunburst function rather than the plotly.express function. However, I used the plotly.express function to create parents, labels and values. For the graphical object version I constructed an id because I have duplicate values for the children in the outer ring.

However the neither I don’t get a result shown for the graphical_object version “fig2”. The express version “fig1” is working as intended.

Do you have an idea what I could try to make it work with the graphical object. Essentially the goal is a white hole in the middle and I need to allow duplicates in the children.

  df = pd.DataFrame({"Country": ["Country1", "Country2", "Country2", "Country1"], "side": ["buy", "sell", "buy", "sell"],
                    "constant": ["CONST", "CONST", "CONST", "CONST"], "trading_volume_eur": [600, 500, 300, 400]})

  path_values = ["constant", "Country", "side"]

  fig = px.sunburst(df, path=path_values, values="trading_volume_eur")
  fig.show()
  labels = fig['data'][0]['labels'].tolist()
  parents = fig['data'][0]['parents'].tolist()
  values = fig['data'][0]['values'].tolist()
  ids = [f"id{x}" for x in range(len(labels))]
  fig2 = go.Figure(
      go.Sunburst(
          # ids=ids,
          labels=labels,
          parents=parents,
          values=values,
          branchvalues='total',
          # rotation=90,
          # insidetextorientation='horizontal'
      ))
  fig2.show()

fig1:

It displays for me after adding this line:

parents = [parent.split("/")[-1] for parent in parents]

(The parents you’ve extracted from fig have the complete parent path, slash-separated, and go.Sunburst() wants only the immediate parent)

It’s a step forward, but probably not displaying quite as you want.

… and if you want to use the unique ids, the parents also have to be converted to ids:

(This code assumes that all labels are unique except on the bottom level)

labels = fig['data'][0]['labels'].tolist()
ids = [f"id{x}" for x in range(len(labels))]
parents = fig['data'][0]['parents'].tolist()
parents = [parent.split("/")[-1] for parent in parents]
parents = [f"id{labels.index(parent)}" if parent else '' for parent in parents]
values = fig['data'][0]['values'].tolist()
fig2 = go.Figure(
    go.Sunburst(
        ids=ids,
        labels=labels,
        parents=parents,
        values=values,
        branchvalues='total',
    ))

Ok, this one seems to do the trick. Didn’t know that the parents have to have a unique ID as well. Interestingly, it does not show the plot at fig2.show() and stays in a loading screen, however, if a write the image to a file it is displayed correctly. Thank you.

Glad it works. fig2.show() is working fine for me, running in VSCode

I am running it there as well. What do you use to preview it?

It opens in a new browser tab for me. I don’t know what settings cause that, but it might be something to do with me using remote SSH in VSCode