You are on page 1of 9

DATA VISUALIZATION USING

PYTHON PANDAS
INFORMATICS PRACTICES
CLASS XII
BAR GRAPHS
A graph drawn using rectangular bars to show how
large each value is. The bars can be horizontal or
vertical.
The length of the rectangular bars, each for a
specific category, represents the value of that
category.
To make a bar chart with matplotlib, we need to use
the plt.bar() function.
BAR GRAPHS
Example: Consider the following data. If we try making a BAR
graph representing age , then it would appear like this.
Name Age Gender
Representing Age Representing Age
100 Asha 15 F 25
Sumit
101 Kashish 18 M 20

Names of students
20 18
102 Meeta 20 F
15 Meeta
103 Sumit 12 M 15
Age 12
10 Kashish
Age
Remember that the x and y
axes will be swapped when 5
Asha
using barh, requiring care
when labelling. 0
Asha Kashish Meeta Sumit 0 5 10 15 20 25
Age
Names of students
Vertical Bar graphs Horizontal Bar graphs
Representing Columns of a dataframe with the help of Bar Graph
import matplotlib.pyplot as plt
import pandas as pd
dict1={'Name':['Asha','Kashish','Meeta','Sumit'],
'Age':[15,18,20,12],
'Gender':['F','M','F','M’] }
df=pd.DataFrame (dict1,index=[100,101,102,103])
print(df)
x=df['Name']
y=df['Age']
plt.bar(x,y,width=0.5,color='green')
plt.title("Representing Age of
kids",fontsize=14,color="blue")
plt.xlabel("Names of students",fontsize=14,color="red")
plt.ylabel("Age",fontsize=14,color="red")
plt.show()
Horizontal Bars
import matplotlib.pyplot as plt
import pandas as pd
dict1={'Name':['Asha','Kashish','Meeta','Sumit'],
'Age':[15,18,20,12],
'Gender':['F','M','F','M’] }
df=pd.DataFrame(dict1,index=[100,101,102,103])
print(df)
x=df['Name']
y=df['Age']
plt.barh(x,y, color='green')
plt.title("Representing Age of
kids",fontsize=14,color="blue") Remember that the x and y axes
plt.xlabel("Age",fontsize=14,color="red") will be swapped when using
plt.ylabel("Names of Students",fontsize=14,color="red") barh, requiring care when
plt.show() labelling.
Example: Plot a graph to show the Monthwise sales of Mid
range cars of a company on a Bar graph.
import matplotlib.pyplot as plt
import pandas as pd
dict1={'Month':['July','August','Sept','Oct','Nov','Dec'],
'Mid_Car':[100,80,150,170,160,145],
'Bike':[150,155,170,180,100,90],
'High_Car':[50,60,70,40,20,10] }
df=pd.DataFrame(dict1)
print(df)
x=df['Month']
y=df['Mid_Car']
plt.bar(x,y, color='green')
plt.title("Month Wise Automobile Sales in North
Region",fontsize=14,color="blue")
plt.xlabel("Month",fontsize=14,color="red")
plt.ylabel("Sales of Mid Range Cars",fontsize=14,color="red")
plt.show()
Some more Variation…………………..
x=df['Month']
y=df['Mid_Car']
plt.bar(x,y, color='green')
plt.title("Month Wise Automobile Sales in North
Region",fontsize=14,color="blue")
plt.xlabel("Month",fontsize=14,color="red")
plt.ylabel("Sales of Mid Range
Cars",fontsize=14,color="red")
plt.xticks(x, fontsize=10, rotation=30)
plt.show()
What if we want to show sales of multiple items
monthwise??? And thus show a comparison of
the sales values of various items.
Pandas plot provides what we need here, putting
the index on the x-axis, and rendering each
column as a separate series or set of bars, with a
(usually) neatly positioned legend.
import matplotlib.pyplot as plt
import pandas as pd
dict1={'Mid_Car':[100,80,150,170],
'Bike':[150,155,170,180],
'High_Car':[50,60,70,40]
}
df=pd.DataFrame(dict1, index=['July','August','Sept','Oct'])
df.plot(kind='bar', color=['green','black','orange'])
plt.title("Comparison of Sales Month
wise",fontsize=14,color="blue")
plt.xlabel("Month",fontsize=14,color="red")
plt.ylabel("Sales",fontsize=14,color="red")
plt.xticks(fontsize=10, rotation=30)
plt.show()

You might also like