SlideShare a Scribd company logo
1 of 12
Download to read offline
MATPLOTLIB with EXAMPLES Aug, 2018
PART 1
Eng. Mohammed Ali Ismail P a g e | 1
https://www.linkedin.com/in/mohammedismail1982/
MATPLOTLIB with EXAMPLES
PART 1
Plot – Subplot – Histograms
Matplotlib is one of the most amazing and effective Pyhton
packages for data visualization and graph representation. It’s
really a free and open-source competitive to Matlab; especially
if Matplotlib is combined with other Pyhton technical packages
like Numpy, Scipy, and Pandas.
Learning Matplotlib using simple examples is very efficient.
And I think that the lack of examples regarding Matplotlib is
the major drawback of the package documentation. This
doesn’t mean that we can get rid of referring to the official
documentation! Whatever any official reference is complicated
and needing more effort to understand; we should keep in
mind that the official reference would be our first priority and
our main source of knowledge.
Here I will start illustrating three plotting techniques with
examples using Matplotlib. These techniques are:
• Plot.
• Subplot.
• Histograms.
Of course; your comments and feedbacks are essential for best
tuning and understanding.
So; let’s go on …
MATPLOTLIB with EXAMPLES Aug, 2018
PART 1
Eng. Mohammed Ali Ismail P a g e | 2
https://www.linkedin.com/in/mohammedismail1982/
1 – Plot
Example (1/4) - Simple plot.
From this example you will also know:
• How to name the plot.
• How to name both the horizontal and vertical axis.
• How to choose whether to display or not display grid.
import matplotlib.pyplot as plt
x = list(range(101))
y = list(map(lambda n: n**2, x))
plt.plot(x, y)
plt.title("DEMO GRAPH")
plt.xlabel("Horizontal axis")
plt.ylabel("Vertical axis")
plt.grid() #If you like to show grid.
plt.show()
MATPLOTLIB with EXAMPLES Aug, 2018
PART 1
Eng. Mohammed Ali Ismail P a g e | 3
https://www.linkedin.com/in/mohammedismail1982/
Example (2/4) – Scattering plot.
From this example you will also know how to choose the color
and style of your data representation.
For more and full knowledge, kindly refer to the following link:
https://matplotlib.org/api/_as_gen/matplotlib.pyplot.plot.html#matplotlib.pyplot.p
lot
import matplotlib.pyplot as plt
Weighs = [10, 17, 23, 27, 32]
Talls = [88, 110, 150, 213, 271]
plt.title("Weighs Vs Talls")
plt.xlabel("WEIGHS")
plt.ylabel("TALLS")
plt.plot(Weighs, Talls, 'ro')
plt.show()
MATPLOTLIB with EXAMPLES Aug, 2018
PART 1
Eng. Mohammed Ali Ismail P a g e | 4
https://www.linkedin.com/in/mohammedismail1982/
Example (3/4) – Axes ranges.
In this example we need to represent the same data in Example
(2/5) with the same representation graph but we want to have
the option of setting the values of ranges for both the
horizontal and vertical axes.
Horizontal axis will be in range: 0 – 50.
Vertical axis will be in range: 100 – 300.
plt.xlabel("WEIGHS")
plt.ylabel("TALLS")
plt.plot(Weighs, Talls, 'bo')
plt.axis([0,50,100,300])
plt.show()
MATPLOTLIB with EXAMPLES Aug, 2018
PART 1
Eng. Mohammed Ali Ismail P a g e | 5
https://www.linkedin.com/in/mohammedismail1982/
Example (4/4) – Multiple representations in one graph.
x = list(range(101))
xblue = list(map(lambda n: n*2, x))
xgreen = list(map(lambda n: n*3, x))
xred = list(map(lambda n: n*4, x))
plt.plot(x, xblue, 'b', x, xgreen, 'g', x, xred, 'r')
plt.show()
MATPLOTLIB with EXAMPLES Aug, 2018
PART 1
Eng. Mohammed Ali Ismail P a g e | 6
https://www.linkedin.com/in/mohammedismail1982/
2 – Subplot
Sometimes we need to keep more than one data graph on eye.
Subplot is facilitating this option. You can view several graphs
simultaneously in horizontal view, vertical view, or matrix
view. You have only to determine the dimensions of your view
model and the location index you like of each graph.
Kindly read the comments in the code examples for boosting
and fixing the concepts in your mind!
Example (1/3) – Two plots in horizontal subplot.
x1 = list(range(101))
y1 = list(map(lambda k: k**2, x1))
x2 = list(range(51))
y2 = list(map(lambda k: k*5, x2))
plt.subplot(1,2,1)
MATPLOTLIB with EXAMPLES Aug, 2018
PART 1
Eng. Mohammed Ali Ismail P a g e | 7
https://www.linkedin.com/in/mohammedismail1982/
plt.plot(x1,y1)
plt.title("Two plots in HORIZONTAL subplot")
plt.subplot(1,2,2)
plt.plot(x2,y2)
plt.show()
#(1,2,1)===> 1 row, 2 columns, position is in index 1
#(1,2,2)===> 1 row, 2 columns, position is in index 2
Example (2/3) – Two plots in vertical subplot.
We will use the same datasets used in Example (2/5).
plt.subplot(2,1,1)
plt.plot(x1,y1)
plt.title("Two plots in VERTICAL subplot")
plt.subplot(2,1,2)
plt.plot(x2,y2)
MATPLOTLIB with EXAMPLES Aug, 2018
PART 1
Eng. Mohammed Ali Ismail P a g e | 8
https://www.linkedin.com/in/mohammedismail1982/
plt.show()
#(2,1,1)===> 2 row, 1 columns, position is in index 1
#(2,1,2)===> 2 row, 1 columns, position is in index 2
Example (3/3) – Multiple plots in matrix subplot.
x1 = list(range(101))
x = list(range(-100, 101))
yr = list(map(lambda k: k**2, x))
yg = list(map(lambda k: k**3, x))
yb = list(map(lambda k: k**4, x))
yy = list(map(lambda k: k**5, x))
plt.subplot(2,2,1)
plt.plot(x,yr,'r')
plt.subplot(2,2,2)
plt.plot(x,yg,'g')
plt.subplot(2,2,3)
MATPLOTLIB with EXAMPLES Aug, 2018
PART 1
Eng. Mohammed Ali Ismail P a g e | 9
https://www.linkedin.com/in/mohammedismail1982/
plt.plot(x,yb,'b')
plt.subplot(2,2,4)
plt.plot(x,yy,'y')
plt.show()
#(2,2,1) ===> 2 rows, 2 columns, position is in index 1
#(2,2,2) ===> 2 rows, 2 columns, position is in index 2
#(2,2,3) ===> 2 rows, 2 columns, position is in index 3
#(2,2,4) ===> 2 rows, 2 columns, position is in index 4
3 – Histogram
Let’s have some fundamental talk about histogram.
Suppose that there is a 23 students in a class. The 23 students
passed a mathematics exam which its full score is 100.
Now we are having the scores the students achieved in the
exam as follow:
MATPLOTLIB with EXAMPLES Aug, 2018
PART 1
Eng. Mohammed Ali Ismail P a g e | 10
https://www.linkedin.com/in/mohammedismail1982/
[99, 40, 82, 6, 38, 85, 87, 94, 0, 80, 18, 83, 26, 49, 81, 59, 68, 45,
9, 13, 17, 5, 57]
Good! Now let’s create the following table:
How many students achieved scores in range … Number of
students is:
From 0 to less than 10? 4
From 10 to less than 20? 3
From 20 to less than 30? 1
From 30 to less than 40? 1
From 40 to less than 50? 3
From 50 to less than 60? 2
From 60 to less than 70? 1
From 70 to less than 80? 0
From 80 to less than 90? 6
From 90 to 100? 2
Using histogram related terms; ranges in the way as mentioned
in the table above are called bins. The number of occurrences -
which in our example case expressing the number of students –
is called frequency.
A frequency vs bins graph is called histogram!
We configured our example to have 10 bins. No problem if you
choose to configure it to have 20, 15, 2, 5 or any number of bins
you want. Worthless to say that more bins you have, more data
resolution you obtain.
Let’s move to Matplotlib examples!
MATPLOTLIB with EXAMPLES Aug, 2018
PART 1
Eng. Mohammed Ali Ismail P a g e | 11
https://www.linkedin.com/in/mohammedismail1982/
Example (1/2) – Basic histogram example.
If you don’t determine the number of bins, it will be set to be
10 by default. In this example we will not determine the
number of bins. But we will do in Example (2/2).
scores = [99, 40, 82, 6, 38, 85, 87, 94, 0, 80, 18, 83, 26, 49,
81, 59, 68, 45, 9, 13, 17, 5, 57]
plt.hist(scores)
plt.grid()
plt.show()
Example (2/2) – Specifying the number of bins.
Now it’s time to configure our desired number of bins which
will be 20 in this example. We will use the same dataset
mentioned in Example (1/2) above.
plt.hist(scores, 20)
plt.grid()
plt.show()
MATPLOTLIB with EXAMPLES Aug, 2018
PART 1
Eng. Mohammed Ali Ismail P a g e | 12
https://www.linkedin.com/in/mohammedismail1982/

