pythonplotlyplotly-dashplotly.graph-objects

problem with inserting data into the bar chart plotly graph_objects


so I'm trying to add the lists ( Internal, External ..) to the bar chart but the data goes to one column "internal"

bar chart

it is supposed to be similar to this

similar to

here is the whole df and the work I did

whole df


Solution

  • In your case, the problem is:

    1. You want to plot a bar chart with plotly.graph_objects
    2. What exactly is data that go.Bar() needed. (status, escalation)

    Deduce from questions above,
    a) How do you fetch the minimum portion of data for the demo in Stack Overflow?
    b) How to prepare the data from your df and transform them available to plot.


    Preparing for the data we want to plot:

    import random
    import pandas as pd
    import plotly.graph_objects as go
    random.seed(42)
    sample_quntity = 50
    status = [random.choice(['Not Done','Something Else','Done']) for i in range(sample_quntity)]
    escalation = [random.choice(['Internal','External','Unspecified']) for i in range(sample_quntity)]
    df = pd.DataFrame({
        'status':status,
        'escalation':escalation
    })
    df
    ###
                status   escalation
    0             Done  Unspecified
    1         Not Done     External
    2         Not Done     Internal
    3             Done  Unspecified
    4   Something Else     External
    5         Not Done  Unspecified
    ⋮                ⋮            ⋮
    43  Something Else  Unspecified
    44        Not Done  Unspecified
    45        Not Done     Internal
    46  Something Else     Internal
    47        Not Done     External
    48  Something Else     External
    49  Something Else     External
    



    Plot:

    # plot bar chart grouping with status and escalation and color by status
    group_list = df['escalation'].unique()
    palette = {"Not Done": "#d89a9e","Something Else":"#e0c1b3", "Done": "#aeb4a9"}
    
    fig = go.Figure(data=[
        go.Bar(name='Not Done',
               x=group_list,
               y=df[df['status'] == 'Not Done'].groupby('escalation').size(),
               marker_color=palette['Not Done']),
        go.Bar(name='Something Else',
               x=group_list,
               y=df[df['status'] == 'Something Else'].groupby('escalation').size(),
               marker_color=palette['Something Else']),
        go.Bar(name='Done',
               x=group_list,
               y=df[df['status'] == 'Done'].groupby('escalation').size(),
               marker_color=palette['Done'])])
    
    fig.update_layout(title='Group by Escalation, Color by Status', barmode='stack')
    fig.show()
    
    

    enter image description here