-1

I have a dataset which contains columns: 'Month', 'Category', and 'Profitability'. I used following to find the sum of 'Profitability' for each month and category.

q1=df.groupby(['Month','Category'])['Profitability'].sum()

Here is the result I got.

Month  Category   
1      Cosmetics       2685.9000
       First Aid       2128.0200
       Magazine         703.8900
       Supplements    37005.6200
       Toiletries      1893.0600
2      Cosmetics       2569.0600
       First Aid       3282.7850
       Magazine         679.1100
       Supplements    36647.8800
       Toiletries      1357.7500
3      Cosmetics       1350.7925
       First Aid       2238.3100
       Magazine         371.1200
       Supplements    21444.0900
       Toiletries      1226.1600

I want to represent them in a bar chart. What would be the best approach to visualize these categorical data?

5
  • 1
    What do you want your visualization to depict? - What do you want the viewer to see? What are you trying to highlight to the viewer? How about a stacked bar chart?- one bar for each category with the values from each month stacked ? Or do you want to show changes over time for each category? Commented May 23, 2020 at 14:17
  • 1
    Maybe this helps: stackoverflow.com/questions/60585523/… Commented May 23, 2020 at 14:20
  • Or maybe a bar for each month with the category values stacked? Commented May 23, 2020 at 14:24
  • @wwii I want to set Month in X axis and Profit in Y axis. Need to draw 5 bars for each month for each Month Commented May 23, 2020 at 14:30
  • A simple line graph is often preferable to a bar plot, especially if you want to show changes over time. You could use different colors to show the categories. Commented May 23, 2020 at 14:54

1 Answer 1

1

A preparatory step is to convert your DataFrame with a single column and a MultiIndex into a DataFrame with "normal" index and a separate column for each category:

df2 = df.Profitability.unstack()

Or if source of your data is a Series (not a DataFrame), then run:

df2 = q1.unstack()

The result, better fit as a figure source, is:

Category  Cosmetics  First Aid  Magazine  Supplements  Toiletries
Month                                                            
1         2685.9000   2128.020    703.89     37005.62     1893.06
2         2569.0600   3282.785    679.11     36647.88     1357.75
3         1350.7925   2238.310    371.12     21444.09     1226.16

​To draw the figure, the basic approach is to take linear scale of y axis. The code to draw it is:

ax = df2.plot.bar(rot=0)
ax.get_figure().suptitle(t='Profitability', fontsize=20)
ax.legend(bbox_to_anchor=(1.35, 1.0));

The last instruction "moves" the legend a bit to the right (compared to its default location), otherwise it would obscure some bars (try to draw without this instruction).

The result is:

enter image description here

Note however that bars for Suppelements are very high, compared to other categories, which are very low.

This is why I came up with the second solution, based on logatithmic scale of y axis:

ax = df2.plot.bar(rot=0, logy=True)
ax.get_figure().suptitle(t='Profitability', fontsize=20)
ax.legend(bbox_to_anchor=(1.1, 1.0))
yTicks = [1000, 3000, 10000, 30000]
yTickLabels = [ f'{i:,}' for i in yTicks ]
ax.set_yticks(yTicks)
ax.set_yticklabels(yTickLabels);

Last 4 instructions change the default ticks on y axis, as in my opinion they are better readable that the default ticks (try to draw a figure without these 4 lines for comparison).

The result is:

enter image description here

Now heigths of bars are easier to compare and y ticks are chosen roughly on tick(n-1) * 3 basis.

Edit

If you want to have the legend with a title, add title='Category' parameter to ax.legend(...) instruction, whichever of the above solutions you choose.

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.