More Related Content

What's hot (6)

The matplotlib Library
The matplotlib LibraryThe matplotlib Library
The matplotlib Library
 
Pre-Cal 30S January 20, 2009
Pre-Cal 30S January 20, 2009Pre-Cal 30S January 20, 2009
Pre-Cal 30S January 20, 2009
 
Matplotlib
MatplotlibMatplotlib
Matplotlib
 
Modelling principles in excel
Modelling principles in excelModelling principles in excel
Modelling principles in excel
 
CS8391 Data Structures Part B Questions Anna University
CS8391 Data Structures Part B Questions Anna UniversityCS8391 Data Structures Part B Questions Anna University
CS8391 Data Structures Part B Questions Anna University
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
 

Similar to Simplifying Matplotlib by examples

PYTHON-Chapter 4-Plotting and Data Science PyLab - MAULIK BORSANIYA
PYTHON-Chapter 4-Plotting and Data Science  PyLab - MAULIK BORSANIYAPYTHON-Chapter 4-Plotting and Data Science  PyLab - MAULIK BORSANIYA
PYTHON-Chapter 4-Plotting and Data Science PyLab - MAULIK BORSANIYAMaulik Borsaniya
 
Introduction to R Graphics with ggplot2
Introduction to R Graphics with ggplot2Introduction to R Graphics with ggplot2
Introduction to R Graphics with ggplot2izahn
 
