pythonplotlyplotly-pythonstacked-chart

How to plot a stacked bar chart of single variable?


I have a dataframe:

df = pd.DataFrame({'name':['a', 'b', 'c', 'd', 'e'], 'value':[54.2, 53.239, 43.352, 36.442, -12.487]})

I'd like to plot a stacked bar chart like the one below with plotly.express:

enter image description here

How can a I do that?


Solution

  • It's a little wordy, but you can set a single value for the x axis, in this case zero. Then you just need to tweak your dimension, lables, and ranges.

    import pandas as pd
    import plotly.express as px
    df = pd.DataFrame({'name':['a', 'b', 'c', 'd', 'e'], 'value':[54.2, 53.239, 43.352, 36.442, -12.487]})
    
    
    df['x'] = 0
    
    fig = px.bar(df, x='x', y='value',color='name', width=500, height=1000)
    
    fig.update_xaxes(showticklabels=False, title=None)
    fig.update_yaxes(range=[-50,200])
    fig.update_traces(width=.3)
    fig.show()
    

    enter image description here