How can we plot a bar chart with categorical values on x-axis in Python?

python

#1

Hi,

I want to plot a bar chart in python with categorical values on x-axis and sum of other variable on Y-axis. Can You please guide me to perform this.

Data Set

Output

Regards,
Imran


#2

@Imran,

You first need to calculate sum of sales by BMI using dataframe.groupby and after that you can plot bar chart. See the below code that I have used to plot sum of sales against category Gender.

For more detail on groupby, you can refer link:
http://pandas.pydata.org/pandas-docs/stable/groupby.html

Code
var = df.groupby(‘Gender’).Sales.sum() #Group sum of Sales at Gender level

ax1.set_xlabel('Gender')  #X-axis label
ax1.set_ylabel('Sum of Sales') #Y-axis label
ax1.set_title("Gender wise Sum of Sales") #Chart title
var.plot(kind='bar') 

Output

Hope this helps!

Regards,
Sunil