Informatics Practices (new) solution CBSE 2021, Compartment, improvement ex...
Informatics Practices (new) solution CBSE  2021, Compartment,  improvement ex...Informatics Practices (new) solution CBSE  2021, Compartment,  improvement ex...
Informatics Practices (new) solution CBSE 2021, Compartment, improvement ex...FarhanAhmade
 
Data visualization using R
Data visualization using RData visualization using R
Data visualization using RUmmiya Mohammedi
 
Data Visualization 2020_21
Data Visualization 2020_21Data Visualization 2020_21
Data Visualization 2020_21Sangita Panchal
 
Data visualization using py plot part i
Data visualization using py plot part iData visualization using py plot part i
Data visualization using py plot part iTutorialAICSIP
 
RDataMining slides-regression-classification
RDataMining slides-regression-classificationRDataMining slides-regression-classification
RDataMining slides-regression-classificationYanchang Zhao
 
Functional programming-advantages
Functional programming-advantagesFunctional programming-advantages
Functional programming-advantagesSergei Winitzki
 
Linear Regression (Machine Learning)
Linear Regression (Machine Learning)Linear Regression (Machine Learning)
Linear Regression (Machine Learning)Omkar Rane
 
maXbox starter68 machine learning VI
maXbox starter68 machine learning VImaXbox starter68 machine learning VI
maXbox starter68 machine learning VIMax Kleiner
 
Customer Segmentation with R - Deep Dive into flexclust
Customer Segmentation with R - Deep Dive into flexclustCustomer Segmentation with R - Deep Dive into flexclust
Customer Segmentation with R - Deep Dive into flexclustJim Porzak
 
DutchMLSchool. Automating Decision Making
DutchMLSchool. Automating Decision MakingDutchMLSchool. Automating Decision Making
DutchMLSchool. Automating Decision MakingBigML, Inc
 
Lecture 1 Pandas Basics.pptx machine learning
Lecture 1 Pandas Basics.pptx machine learningLecture 1 Pandas Basics.pptx machine learning
Lecture 1 Pandas Basics.pptx machine learningmy6305874
 
