0

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

Michael S.
  • 3,050
  • 4
  • 19
  • 34
user19113361
  • 13
  • 1
  • 3
  • 1
    Please [edit] your question to include your code as **text** rather than as screenshot(s). On stack overflow images should not be used for textual content, see [*Discourage screenshots of code and/or errors*](https://meta.stackoverflow.com/a/307500) and [*Why not upload images of code on SO when asking a question*](https://meta.stackoverflow.com/a/285557) for why. For instructions on formatting see *[How do I format my code blocks?](https://meta.stackexchange.com/q/22186)*. A [mcve] showing what you have tried that did not work would maximize your chances of getting help. See [ask]. – dbc Aug 02 '22 at 02:01

1 Answers1

0

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

miken32
  • 42,008
  • 16
  • 111
  • 154
Baron Legendre
  • 2,053
  • 3
  • 5
  • 22