1

I am trying to plot a bar graph, Sale vs categories. Below is my data frame,

  S2PName-Category  S2BillDate   totSale  count
0        Beverages  2020-03-06   4452.62    252
1             Food  2020-03-06  36556.91    774
2           Others  2020-03-06    608.95     99
3           Snacks  2020-03-06   2662.75    139
4            Juice  2020-03-06   2139.49     40
5         IceCream  2020-03-06    135.00      4
6              OOB  2020-03-06    390.00     20

My Code :

data = [go.Bar(x=df['S2PName-Category'].unique(),
        y=df[df['S2PName-Category']==category]['totSale'],
        name = category) for category in df['S2PName-Category'].unique()]

layout = go.Layout(title='Category wise performance',
        xaxis = dict(title = 'Categories', automargin = True),
        yaxis = dict(tickprefix= '₹', tickformat=',.2f',type='log',autorange = True),
        hovermode = 'closest',
        plot_bgcolor =  colors['background'],
        paper_bgcolor = colors['background'],
        font = dict(color = colors['text'])
        )

My Graph : enter image description here

Only Beverages from the category ('S2PName-Category') , ispopulated in x-axis, rest are not getting populated. Can anybody point out what went wrong, and how to solve this ? Thanks !

TRIAL 2 : I was trying out other possibilties, due to list comprehension my bar chart gets grouped, is there a way to get unique categories to be plotted ?

Code :

data = [go.Bar(x=df['S2PName-Category'],
        y=df['totSale'],
        name = category) for category in df['S2PName-Category'].unique()]

    layout = go.Layout(title='Category wise performance',
        xaxis = dict(title = 'Categories', automargin = True),
        yaxis = dict(tickprefix= '₹', tickformat=',.2f',type='log',autorange = True),
        hovermode = 'closest',
        plot_bgcolor =  colors['background'],
        paper_bgcolor = colors['background'],
        font = dict(color = colors['text'])
        )

Graph Op : enter image description here

3
  • I'ts hard to point out what's wrong without a complete code snippet. As it now stands, it fails amon other places on y=df[df['S2PName-Category']==category]['totSale']. Commented May 3, 2020 at 21:33
  • Actually thats my complete code snippet, what else more would be reqd ? If u could let me know I would be happy to share. I have posted my data frame, I just want to plot a bar graph, but use for loop for traces / name attribute. While doing that i messed up somehow Commented May 4, 2020 at 6:45
  • @vestland I have ediited my question as well Commented May 4, 2020 at 7:48

1 Answer 1

1

If your provided data sample does in fact represent your real data source, you can just set up a figure with go.Figure() and add an individual trace for each 'S2PName-Category' If this does not suffice, you will have to provide a better data sample.

enter image description here

Complete code:

import plotly.express as px
import pandas as pd
import plotly.graph_objs as go

df = pd.DataFrame({'S2PName-Category': {0: 'Beverages',
                    1: 'Food',
                    2: 'Others',
                    3: 'Snacks',
                    4: 'Juice',
                    5: 'IceCream',
                    6: 'OOB'},
                    'S2BillDate': {0: '2020-03-06',
                    1: '2020-03-06',
                    2: '2020-03-06',
                    3: '2020-03-06',
                    4: '2020-03-06',
                    5: '2020-03-06',
                    6: '2020-03-06'},
                    'totSale': {0: 4452.62,
                    1: 36556.91,
                    2: 608.95,
                    3: 2662.75,
                    4: 2139.49,
                    5: 135.0,
                    6: 390.0},
                    'count': {0: 252, 1: 774, 2: 99, 3: 139, 4: 40, 5: 4, 6: 20}})


fig = go.Figure()
for cat in df['S2PName-Category']:
    dft = df[df['S2PName-Category']==cat]
    fig.add_traces(go.Bar(x=dft['S2PName-Category'], y=dft['totSale'], name=cat))

fig.show()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.