모듈형 패키지를 활용한 나만의 기계학습 모형 만들기 - 회귀나무모형을 중심으로
모듈형 패키지를 활용한 나만의 기계학습 모형 만들기 - 회귀나무모형을 중심으로 모듈형 패키지를 활용한 나만의 기계학습 모형 만들기 - 회귀나무모형을 중심으로
모듈형 패키지를 활용한 나만의 기계학습 모형 만들기 - 회귀나무모형을 중심으로 r-kor
 
Analysis of Microsoft, Google and Apple Stock Prices
Analysis of Microsoft, Google and Apple Stock PricesAnalysis of Microsoft, Google and Apple Stock Prices
Analysis of Microsoft, Google and Apple Stock PricesHira Nadeem
 

Similar to Simplifying Matplotlib by examples (20)

PYTHON-Chapter 4-Plotting and Data Science PyLab - MAULIK BORSANIYA
PYTHON-Chapter 4-Plotting and Data Science  PyLab - MAULIK BORSANIYAPYTHON-Chapter 4-Plotting and Data Science  PyLab - MAULIK BORSANIYA
PYTHON-Chapter 4-Plotting and Data Science PyLab - MAULIK BORSANIYA
 
Introduction to R Graphics with ggplot2
Introduction to R Graphics with ggplot2Introduction to R Graphics with ggplot2
Introduction to R Graphics with ggplot2
 
Informatics Practices (new) solution CBSE 2021, Compartment, improvement ex...
Informatics Practices (new) solution CBSE  2021, Compartment,  improvement ex...Informatics Practices (new) solution CBSE  2021, Compartment,  improvement ex...
Informatics Practices (new) solution CBSE 2021, Compartment, improvement ex...
 
Data visualization using R
Data visualization using RData visualization using R
Data visualization using R
 
Data Visualization 2020_21
Data Visualization 2020_21Data Visualization 2020_21
Data Visualization 2020_21
 
12-IP.pdf
12-IP.pdf12-IP.pdf
12-IP.pdf
 
Data visualization using py plot part i
Data visualization using py plot part iData visualization using py plot part i
Data visualization using py plot part i
 
RDataMining slides-regression-classification
RDataMining slides-regression-classificationRDataMining slides-regression-classification
RDataMining slides-regression-classification
 
Functional programming-advantages
Functional programming-advantagesFunctional programming-advantages
Functional programming-advantages
 
ffm vignette 08 29 16
ffm vignette 08 29 16ffm vignette 08 29 16
ffm vignette 08 29 16
 
Linear Regression (Machine Learning)
Linear Regression (Machine Learning)Linear Regression (Machine Learning)
Linear Regression (Machine Learning)
 
maXbox starter68 machine learning VI
maXbox starter68 machine learning VImaXbox starter68 machine learning VI
maXbox starter68 machine learning VI
 
Customer Segmentation with R - Deep Dive into flexclust
Customer Segmentation with R - Deep Dive into flexclustCustomer Segmentation with R - Deep Dive into flexclust
Customer Segmentation with R - Deep Dive into flexclust
 
An Evaluation of Models for Runtime Approximation in Link Discovery
An Evaluation of Models for Runtime Approximation in Link DiscoveryAn Evaluation of Models for Runtime Approximation in Link Discovery
An Evaluation of Models for Runtime Approximation in Link Discovery
 
DutchMLSchool. Automating Decision Making
DutchMLSchool. Automating Decision MakingDutchMLSchool. Automating Decision Making
DutchMLSchool. Automating Decision Making
 
Lecture 1 Pandas Basics.pptx machine learning
Lecture 1 Pandas Basics.pptx machine learningLecture 1 Pandas Basics.pptx machine learning
Lecture 1 Pandas Basics.pptx machine learning
 
Generative AI for Reengineering Variants into Software Product Lines: An Expe...
Generative AI for Reengineering Variants into Software Product Lines: An Expe...Generative AI for Reengineering Variants into Software Product Lines: An Expe...
Generative AI for Reengineering Variants into Software Product Lines: An Expe...
 
모듈형 패키지를 활용한 나만의 기계학습 모형 만들기 - 회귀나무모형을 중심으로
모듈형 패키지를 활용한 나만의 기계학습 모형 만들기 - 회귀나무모형을 중심으로 모듈형 패키지를 활용한 나만의 기계학습 모형 만들기 - 회귀나무모형을 중심으로
모듈형 패키지를 활용한 나만의 기계학습 모형 만들기 - 회귀나무모형을 중심으로
 
