Python :
Decision Making and Loop
Punit A. Lathiya
Assistant Professor
EC Department
GEC Rajkot
Syllabus : 3151108
Syllabus : 3151108
Teaching and Examination Scheme
Books
Online Courses
• https://www.coursera.org/learn/python-programming
• https://nptel.ac.in/courses/106/106/106106145/
• https://nptel.ac.in/courses/106/106/106106182/
Index
• Modules to import
• Types of Plots
• Multiple Plots
• Labeling
Introduction
• matplotlib stands for mathematical plotting library.
• Author : John D. Hunter
• Open Source
• It supports both interactive and non-interactive modes
of plotting and can save images in a variety of formats
like (JPEG, PS, PDF, PNG etc).
• Two package within matplotlib exists.,
▫ matplotlib.pylab and
▫ matplotlib.pyplot
Introduction
• To install matplotlib:
▫ open command prompt:
pip install matplotlib
• Importing matplotlib:
▫ import matplotlib
▫ import matplotlib.pyplot as plt
Plot Types Plot Styling Plot Labels
• Line plot • Line plot • Xlabel
• Scatter plot ▫ Line width • Ylabel
• Bar chart ▫ Line Marker • Title
• Pie chart
▫ Line Color • Grid
• Histogram
• Multiple plot in one
• Multiple plot in subplot
Features of Plot
• Figure
• Axes
• Xlabel
• Ylabel
• Title
• Grid
• Legend
• Linestyle, Linecolor, Markers
Line Plot
Multiple Plots – within same plot
Marker Type
Multiple Plots – Separate plots
Figure-1
Figure-2
Multiple Plots – Using Subplot
Syntax : pt.subplot(row, col, fig_num)
Figure-1
Sine Cosine
Subplot(2,2,1) Subplot(2,2,2)
Tan Power
Subplot(2,2,3) Subplot(2,2,4)
Multiple Plots – Using Subplot
pt.plot(x,y,'-x',linewidth=2,color='red')
pt.legend(["square","cube",], loc = "upper right" )
Method for applying Line Colors
b: blue
r: red
g: green
c: cyan
m: magenta
y: yellow
k: black
w: white
Method for applying Line style
• Using Linestyle Command:
▫ Solid
▫ Dashed
▫ Dotted
▫ DashDot
Method for applying Line style
• Using ls command:
▫ ‘-’ : Solid
▫ ‘--’ : Dashed
▫ ‘:’ : Dotted
▫ ‘-.’ : Dashdot
Various plot Types
• import matplotlib.pyplot as plt
• x = [1,2,3,4,5,6,7,8,9,10]
• y = [2.4,5.9,8,9.5,7,5.6,8.2,4.5,2,5]
• # Line Plot
• plt.plot(x,y)
• plt.title('Line Graph')
• plt.show()
Various plot Types
• import matplotlib.pyplot as plt
• x = [1,2,3,4,5,6,7,8,9,10]
• y = [2.4,5.9,8,9.5,7,5.6,8.2,4.5,2,5]
• # Bar Plot
• plt.bar(x,y)
• plt.title('Bar Graph')
• plt.show()
Various plot Types
• import matplotlib.pyplot as plt
• # Pie Plot
• fruit = 'APPLE', 'BANANA',
'MANGO', 'KIVI'
• num = [15, 30, 45, 10]
• plt.pie(num,labels=fruit)
• plt.title('Pie Graph')
• plt.show()
Various plot Types
• import matplotlib.pyplot as plt
• x = [1,2,3,4,5,6,7,8,9,10]
• y = [2.4,5.9,8,9.5,7,5.6,8.2,4.5,2,5]
• #Scatter Plot
• plt.scatter(x,y)
• plt.title('Scatter Graph')
• plt.show()
Various plot Types
• import matplotlib.pyplot as plt
• x = [1,2,3,4,5,6,7,8,9,10]
• y = [2.4,5.9,8,9.5,7,5.6,8.2,4.5,2,5]
• #Histogram Plot
• plt.hist(y)
• plt.title('Histogram Plot')
• plt.show()
Thank You