SlideShare a Scribd company logo
Matplotlib
Matplotlib
● Visualizing data is crucial to quickly
understanding trends and relationships in
your dataset.
● Matplotlib is one of the most popular
libraries for plotting with Python.
Matplotlib
● Matplotlib is known as the “Grandfather” of
plotting and visualization libraries for
Python.
● Many other visualization libraries are built
directly off of Matplotlib (e.g. seaborn and
pandas built-in visualization).
Matplotlib
● Matplotlib is heavily inspired by the
plotting functions of the MatLab
programming language.
● It allows for the creation of almost any plot
type and heavy customization.
Matplotlib
● This ability to heavily customize a plot
comes at a trade-off for beginners, since it
can be confusing to learn the Matplolib
syntax at first.
● This is mainly due to the fact that there are
actually two separate approaches to
creating plots, functional based methods
and OOP based methods.
Matplotlib
● This Matplotlib section seeks to clear up
any confusion by clearly separating out
these two approaches.
○ Matplotlib Basics
■ Functional Method
○ Matplotlib Figures and Subplots
■ OOP Method
Matplotlib
● Topics Covered
○ Matplotlib Basics and Functions
○ Matplotlib Figures
○ Matplotlib Subplots
○ Matplotlib Styling
○ Exercise Questions and Solutions
Matplotlib
● Specialized plot types such as histograms
won’t be covered with matplotlib, since we
will later learn how to use seaborn to easily
create statistical plots.
● It is important to learn matplotlib first
however, since seaborn builds directly off of
Matplotlib.
Matplotlib
● Throughout this section we will be
referencing the excellent Matplotlib online
documentation:
○ https://matplotlib.org/
● As well as the gallery of example plots
and codes (very useful!)
○ https://matplotlib.org/gallery.html
Matplotlib
● Two main goals with Matplotlib:
○ Be able to plot out a functional
relationship:
■ y = 2x
○ Be able to plot out a relationship
between raw data points:
■ x = [1,2,3,4]
■ y = [2,4,6,8]
Let’s get started!
Matplotlib Basics
Matplotlib
● The most basic way to use Matplotlib is
through the function plot calls:
○ plt.plot(x,y)
● These function calls are simple to use, but
don’t allow for very high degrees of control.
Matplotlib
● We recommend using these simple
plt.plot() calls for quickly visualizing
relationships and data.
● Later on we will explore the more robust
OOP Matplotlib Figure API.
Matplotlib
● Note!
○ There are slight differences in displaying
plots within a notebook versus running a
python script.
○ If you are running .py scripts instead of
.ipynb notebooks, you will need to add
the plt.show() command discussed in
this video.
Matplotlib
Figure Object
PART ONE : UNDERSTANDING THE FIGURE
Matplotlib
● The more comprehensive Matplotlib OOP
API makes use of a Figure object.
● We then add axes to this Figure object and
then plot on those axes.
● This allows for very robust controls over the
entire plot.
Matplotlib
● Let’s quickly visually build an
understanding of the Figure object before
coding it with Python…
● Note:
○ The Figure object we’re about to
show is technically not visible until
you add axes to it.
Matplotlib
● plt.figure()
<Figure size 432x288 with 0 Axes>
Matplotlib
● plt.figure(figsize=(10,10))
<Figure size 720x720 with 0 Axes>
Matplotlib
● fig = plt.figure()
● Blank canvas, waiting for a set of axes for
plotting.
<Figure size 432x288 with 0 Axes>
Matplotlib
● fig = plt.figure()
axes = fig.add_axes([0, 0, 1, 1])
Matplotlib
● fig = plt.figure()
axes = fig.add_axes([0, 0, 1, 1])
FIGURE
Matplotlib
● fig = plt.figure()
axes = fig.add_axes([0, 0, 1, 1])
FIGURE
(0,0)
(x,y)
Lower Left Corner of Axes
Matplotlib
● fig = plt.figure()
axes = fig.add_axes([0, 1, 1, 1])
FIGURE
(0,1)
(x,y)
Lower Left Corner of Axes
Matplotlib
● fig = plt.figure()
axes = fig.add_axes([ 0.5, 0.5, 1, 1])
FIGURE
(0.5,0.5)
(x,y)
Lower Left Corner of Axes
Matplotlib
● fig = plt.figure()
axes = fig.add_axes([0, 0, 1, 1])
FIGURE
(width,height)
of Axes
Matplotlib
● fig = plt.figure()
axes = fig.add_axes([0, 0, 1, 1])
(width,height)
of Axes
Matplotlib
● fig = plt.figure()
axes = fig.add_axes([0, 0, 0.5, 0.5])
FIGURE
(width,height)
of Axes
Matplotlib
● fig = plt.figure()
axes = fig.add_axes([0, 0, 0.5, 0.5])
(width,height)
of Axes
Matplotlib
● fig = plt.figure()
axes = fig.add_axes([0, 0, 0.5, 1])
FIGURE
(width,height)
of Axes
Matplotlib
● fig = plt.figure()
axes = fig.add_axes([0, 0, 0.5, 1])
(width,height)
of Axes
Matplotlib
● fig = plt.figure()
axes = fig.add_axes([0, 0, 1, 1])
axes.plot(x, y)
Matplotlib
● fig = plt.figure()
axes = fig.add_axes([0, 0, 1, 1])
axes.plot(x, y)
Matplotlib
● fig = plt.figure()
axes = fig.add_axes([0, 0, 1, 1])
axes.plot(x, y)
Matplotlib
● fig = plt.figure()
axes = fig.add_axes([0, 0, 1, 1])
axes.plot(x, y)
Matplotlib
● This methodology allows us to add in
multiple axes as well as move and resize
the axes.
Matplotlib
● In theory we could set axes side by side
using plt.figure() calls, but typically it is
easier to use plt.subplots() function calls for
this.
● We’ll explore multiple side by side plots in a
future lecture, for now let’s explore the
Figure object methodology for Matplotlib!
Matplotlib
Figure Object
PART TWO: IMPLEMENTING FIGURES AND
AXES
Matplotlib
Figure Object
PART THREE: FIGURE PARAMETERS
Matplotlib SubPlots
Matplotlib
● In theory we could create a Figure object
and then manually add and arrange sets of
axes to line up multiple plots side by side.
● However, Matplotlib comes with a pre-
configured function call plt.subplots() that
automatically does this for us!
Matplotlib
● The plt.subplots() call allows us to easily
create Figure and Axes objects in side by
side formations.
● The plt.subplots() command returns a tuple
containing the Figure canvas and then a
numpy array holding the axes objects.
Matplotlib
● fig, axes = plt.subplots(nrows=1, ncols=2)
Matplotlib
● fig, axes = plt.subplots(nrows=2, ncols=2)
Matplotlib
● plt.subplots() returns a tuple which by
common convention we label (fig,axes):
○ fig
■ This is the entire Figure canvas.
○ axes
■ This is a numpy array holding each
of the axes according to position in
the overall canvas.
Matplotlib
● Let’s explore how to use plt.subplots() to
easily create and align multiple plots!
Matplotlib Styling
PART ONE: LEGENDS
Matplotlib
● Matplotlib offers very robust styling
functions that allow us to edit colors,
legends, line widths, markers, and much
more!
● Note: Due to the wide amount of possible
variations, we will be copying and pasting
from the lecture notebook to save typing
time in the video.
Matplotlib
● Main Styling Discussed:
○ Legends
○ Visual Styling
■ Colors
■ Editing Lines
● Colors, Widths, Styles
■ Editing Markers
● Colors, Size, Styles, Edges
Matplotlib
● Let’s begin with adding legends!
Matplotlib Styling
PART TWO: VISUAL STYLING
Additional Matplotlib
Commands
Matplotlib
● Matplotlib is a huge library!
● We’ve added a notebook with some
additional concepts you may want to
explore on your own.
● We don’t use these concepts in the course
however, so feel free to skip this notebook
for now.
Matplotlib
● An important note is that almost any
Matplotlib question you can think of
already has an answer in StackOverflow or
an example in the Matplotlib gallery.
● Leverage these many examples to your
advantage and do not waste energy and
time into memorizing esoteric commands!
Matplotlib
● Section
Matplotlib Exercises
Solutions