Analysis of Microsoft, Google and Apple Stock Prices
Analysis of Microsoft, Google and Apple Stock PricesAnalysis of Microsoft, Google and Apple Stock Prices
Analysis of Microsoft, Google and Apple Stock Prices
 
2013-June: 7th Semester ISE Question Papers
2013-June: 7th  Semester ISE Question Papers2013-June: 7th  Semester ISE Question Papers
2013-June: 7th Semester ISE Question Papers
 

Recently uploaded

20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdf20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdfHuman37
 
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...dajasot375
 
Brighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data StorytellingBrighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data StorytellingNeil Barnes
 
Data Science Project: Advancements in Fetal Health Classification
Data Science Project: Advancements in Fetal Health ClassificationData Science Project: Advancements in Fetal Health Classification
Data Science Project: Advancements in Fetal Health ClassificationBoston Institute of Analytics
 
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改atducpo
 
Unveiling Insights: The Role of a Data Analyst
Unveiling Insights: The Role of a Data AnalystUnveiling Insights: The Role of a Data Analyst
Unveiling Insights: The Role of a Data AnalystSamantha Rae Coolbeth
 
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...Florian Roscheck
 
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptxAnupama Kate
 
Ukraine War presentation: KNOW THE BASICS
Ukraine War presentation: KNOW THE BASICSUkraine War presentation: KNOW THE BASICS
Ukraine War presentation: KNOW THE BASICSAishani27
 
VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...
VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...
VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...Suhani Kapoor
 
Spark3's new memory model/management
Spark3's new memory model/managementSpark3's new memory model/management
Spark3's new memory model/managementakshesh doshi
 
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...Suhani Kapoor
 
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Callshivangimorya083
 
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...Sapana Sha
 
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...Jack DiGiovanna
 
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptdokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptSonatrach
 

Recently uploaded (20)

20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdf20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdf
 
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
 
Brighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data StorytellingBrighton SEO | April 2024 | Data Storytelling
Brighton SEO | April 2024 | Data Storytelling
 
Data Science Project: Advancements in Fetal Health Classification
Data Science Project: Advancements in Fetal Health ClassificationData Science Project: Advancements in Fetal Health Classification
Data Science Project: Advancements in Fetal Health Classification
 
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
代办国外大学文凭《原版美国UCLA文凭证书》加州大学洛杉矶分校毕业证制作成绩单修改
 
Unveiling Insights: The Role of a Data Analyst
Unveiling Insights: The Role of a Data AnalystUnveiling Insights: The Role of a Data Analyst
Unveiling Insights: The Role of a Data Analyst
 
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
 
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
 
100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx
 
Ukraine War presentation: KNOW THE BASICS
Ukraine War presentation: KNOW THE BASICSUkraine War presentation: KNOW THE BASICS
Ukraine War presentation: KNOW THE BASICS
 
VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...
VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...
VIP High Class Call Girls Bikaner Anushka 8250192130 Independent Escort Servi...
 
VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...
VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...
VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...
 
Spark3's new memory model/management
Spark3's new memory model/managementSpark3's new memory model/management
Spark3's new memory model/management
 
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
 
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in Kishangarh
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in  KishangarhDelhi 99530 vip 56974 Genuine Escort Service Call Girls in  Kishangarh
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in Kishangarh
 
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls Punjabi Bagh 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
 
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
 
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
 
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptdokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
 
Russian Call Girls Dwarka Sector 15 💓 Delhi 9999965857 @Sabina Modi VVIP MODE...
Russian Call Girls Dwarka Sector 15 💓 Delhi 9999965857 @Sabina Modi VVIP MODE...Russian Call Girls Dwarka Sector 15 💓 Delhi 9999965857 @Sabina Modi VVIP MODE...
Russian Call Girls Dwarka Sector 15 💓 Delhi 9999965857 @Sabina Modi VVIP MODE...
 

