I`m using an Icicle chart to data from a hierarchy perspective, with a button to adjust the values shown. The following is an example of my code:

df = pd.DataFrame({
    'YEAR': ['2023', '2023', '2023', '2023'],
    'COMPANY': ['Company A','Company A','Company A','Company A'],
    'ORG': ['People', 'People', 'Customer', 'Customer'],
    'DEPARTMENT': ['HR', 'HR', 'Marketing', 'Marketing'],
    'AREA': ['Reward', 'Benefits', 'Campaigns', 'Insights'],
    'Employees': [5, 4, 9, 11],
    'Salary Cost' : [80000,75000,113000,140000]
})

figs = {
    c: px.icicle(df, path=["COMPANY","ORG", "DEPARTMENT", "AREA"], values=c, color='AREA', height=700)
    for c in ["Employees", "Salary Cost"]
}

fig = figs["Employees"]

#Button for FTE and HC
fig.update_layout(
    updatemenus=[
        {
            "buttons": [
                {
                    "label": c, #FTE or HC, needs to match df column names
                    "method": "restyle",
                    "args": [
                        {
                            "values": [figs[c].data[0].values],
                            "hovertemplate": figs[c].data[0].hovertemplate,
                        }
                    ],
                }
                for c in figs.keys()
            ]
        }
    ]
)

I`d like the icicle chart to be top down rather than right to left. Looking at the plotly documentation, there is an example however this is under the graph_objects and not all the same features are available.

2

There are 2 best solutions below

2
On

They have an example of the vertical orientation in their docs. Here's a demonstration of how to apply this on your code.

import plotly.graph_objects as go
import pandas as pd
import numpy as np


df = pd.DataFrame({
    'YEAR': ['2023', '2023', '2023', '2023'],
    'COMPANY': ['Company A','Company A','Company A','Company A'],
    'ORG': ['People', 'People', 'Customer', 'Customer'],
    'DEPARTMENT': ['HR', 'HR', 'Marketing', 'Marketing'],
    'AREA': ['Reward', 'Benefits', 'Campaigns', 'Insights'],
    'Employees': [5, 4, 9, 11],
    'Salary Cost' : [80000,75000,113000,140000]
})
r_df = pd.DataFrame(np.concatenate([df.iloc[:,i:i+2].to_numpy() for i in range(1, 5)]), columns=['parents', 'ids']).drop_duplicates()

fig = go.Figure(
    go.Icicle(
        ids = r_df.ids,
        labels = r_df.ids,
        parents = r_df.parents,
        root_color="lightgrey",
        tiling = dict(
            orientation='v'
        )
    )
)
fig.update_layout(margin = dict(t=50, l=25, r=25, b=25))
fig.show()

Icicle chart

0
On

Note plotly.express provides functions to create entire figures at once with less code but still, it's built on top of Plotly Graph Objects, ie. px.icicle() returns a go.Figure with a go.Icicle trace in it. So, once the figure is built, you can update the (graph object) trace(s) attributes regardless of the function used to create it.

You can set the icicle direction to be top->down by setting the tiling orientation accordingly :

fig.update_traces(tiling_orientation='v')