Matplotlib
Date:14-May_2023
Note: This note is exclusive only for SPMVV (tirupati) Python and AI students.
When data is shown in the form of pictures, it becomes easy for the user to understand it.
Representing the data in the form of pictures or graphs is called ‘data visualisation’.
For this purpose, we can use the pyplot submodule.
Bar Graph - 1:
A bar graph represents data in the form of vertical or horizontal bars. It is useful to compare
the quantities.
Let us create a bar graph with employee id on x-axis and their salary on y-axis. We will take
this data from the DataFrame.
import [Link] as plt
import pandas as pd
empdata = {'empid': [1001,1002,1003,1004,1005,1006],
'ename':
["Sridhar","Abhishek","Lavanya","Sathish","kumar","Chaitanya"],
'sal': [110000,100000,70000,55000,45000,66000],
'doj':
["12-1-2017","4-9-2018","21-3-2012","16-8-2010","18-6-2019","22-10-2020
"]}
#create a dataframe
[Link]/in/shivakrishnavaraprasad/
df=[Link](empdata)
df
#extract empid and salary data into x and y axis
x=df['empid']
y=df['sal']
# create bar graph
[Link](x,y, label="Employee data", color='red')
# set dx and y axis labels
[Link]("Employee ids")
[Link]("Employee salaries")
#set company title
[Link]("SPMVV CO")
# show legend (colors and labels)
[Link]()
# display the graph
[Link]()
Bar graph - 2
We can create bar graphs from more than one data set that are coming from multiple data
[Link]/in/shivakrishnavaraprasad/
frames. For example we can take emp ids and salaries for 2 departments. sales and team
and production [Link] this purpose we can call [Link] two times.
import [Link] as plt
# take emp ids ans salaries from sales department
x=[1001,1003,1005,1006,1008,1011]
y=[100000, 200000, 180000, 150000,165000,95000]
# # take emp ids ans salaries from production department
x1=[1002,1004,1007, 1009, 1012,1015]
y1=[50000,65000,99000,120000,155000,85000]
[Link](x,y, label="sales dept. ", color='red')
[Link](x1,y1, label="production dept.", color="green")
[Link]("emp id")
[Link]=("salaries")
[Link]("SPMVV CO")
[Link]()
[Link]()
[Link]/in/shivakrishnavaraprasad/
Histogram
Histogram shows distributions of values. histogram is similar to bar graph but it is useful to
show values grouped in intervals or bins. Ex: collect the age of employees and show 1-10
years and 10-20 years.
import [Link] as plt
emp_ages= [22,45,33,22,43,55,47,66,56,38,32,52,33,44,55,53]
bins=[0,10,20,30,40,50,60]
#create histogram as bar type
[Link](emp_ages, bins, histtype='bar',rwidth=0.8, color='cyan')
# set labels
[Link]('employee ages')
[Link]("no. of employees")
[Link]("SPMVV CO")
[Link]()
[Link]()
Pie Chart
[Link]/in/shivakrishnavaraprasad/
Startangle=90 starts at 90 degree(default 3pm)
explode=(0,0.2,0,0) each slice has some depth from 0-1.0
For a better view we take 0.2 for the 2nd slice.
shadow=True - a shadow for better view
autoptc=%.1f%% - 1 fraction
import [Link] as plt
# %of employees in each dept
slices=[50,20,15,15]
# dept. names
depts=["Sales", "Production", "Hr", "Finance"]
# take the colours of each department
cols=["magenta","cyan", "brown", "gold"]
[Link](slices, labels=depts, colours=cols, startangle=90,
explode=(0,0.2,0,0), shadow=True, autopct="%.1f%%")
[Link]("SPMVV CO")
[Link]()
[Link]()
[Link]/in/shivakrishnavaraprasad/
Line graph
import [Link] as plt
years=["2015","2016","2017","2018","2019","2020"]
profits = [6.4,7.8,8.3,9.5,10.2,10.5]
# creat graph
[Link](years,profits, "blue")
# set title and labels
[Link]("SPMVV CO")
[Link]("years")
[Link]("Profits in Laks - Rs")
[Link]()
Thank you!
Shiva krishna vara prasad.
[Link]/in/shivakrishnavaraprasad/
Contact:
[Link]/in/shivakrishnavaraprasad/
shivakrishnavaraprasad@[Link]