简体   繁体   中英

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"

条形图

it is supposed to be similar to this

如同

here is the whole df and the work I did

整个df

The society mentioned minimal reproducible example, then you gotta think about what that means. In your case, the problem is:

  1. You wanna plot 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 StackOverflow?
b) How to prepare the data from your df and transform them available to plot.


Preparing for the data we wanna 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()

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM