[Python] Stacked Bar Graphs with only Plotly Express

Discussão em 'Python' iniciado por Stack, Abril 3, 2025.

  1. Stack

    Stack Membro Participativo

    I have data stored in three different lists like this:

    labels = ['label1', 'label2', 'label3']
    types = ['type1', 'type2', 'type3', 'type4', 'type5', 'type6']
    counts = [[3,5,2,1,7,10], [2,2,4,1,7,2], [1,6,8,11,2,3]]


    counts list holds counts of types for every label, like: label1 has 3 of type1, 5 of type2, ...; label2 has 2 of type1, 2 of type2, ... etc.

    I can create a stacked bar graph with plotly.graph_objects but I want to create this graph with plotly.express.

    My code to create the stacked bar graph with plotly.graph_objects:

    import plotly.graph_objects as go

    for i in range(len(types)):
    if i == 0:
    fig = go.Figure(go.Bar(x=labels, y=[sub for sub in counts], name=types))
    else:
    fig.add_trace(go.Bar(x=labels, y=[sub for sub in counts], name=types))

    fig.update_layout(barmode='stack')
    fig.show()


    How can I achieve the same result with only using plotly.express, since I create all my other graphs with plotly.express and don't want to import plotly.graph_objects

    Update: I can pass my data to a pandas dataframe and create the graph with plotly.express with the code below:

    import pandas as pd
    import plotly.express as px

    df = pd.DataFrame([count for sub in counts for count in sub],
    index=pd.MultiIndex.from_product([labels,types]),
    names=['label', 'type'],
    columns=['count'])
    fig = px.bar(df, x=df.index.get_level_values('label'),
    y='count',
    color=df.index.get_level_values('type'),
    barmode='stack')
    fig.show()


    But is there any way to do it without pandas too?

    Continue reading...

Compartilhe esta Página