More Related Content

PPTX
Unit III for data science engineering.pptx
PPTX
Introduction to matplotlib
PPTX
Unit3-v1-Plotting and Visualization.pptx
PPTX
Visualization and Matplotlib using Python.pptx
PPTX
Python Visualization API Primersubplots
PPTX
Matplotlib yayyyyyyyyyyyyyin Python.pptx
PPTX
Introduction to Pylab and Matploitlib.
PPTX
UNIT-5-II IT-DATA VISUALIZATION TECHNIQUES
Unit III for data science engineering.pptx
Introduction to matplotlib
Unit3-v1-Plotting and Visualization.pptx
Visualization and Matplotlib using Python.pptx
Python Visualization API Primersubplots
Matplotlib yayyyyyyyyyyyyyin Python.pptx
Introduction to Pylab and Matploitlib.
UNIT-5-II IT-DATA VISUALIZATION TECHNIQUES

Similar to Matplotlib.pptx for data analysis and visualization (20)

PPTX
2. Python Library Matplotlibmmmmmmmm.pptx
PPTX
MATLAB & Image Processing
PDF
Use the Matplotlib, Luke @ PyCon Taiwan 2012
PDF
The matplotlib Library
PDF
Gráficas en python
PPTX
Data Visualization 2020_21
PDF
slides_python_ for basic operation of it
PDF
Image processing
PPTX
matplotlib _
ODP
Talk on Standard Template Library
PDF
Lecture 02 visualization and programming
DOCX
Data visualization using py plot part i
PPT
Matlab Tutorial.ppt
PDF
Mit6 094 iap10_lec05
PDF
Presentation: Plotting Systems in R
PDF
Matplotlib Review 2021
PDF
Matplotlib_Complete review_2021_abridged_version
PPTX
Subplots_in_Matplotlib_Presentation-1.pptx
PPTX
Introduction_to_Matplotlibpresenatration.pptx
PPTX
Seaborn.pptx
2. Python Library Matplotlibmmmmmmmm.pptx
MATLAB & Image Processing
Use the Matplotlib, Luke @ PyCon Taiwan 2012
The matplotlib Library
Gráficas en python
Data Visualization 2020_21
slides_python_ for basic operation of it
Image processing
matplotlib _
Talk on Standard Template Library
Lecture 02 visualization and programming
Data visualization using py plot part i
Matlab Tutorial.ppt
Mit6 094 iap10_lec05
Presentation: Plotting Systems in R
Matplotlib Review 2021
Matplotlib_Complete review_2021_abridged_version
Subplots_in_Matplotlib_Presentation-1.pptx
Introduction_to_Matplotlibpresenatration.pptx
Seaborn.pptx
Ad

