Multivel Plotly Chart

16 Views Asked by At

I would like to build a stacked horizontal bar chart but with 4 levels on the y-axis. I've written this code:

# define ylabels
ylabels = [list(df.index.get_level_values(0)),
           list(df.index.get_level_values(1)),
           list(df.index.get_level_values(2)),
           list(df.index.get_level_values(3)),
           df.index]

# Let's create a bar chart
fig = go.Figure()`

# create traces
for idx, tech in enumerate(df.columns):
    fig.add_trace(
        go.Bar(
            x=df[tech],
            y=ylabels,
            showlegend=False,
            width=0.5,
            orientation='h',
        )
    )`

# specify layout
fig.update_layout(
    autosize=False,
    width=1000,
    height=500,
    xaxis=dict(  # Change xaxis to yaxis
        titlefont_size=16,
        tickfont_size=12,
        title="Mio. Stk.",
        showspikes=True),
    yaxis=dict(  # Change yaxis to xaxis
        titlefont_size=16,
        tickfont_size=12),
    template='plotly_white',
    barmode='stack',
)

fig.update_layout(legend=dict(
    orientation="h",
    yanchor="bottom",
    y=1.02,
    xanchor="right",
    x=1
))

fig.show()`

However, it is just displaying the firsts two lists I've created inside my

ylabels (list(df.index.get_level_values(0)),
           list(df.index.get_level_values(1)),)

. If someone could help me I would appreciate a lot. thanks :) I'm expecting to display all my ylabels values on y-axis

0

There are 0 best solutions below