Search code examples
pythongraphplotlyplotly-python

Stacked Bar Graphs with only Plotly Express


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[i] for sub in counts], name=types[i]))
    else:
        fig.add_trace(go.Bar(x=labels, y=[sub[i] for sub in counts], name=types[i]))

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?


Solution

  • You can create a dataframe and structure it from your lists. Then it's simple to use Plotly Express

    import plotly.express as px
    import pandas as pd
    
    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]]
    
    px.bar(
        pd.DataFrame(counts, columns=types, index=labels).reset_index().melt(id_vars="index"),
        x="index",
        y="value",
        color="variable",
    )
    

    enter image description here

    numpy

    import numpy as np
    
    c = np.array(counts)
    px.bar(x=np.tile(labels, c.shape[1]), color=np.repeat(types, c.shape[0]), y=c.T.flatten())