Recently uploaded (20)

PDF
.pdf is not working space design for the following data for the following dat...
PPTX
Database Infoormation System (DBIS).pptx
PDF
Galatica Smart Energy Infrastructure Startup Pitch Deck
PPTX
MODULE 8 - DISASTER risk PREPAREDNESS.pptx
PPTX
Computer network topology notes for revision
PDF
Clinical guidelines as a resource for EBP(1).pdf
PPTX
1_Introduction to advance data techniques.pptx
PPT
ISS -ESG Data flows What is ESG and HowHow
PDF
Lecture1 pattern recognition............
PPTX
DISORDERS OF THE LIVER, GALLBLADDER AND PANCREASE (1).pptx
PDF
TRAFFIC-MANAGEMENT-AND-ACCIDENT-INVESTIGATION-WITH-DRIVING-PDF-FILE.pdf
PDF
Foundation of Data Science unit number two notes
PPT
Reliability_Chapter_ presentation 1221.5784
PPTX
01_intro xxxxxxxxxxfffffffffffaaaaaaaaaaafg
PPTX
Data_Analytics_and_PowerBI_Presentation.pptx
PDF
Recruitment and Placement PPT.pdfbjfibjdfbjfobj
PPTX
Introduction to Basics of Ethical Hacking and Penetration Testing -Unit No. 1...
PDF
“Getting Started with Data Analytics Using R – Concepts, Tools & Case Studies”
PPTX
Introduction to Firewall Analytics - Interfirewall and Transfirewall.pptx
PPTX
mbdjdhjjodule 5-1 rhfhhfjtjjhafbrhfnfbbfnb
.pdf is not working space design for the following data for the following dat...
Database Infoormation System (DBIS).pptx
Galatica Smart Energy Infrastructure Startup Pitch Deck
MODULE 8 - DISASTER risk PREPAREDNESS.pptx
Computer network topology notes for revision
Clinical guidelines as a resource for EBP(1).pdf
1_Introduction to advance data techniques.pptx
ISS -ESG Data flows What is ESG and HowHow
Lecture1 pattern recognition............
DISORDERS OF THE LIVER, GALLBLADDER AND PANCREASE (1).pptx
TRAFFIC-MANAGEMENT-AND-ACCIDENT-INVESTIGATION-WITH-DRIVING-PDF-FILE.pdf
Foundation of Data Science unit number two notes
Reliability_Chapter_ presentation 1221.5784
01_intro xxxxxxxxxxfffffffffffaaaaaaaaaaafg
Data_Analytics_and_PowerBI_Presentation.pptx
Recruitment and Placement PPT.pdfbjfibjdfbjfobj
Introduction to Basics of Ethical Hacking and Penetration Testing -Unit No. 1...
“Getting Started with Data Analytics Using R – Concepts, Tools & Case Studies”
Introduction to Firewall Analytics - Interfirewall and Transfirewall.pptx
mbdjdhjjodule 5-1 rhfhhfjtjjhafbrhfnfbbfnb
Ad

