DATA
VISUALIZATION
WITH MATPLOTIB
BY
POOJA S
I B.C.A
Agenda
Introduction
Matplotlib Library
Matplotlib Architecture
Pyplot
Data Visualization with Jupyter Notebook
DATA VISUALIZATION WITH MATPLOTIB 2
DATA VISUALIZATION
Presentation title 3
The previous chapters covered the Python libraries
that are responsible for data processing, and this
chapter covers the libraries that take care of
visualization. You’ll first get a broad overview of
the matplotlib library, and then the chapter
concludes with the seaborn library, which extends
matplotlib with the representation of statistical
graphic elements.
Data visualization is often underestimated
in data analysis, but it is a very important
factor because incorrect or inefficient data
representation can ruin an otherwise
excellent analysis
DATA VISUALIZATION WITH MATPLOTIB
• Extreme simplicity in its use
Matplotlib • Gradual development and interactive data
visualization
Library • Expressions and text in LaTeX
• Greater control over graphic elements
• Ability to export it to many formats, such as
PNG, PDF, SVG, and EP
5
DATA VISUALIZATION WITH MATPLOTIB
Installation
There are many options for installing the matplotlib library.
If you choose to use the Anaconda distribution, installing
the matplotlib package is very simple. You can do this
graphically, using Anaconda Navigator. Activate the virtual
environment you need to work on and then look for
matplotlib among the distribution’s packages to install, Then
select the two packages— matplotlib and matplotlib-base—
in the list of available ones. Finally, click the Apply button
at the bottom right to start the installation
6
DATA VISUALIZATION WITH MATPLOTIB
If instead you prefer to use the command
console, on Anaconda you can open a session via
the CMD. exe Prompt and then enter the
following command:
conda install matplotlib
CODE
If instead you have decided not to use the Anaconda
platform on your system, you can install matplotlib COMMAND
directly from the command console of your system
using the pip command
pip install matplotlib
7
DATA VISUALIZATION WITH MATPLOTIB
The matplotlib Architecture
The architecture of matplotlib is
logically structured into three layers,
which are placed at three different
levels .The communication is
unidirectional, that is, each layer can
communicate with the underlying
layer, while the lower layers cannot
communicate with the top ones
8
DATA VISUALIZATION WITH MATPLOTIB
In the diagram of the matplotlib architecture , the
layer that works at the lowest level is the
Backend layer. This layer contains the matplotlib
Backend APIs, a set of classes that implement the graphic
elements at a low level.
Layer • FigureCanvas is the object that embodies the
concept of a drawing area.
• Renderer is the object that draws on
FigureCanvas.
• Event is the object that handles user inputs
(keyboard and mouse events)
9
DATA VISUALIZATION WITH MATPLOTIB
Artist Layer
There are two Artist classes: primitive and
composite.
• The primitive artists are individual objects that
constitute the basic elements that form a
graphical representation in a plot, for example a
Line2D, or as a geometric figure such as a
Rectangle or Circle, or even pieces of text.
• The composite artists are graphic elements that
are composed of several base elements, namely,
the primitive artists. Composite artists are for
example the Axis, Ticks, Axes, and Figures
10
DATA VISUALIZATION WITH MATPLOTIB
Artist classes and their related functions (the
matplotlib API) are particularly suitable to all
developers, especially for those who work on
web application servers or develop the GUI. But
for purposes of calculation, and in particular for
the analysis and visualization of data, the
scripting layer is best. This layer consists of an Scripting
interface called pyplot
pylab and pyplot
Layer (pyplot)
In general there is talk of pylab and pyplot. But what
is the difference between these two packages? pylab
is a module that is installed along with matplotlib,
while pyplot is an internal module of matplotlib.
Often you will find references to one or the other
approach
11
DATA VISUALIZATION WITH MATPLOTIB
from pylab import *
import matplotlib.pyplot as plt
import numpy as np
pylab combines the functionality of pyplot with the capabilities of NumPy in a
single namespace, and therefore you do not need to import NumPy separately.
Furthermore, if you import pylab, pyplot and NumPy functions can be called directly
without any reference to a module (namespace), making the environment more similar to
MATLAB.
Pylab plot(x,y)
array([1,2,3,4])
and Pyplot Instead of
plt.plot()
np.array([1,2,3,4]
The pyplot package provides the classic Python interface for programming the
matplotlib library, has its own namespace, and requires the import of the NumPy
package separately. This approach is the one chosen for this book; it is the main topic of
this chapter; and it is used for the rest of the book. This approach is shared and approved
by most Python developers.
12
DATA VISUALIZATION WITH MATPLOTIB
Pyplot
The pyplot module is a collection of command-style functions
that allow you to use matplotlib much like MATLAB. Each
pyplot function will operate or make some changes to the Figure
object, for example, the creation of the Figure itself, the creation
of a plotting area, representation of a line, decoration of the plot
with a label, and so on. pyplot also is stateful, in that it tracks
the status of the current figure and its plotting area. The called
functions act on the current figure
13
The Plotting Window
To get familiar with the
matplotlib library and in a
particular way with
pyplot, you will start
creating a simple
interactive chart. Using
matplotlib, this operation
is very simple; in fact, you
can achieve it using only
three lines of code
DATA VISUALIZATION WITH MATPLOTIB 14
DATA VISUALIZATION WITH MATPLOTIB
Toolbar on Plotting Windows
The plotting window is
characterized by a toolbar at the
top in which there is a series of
buttons.
15
DATA VISUALIZATION WITH MATPLOTIB
Data Visualization with Jupyter Notebook
A more convenient way to work interactively
and professionally with matplot-generated
charts is to use Jupyter Notebook. In fact, by
opening a Notebook on Jupyter and entering
the previous code, you will find that the chart
will be displayed directly integrated into the
Notebook, without directly invoking the
show() command
16
DATA VISUALIZATION WITH MATPLOTIB
Data Visualization with Jupyter Notebook
(WITH X AND Y AXIS)
plt.plot([1,2,3,4],[1,4,9,16])
17
Set the Properties of the Plot
• The size of the axes matches perfectly with the range of the input data
• There is neither a title nor axis labels
• There is no legend
• A Blue line connecting the points is drawn
Presentation title 18
Matplotlib and NumPy
import math
import numpy as np
t = np.arange(0,2.5,0.1)
y1 = np.sin(math.pi*t)
y2 = np.sin(math.pi*t+math.pi/2)
y3 = np.sin(math.pi*t-math.pi/2)
plt.plot(t,y1,'b*',t,y2,'g^',t,y3,'ys')
Presentation title 19
Thank you