python plotly icicle, order and sort

24 Views Asked by At

I want to use icicle chart to show function call stacks, the MWE is as the following

import pandas as pd
import numpy as np
import plotly.express as px

data = np.array(
        [
            ["main", "A-Func", None, 1],
            ["main", "C-Func", "sub1", 1],
            ["main", "C-Func", "sub2", 1],
            ["main", "B-Func", None, 1],
            ["main", "D-func", "sub1", 1],
            ["main", "D-func", "sub2", 1],
            ["main", "D-func", "sub3", 1],
        ]
        )

df = pd.DataFrame(data, columns = ['Level0', 'Level1', 'Level2', 'Weight'])
fig = px.icicle(df, path=['Level0', 'Level1', 'Level2'], values='Weight')
fig.update_traces(root_color="lightblue")
fig.show()

This is yielding a graph sorted by both Weight and Name

enter image description here

If I added sort parameter

fig.update_traces(root_color="lightblue", sort=False)

The weight is not sorted, however the name are still sorted

enter image description here

What I want to achieve is the nature order of these function calls as I create the numpy object

main
  |
  +--- A-Func
  |
  +--- C-Func
  |     |
  |     +--- sub1
  |     |
  |     +--- sub2
  |
  +--- B-Func
  |
  +--- D-Func
        |
        +--- sub1
        |
        +--- sub2
        |
        +--- sub3

Anyone can provide suggestions on how to achieve this?

0

There are 0 best solutions below