Simplifying Matplotlib by examples

  • 1. MATPLOTLIB with EXAMPLES Aug, 2018 PART 1 Eng. Mohammed Ali Ismail P a g e | 1 https://www.linkedin.com/in/mohammedismail1982/ MATPLOTLIB with EXAMPLES PART 1 Plot – Subplot – Histograms Matplotlib is one of the most amazing and effective Pyhton packages for data visualization and graph representation. It’s really a free and open-source competitive to Matlab; especially if Matplotlib is combined with other Pyhton technical packages like Numpy, Scipy, and Pandas. Learning Matplotlib using simple examples is very efficient. And I think that the lack of examples regarding Matplotlib is the major drawback of the package documentation. This doesn’t mean that we can get rid of referring to the official documentation! Whatever any official reference is complicated and needing more effort to understand; we should keep in mind that the official reference would be our first priority and our main source of knowledge. Here I will start illustrating three plotting techniques with examples using Matplotlib. These techniques are: • Plot. • Subplot. • Histograms. Of course; your comments and feedbacks are essential for best tuning and understanding. So; let’s go on …
  • 2. MATPLOTLIB with EXAMPLES Aug, 2018 PART 1 Eng. Mohammed Ali Ismail P a g e | 2 https://www.linkedin.com/in/mohammedismail1982/ 1 – Plot Example (1/4) - Simple plot. From this example you will also know: • How to name the plot. • How to name both the horizontal and vertical axis. • How to choose whether to display or not display grid. import matplotlib.pyplot as plt x = list(range(101)) y = list(map(lambda n: n**2, x)) plt.plot(x, y) plt.title("DEMO GRAPH") plt.xlabel("Horizontal axis") plt.ylabel("Vertical axis") plt.grid() #If you like to show grid. plt.show()
  • 3. MATPLOTLIB with EXAMPLES Aug, 2018 PART 1 Eng. Mohammed Ali Ismail P a g e | 3 https://www.linkedin.com/in/mohammedismail1982/ Example (2/4) – Scattering plot. From this example you will also know how to choose the color and style of your data representation. For more and full knowledge, kindly refer to the following link: https://matplotlib.org/api/_as_gen/matplotlib.pyplot.plot.html#matplotlib.pyplot.p lot import matplotlib.pyplot as plt Weighs = [10, 17, 23, 27, 32] Talls = [88, 110, 150, 213, 271] plt.title("Weighs Vs Talls") plt.xlabel("WEIGHS") plt.ylabel("TALLS") plt.plot(Weighs, Talls, 'ro') plt.show()
  • 4. MATPLOTLIB with EXAMPLES Aug, 2018 PART 1 Eng. Mohammed Ali Ismail P a g e | 4 https://www.linkedin.com/in/mohammedismail1982/ Example (3/4) – Axes ranges. In this example we need to represent the same data in Example (2/5) with the same representation graph but we want to have the option of setting the values of ranges for both the horizontal and vertical axes. Horizontal axis will be in range: 0 – 50. Vertical axis will be in range: 100 – 300. plt.xlabel("WEIGHS") plt.ylabel("TALLS") plt.plot(Weighs, Talls, 'bo') plt.axis([0,50,100,300]) plt.show()
  • 5. MATPLOTLIB with EXAMPLES Aug, 2018 PART 1 Eng. Mohammed Ali Ismail P a g e | 5 https://www.linkedin.com/in/mohammedismail1982/ Example (4/4) – Multiple representations in one graph. x = list(range(101)) xblue = list(map(lambda n: n*2, x)) xgreen = list(map(lambda n: n*3, x)) xred = list(map(lambda n: n*4, x)) plt.plot(x, xblue, 'b', x, xgreen, 'g', x, xred, 'r') plt.show()
  • 6. MATPLOTLIB with EXAMPLES Aug, 2018 PART 1 Eng. Mohammed Ali Ismail P a g e | 6 https://www.linkedin.com/in/mohammedismail1982/ 2 – Subplot Sometimes we need to keep more than one data graph on eye. Subplot is facilitating this option. You can view several graphs simultaneously in horizontal view, vertical view, or matrix view. You have only to determine the dimensions of your view model and the location index you like of each graph. Kindly read the comments in the code examples for boosting and fixing the concepts in your mind! Example (1/3) – Two plots in horizontal subplot. x1 = list(range(101)) y1 = list(map(lambda k: k**2, x1)) x2 = list(range(51)) y2 = list(map(lambda k: k*5, x2)) plt.subplot(1,2,1)
  • 7. MATPLOTLIB with EXAMPLES Aug, 2018 PART 1 Eng. Mohammed Ali Ismail P a g e | 7 https://www.linkedin.com/in/mohammedismail1982/ plt.plot(x1,y1) plt.title("Two plots in HORIZONTAL subplot") plt.subplot(1,2,2) plt.plot(x2,y2) plt.show() #(1,2,1)===> 1 row, 2 columns, position is in index 1 #(1,2,2)===> 1 row, 2 columns, position is in index 2 Example (2/3) – Two plots in vertical subplot. We will use the same datasets used in Example (2/5). plt.subplot(2,1,1) plt.plot(x1,y1) plt.title("Two plots in VERTICAL subplot") plt.subplot(2,1,2) plt.plot(x2,y2)
  • 8. MATPLOTLIB with EXAMPLES Aug, 2018 PART 1 Eng. Mohammed Ali Ismail P a g e | 8 https://www.linkedin.com/in/mohammedismail1982/ plt.show() #(2,1,1)===> 2 row, 1 columns, position is in index 1 #(2,1,2)===> 2 row, 1 columns, position is in index 2 Example (3/3) – Multiple plots in matrix subplot. x1 = list(range(101)) x = list(range(-100, 101)) yr = list(map(lambda k: k**2, x)) yg = list(map(lambda k: k**3, x)) yb = list(map(lambda k: k**4, x)) yy = list(map(lambda k: k**5, x)) plt.subplot(2,2,1) plt.plot(x,yr,'r') plt.subplot(2,2,2) plt.plot(x,yg,'g') plt.subplot(2,2,3)
  • 9. MATPLOTLIB with EXAMPLES Aug, 2018 PART 1 Eng. Mohammed Ali Ismail P a g e | 9 https://www.linkedin.com/in/mohammedismail1982/ plt.plot(x,yb,'b') plt.subplot(2,2,4) plt.plot(x,yy,'y') plt.show() #(2,2,1) ===> 2 rows, 2 columns, position is in index 1 #(2,2,2) ===> 2 rows, 2 columns, position is in index 2 #(2,2,3) ===> 2 rows, 2 columns, position is in index 3 #(2,2,4) ===> 2 rows, 2 columns, position is in index 4 3 – Histogram Let’s have some fundamental talk about histogram. Suppose that there is a 23 students in a class. The 23 students passed a mathematics exam which its full score is 100. Now we are having the scores the students achieved in the exam as follow:
  • 10. MATPLOTLIB with EXAMPLES Aug, 2018 PART 1 Eng. Mohammed Ali Ismail P a g e | 10 https://www.linkedin.com/in/mohammedismail1982/ [99, 40, 82, 6, 38, 85, 87, 94, 0, 80, 18, 83, 26, 49, 81, 59, 68, 45, 9, 13, 17, 5, 57] Good! Now let’s create the following table: How many students achieved scores in range … Number of students is: From 0 to less than 10? 4 From 10 to less than 20? 3 From 20 to less than 30? 1 From 30 to less than 40? 1 From 40 to less than 50? 3 From 50 to less than 60? 2 From 60 to less than 70? 1 From 70 to less than 80? 0 From 80 to less than 90? 6 From 90 to 100? 2 Using histogram related terms; ranges in the way as mentioned in the table above are called bins. The number of occurrences - which in our example case expressing the number of students – is called frequency. A frequency vs bins graph is called histogram! We configured our example to have 10 bins. No problem if you choose to configure it to have 20, 15, 2, 5 or any number of bins you want. Worthless to say that more bins you have, more data resolution you obtain. Let’s move to Matplotlib examples!
  • 11. MATPLOTLIB with EXAMPLES Aug, 2018 PART 1 Eng. Mohammed Ali Ismail P a g e | 11 https://www.linkedin.com/in/mohammedismail1982/ Example (1/2) – Basic histogram example. If you don’t determine the number of bins, it will be set to be 10 by default. In this example we will not determine the number of bins. But we will do in Example (2/2). scores = [99, 40, 82, 6, 38, 85, 87, 94, 0, 80, 18, 83, 26, 49, 81, 59, 68, 45, 9, 13, 17, 5, 57] plt.hist(scores) plt.grid() plt.show() Example (2/2) – Specifying the number of bins. Now it’s time to configure our desired number of bins which will be 20 in this example. We will use the same dataset mentioned in Example (1/2) above. plt.hist(scores, 20) plt.grid() plt.show()
  • 12. MATPLOTLIB with EXAMPLES Aug, 2018 PART 1 Eng. Mohammed Ali Ismail P a g e | 12 https://www.linkedin.com/in/mohammedismail1982/