1

Tried to create grouped bar chart in pandas. Data looks like this

month companyname profit
dec a 20
jan a 30
feb a 50
dec c 80
jan c 20
feb c 34
dec b 20
jan b 30
feb b 50
dec a 20
jan a 30
feb a 50
  • x axis: company name
  • y axis: profit
  • hue: month.

Desired Output:

bargraph

BeRT2me
  • 12,699
  • 2
  • 13
  • 31
Jack
  • 181
  • 10

2 Answers2

0
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

sns.barplot(data=df, x='companyname', y='profit', hue='month')
plt.show()

Output

BeRT2me
  • 12,699
  • 2
  • 13
  • 31
0

Try this:

import calendar

month_order = map(str.lower, calendar.month_abbr[1:]) 
df.pivot_table('profit', 'companyname', 'month')\
  .reindex(month_order, axis=1)\
  .dropna(how='all', axis=1)\
  .plot.bar()

Output:

enter image description here

Note: Here I import calendar to get the month abbreviation order and use map to lowercase the string to match your data, then use pd.DataFrame.reindex to reorder the columns and plotted.

Scott Boston
  • 147,308
  • 15
  • 139
  • 187