LINE CHART
plt.show()
• Define the x-axis and y-axis values as lists.
• Plot them on canvas using .plot() function
• Give a name to x-axis and y-axis
using .xlabel() and .ylabel() functions.
• Give a title to your plot using .title() function.
• If more than two graphs plotted then display legend
using .legend() function
• Finally, to view your plot, we use .show() function.
Python Code Graph
import matplotlib.pyplot as plt
x=["Mon","Tue","Wed","Thu","Fri","S
at","Sun"]
y=[2000,2800,3000,2500,2300,2500,
1000]
Python Code Graph
import matplotlib.pyplot as plt
x=["Mon","Tue","Wed","Thu","Fri","S
at","Sun"]
y=[2000,2800,3000,2500,2300,2500,
1000]
plt.plot(x,y)
plt.show()
Python Code Graph
import matplotlib.pyplot as plt
x=["Mon","Tue","Wed","Thu","Fri","S
at","Sun"]
y=[2000,2800,3000,2500,2300,2500,
1000]
plt.title("Day wise Tickets sold ")
plt.show()
Python Code Graph
import matplotlib.pyplot as plt
x=["Mon","Tue","Wed","Thu","Fri","S
at","Sun"]
y=[2000,2800,3000,2500,2300,2500,
1000]
plt.title("Day wise Tickets sold ")
plt.xlabel("Days")
plt.show()
Python Code Graph
import matplotlib.pyplot as plt
x=["Mon","Tue","Wed","Thu","Fri","S
at","Sun"]
y=[2000,2800,3000,2500,2300,2500,
1000]
plt.title("Day wise Tickets sold ")
plt.xlabel("Days")
plt.ylabel("No of tickets sold")
plt.show()
Python Code Graph
import matplotlib.pyplot as plt
x=["Mon","Tue","Wed","Thu","Fri","Sa
t","Sun"]
y=[2000,2800,3000,2500,2300,2500,1
000]
plt.title("Day wise Tickets sold ")
plt.xlabel("Days")
plt.ylabel("No of tickets sold")
Plt.legend()
plt.show()
Python Code Graph
import matplotlib.pyplot as plt
x=["Mon","Tue","Wed","Thu","Fri","Sa
t","Sun"]
y=[2000,2800,3000,2500,2300,2500,1
000]
plt.title("Day wise Tickets sold ")
plt.xlabel("Days")
plt.ylabel("No of tickets sold")
Plt.legend([“SINGLE”])
plt.show()
Plt.plot(Xvalue,Yvalue)
BAR CHART
Plt.bar(Xvalue , Yvalue)
import matplotlib.pyplot as plt
langs = ['C', 'C++', 'Java', 'Python', 'PHP']
students = [23,17,35,29,12]
plt.bar(langs,students)
plt.show()
Bar customization
color Change the bar color
width Width of bar. Should be between 0 and 1
bottom Value from y-axis starts. Default is 0
align Position of x-label. Value can be ‘edge’ or ’center’
edgecolor Color of border line of bar
linewidth Width of the border line of bar
tick_label List of new labels for x axis
label Labels for legends
import matplotlib.pyplot as plt
langs = ['C', 'C++', 'Java', 'Python', 'PHP']
students = [23,17,35,29,12]
plt.bar(langs,students,width=0.25,edgecolor='r',\
linewidth=5,tick_label=['A','B','C','D','E'])
plt.show()
Plotting Multiple Bars
import numpy as np
import matplotlib.pyplot as plt
sub=['IP','ECO','ACC']
stud1=[10,15,30]
stud2=[20,16,18]
stud3=[19,13,20]
plt.bar(sub,stud1,color='b')
plt.bar(sub,stud2,color='g')
plt.bar(sub,stud3,color='r')
plt.show()
import numpy as np
import matplotlib.pyplot as plt
sub=['IP','ECO','ACC']
stud1=[10,15,30]
stud2=[20,16,18]
stud3=[19,13,20]
x=np.array([0,1,2])
plt.bar(x,stud1,color='b',width=0.25)
plt.bar(x+0.25,stud2,color='g',width=0.25)
plt.bar(x+0.50,stud3,color='r',width=0.25)
plt.xticks(x,sub)
plt.show()
import numpy as np
import matplotlib.pyplot as plt
sub=['IP','ECO','ACC']
stud1=[10,15,30]
stud2=[20,16,18]
stud3=[19,13,20]
x=np.array([0,1,2]) #x=np.arange(len(sub))
plt.bar(x,stud1,color='b',width=0.25,label="ramesh")
plt.bar(x+0.25,stud2,color='g',width=0.25,label="latha")
plt.bar(x+0.50,stud3,color='r',width=0.25,label="seema")
plt.xticks(x+.25,sub)
plt.legend()
plt.show()
HISTOGRAM
import matplotlib.pyplot as plt
import numpy as np
marks=[10,8,23,34,20,12,42,3,5,2,9,24]
plt.hist(marks)
plt.show()
import matplotlib.pyplot as plt
import numpy as np
marks=[10,8,23,34,20,12,42,3,5,2,9,24]
plt.hist(marks,edgecolor='r')
plt.show()
import matplotlib.pyplot as plt
import numpy as np
marks=[10,8,23,34,20,12,42,3,5,2,9,24]
plt.hist(marks,bins=2,edgecolor='r')
plt.show()
import matplotlib.pyplot as plt
import numpy as np
marks=[10,8,23,34,20,12,42,3,5,2,9,24]
plt.hist(marks,bins=4,ec='y')
plt.xlabel("Marks")
plt.ylabel("Count")
plt.title("Marks histogram")
plt.show()
import matplotlib.pyplot as plt
import numpy as np
marks=[10,8,23,34,20,12,42,3,5,2,9,24]
plt.hist(marks,bins=[2,6,8,12,24],ec='y',
cumulative=False)
plt.xlabel("Marks")
plt.ylabel("Count")
plt.title("Marks histogram")
plt.show()
EXERCISE
Write Python code for
import matplotlib.pyplot as plt #Statement 1
apps=["Arogya Setu","WPS Office","Cam
Scanner","WhatsApp","Telegram"]
ps_rating=[3.9,4.5,4.6,4.2,4.3]
plt.bar(apps,ps_rating,color='m',label=“App Rating”)
plt.xlabel("Apps")
plt. ylabel ("Rating")
plt. legend()
plt. show()
Write the python code for plotting graph
Code
import matplotlib.pyplot as plt
overs=['1-10','11-20','21-30','31-40','41-50']
runs=[65,55,70,60,90]
plt.xlabel('Over Range')
plt.ylabel('Runs Scored')
plt.title('India Scoring Rate')
plt.bar(overs,runs)
plt.show( )