Matplotlib.pptx for data analysis and visualization

  • 2. Matplotlib ● Visualizing data is crucial to quickly understanding trends and relationships in your dataset. ● Matplotlib is one of the most popular libraries for plotting with Python.
  • 3. Matplotlib ● Matplotlib is known as the “Grandfather” of plotting and visualization libraries for Python. ● Many other visualization libraries are built directly off of Matplotlib (e.g. seaborn and pandas built-in visualization).
  • 4. Matplotlib ● Matplotlib is heavily inspired by the plotting functions of the MatLab programming language. ● It allows for the creation of almost any plot type and heavy customization.
  • 5. Matplotlib ● This ability to heavily customize a plot comes at a trade-off for beginners, since it can be confusing to learn the Matplolib syntax at first. ● This is mainly due to the fact that there are actually two separate approaches to creating plots, functional based methods and OOP based methods.
  • 6. Matplotlib ● This Matplotlib section seeks to clear up any confusion by clearly separating out these two approaches. ○ Matplotlib Basics ■ Functional Method ○ Matplotlib Figures and Subplots ■ OOP Method
  • 7. Matplotlib ● Topics Covered ○ Matplotlib Basics and Functions ○ Matplotlib Figures ○ Matplotlib Subplots ○ Matplotlib Styling ○ Exercise Questions and Solutions
  • 8. Matplotlib ● Specialized plot types such as histograms won’t be covered with matplotlib, since we will later learn how to use seaborn to easily create statistical plots. ● It is important to learn matplotlib first however, since seaborn builds directly off of Matplotlib.
  • 9. Matplotlib ● Throughout this section we will be referencing the excellent Matplotlib online documentation: ○ https://matplotlib.org/ ● As well as the gallery of example plots and codes (very useful!) ○ https://matplotlib.org/gallery.html
  • 10. Matplotlib ● Two main goals with Matplotlib: ○ Be able to plot out a functional relationship: ■ y = 2x ○ Be able to plot out a relationship between raw data points: ■ x = [1,2,3,4] ■ y = [2,4,6,8]
  • 13. Matplotlib ● The most basic way to use Matplotlib is through the function plot calls: ○ plt.plot(x,y) ● These function calls are simple to use, but don’t allow for very high degrees of control.
  • 14. Matplotlib ● We recommend using these simple plt.plot() calls for quickly visualizing relationships and data. ● Later on we will explore the more robust OOP Matplotlib Figure API.
  • 15. Matplotlib ● Note! ○ There are slight differences in displaying plots within a notebook versus running a python script. ○ If you are running .py scripts instead of .ipynb notebooks, you will need to add the plt.show() command discussed in this video.
  • 16. Matplotlib Figure Object PART ONE : UNDERSTANDING THE FIGURE
  • 17. Matplotlib ● The more comprehensive Matplotlib OOP API makes use of a Figure object. ● We then add axes to this Figure object and then plot on those axes. ● This allows for very robust controls over the entire plot.
  • 18. Matplotlib ● Let’s quickly visually build an understanding of the Figure object before coding it with Python… ● Note: ○ The Figure object we’re about to show is technically not visible until you add axes to it.
  • 21. Matplotlib ● fig = plt.figure() ● Blank canvas, waiting for a set of axes for plotting. <Figure size 432x288 with 0 Axes>
  • 22. Matplotlib ● fig = plt.figure() axes = fig.add_axes([0, 0, 1, 1])
  • 23. Matplotlib ● fig = plt.figure() axes = fig.add_axes([0, 0, 1, 1]) FIGURE
  • 24. Matplotlib ● fig = plt.figure() axes = fig.add_axes([0, 0, 1, 1]) FIGURE (0,0) (x,y) Lower Left Corner of Axes
  • 25. Matplotlib ● fig = plt.figure() axes = fig.add_axes([0, 1, 1, 1]) FIGURE (0,1) (x,y) Lower Left Corner of Axes
  • 26. Matplotlib ● fig = plt.figure() axes = fig.add_axes([ 0.5, 0.5, 1, 1]) FIGURE (0.5,0.5) (x,y) Lower Left Corner of Axes
  • 27. Matplotlib ● fig = plt.figure() axes = fig.add_axes([0, 0, 1, 1]) FIGURE (width,height) of Axes
  • 28. Matplotlib ● fig = plt.figure() axes = fig.add_axes([0, 0, 1, 1]) (width,height) of Axes
  • 29. Matplotlib ● fig = plt.figure() axes = fig.add_axes([0, 0, 0.5, 0.5]) FIGURE (width,height) of Axes
  • 30. Matplotlib ● fig = plt.figure() axes = fig.add_axes([0, 0, 0.5, 0.5]) (width,height) of Axes
  • 31. Matplotlib ● fig = plt.figure() axes = fig.add_axes([0, 0, 0.5, 1]) FIGURE (width,height) of Axes
  • 32. Matplotlib ● fig = plt.figure() axes = fig.add_axes([0, 0, 0.5, 1]) (width,height) of Axes
  • 33. Matplotlib ● fig = plt.figure() axes = fig.add_axes([0, 0, 1, 1]) axes.plot(x, y)
  • 34. Matplotlib ● fig = plt.figure() axes = fig.add_axes([0, 0, 1, 1]) axes.plot(x, y)
  • 35. Matplotlib ● fig = plt.figure() axes = fig.add_axes([0, 0, 1, 1]) axes.plot(x, y)
  • 36. Matplotlib ● fig = plt.figure() axes = fig.add_axes([0, 0, 1, 1]) axes.plot(x, y)
  • 37. Matplotlib ● This methodology allows us to add in multiple axes as well as move and resize the axes.
  • 38. Matplotlib ● In theory we could set axes side by side using plt.figure() calls, but typically it is easier to use plt.subplots() function calls for this. ● We’ll explore multiple side by side plots in a future lecture, for now let’s explore the Figure object methodology for Matplotlib!
  • 39. Matplotlib Figure Object PART TWO: IMPLEMENTING FIGURES AND AXES
  • 42. Matplotlib ● In theory we could create a Figure object and then manually add and arrange sets of axes to line up multiple plots side by side. ● However, Matplotlib comes with a pre- configured function call plt.subplots() that automatically does this for us!
  • 43. Matplotlib ● The plt.subplots() call allows us to easily create Figure and Axes objects in side by side formations. ● The plt.subplots() command returns a tuple containing the Figure canvas and then a numpy array holding the axes objects.
  • 44. Matplotlib ● fig, axes = plt.subplots(nrows=1, ncols=2)
  • 45. Matplotlib ● fig, axes = plt.subplots(nrows=2, ncols=2)
  • 46. Matplotlib ● plt.subplots() returns a tuple which by common convention we label (fig,axes): ○ fig ■ This is the entire Figure canvas. ○ axes ■ This is a numpy array holding each of the axes according to position in the overall canvas.
  • 47. Matplotlib ● Let’s explore how to use plt.subplots() to easily create and align multiple plots!
  • 49. Matplotlib ● Matplotlib offers very robust styling functions that allow us to edit colors, legends, line widths, markers, and much more! ● Note: Due to the wide amount of possible variations, we will be copying and pasting from the lecture notebook to save typing time in the video.
  • 50. Matplotlib ● Main Styling Discussed: ○ Legends ○ Visual Styling ■ Colors ■ Editing Lines ● Colors, Widths, Styles ■ Editing Markers ● Colors, Size, Styles, Edges
  • 51. Matplotlib ● Let’s begin with adding legends!
  • 54. Matplotlib ● Matplotlib is a huge library! ● We’ve added a notebook with some additional concepts you may want to explore on your own. ● We don’t use these concepts in the course however, so feel free to skip this notebook for now.
  • 55. Matplotlib ● An important note is that almost any Matplotlib question you can think of already has an answer in StackOverflow or an example in the Matplotlib gallery. ● Leverage these many examples to your advantage and do not waste energy and time into memorizing esoteric commands!