DSV Mod 5 PPT
DSV Mod 5 PPT
Prepared
By
Prof. Ajay N
Assistant Professor,
Dept. of CSE,
SJCIT, Chickballapur.
Introduction
In the previous chapter, we focused on various visualizations and identified which
visualization is best suited to show certain information for a given dataset.
We learned about the features, uses, and best practices for following various plots such as
comparison plots, relation plots, composition plots, distribution plots, and geoplots.
Matplotlib is probably the most popular plotting library for Python. It is used for data
science and machine learning visualizations all around the world. John Hunter was an
American neurobiologist who began developing Matplotlib in 2003.
Overview of Plots in Matplotlib
Plots in Matplotlib have a hierarchical structure that nests Python objects to create a tree-
like structure. Each plot is encapsulated in a Figure object. This Figure is the top-level
container of the visualization. It can have multiple axes, which are basically individual
plots inside this top-level container.
The two main components of a plot are as follows:
• Figure
The Figure is an outermost container that allows you to draw
multiple plots within it. It not only holds the Axes object but
also has the ability to configure the Title.
• Axes
The axes is an actual plot, or subplot, depending on whether you
Figure 5.1: A Figure contains at least one axes object want to plot single or multiple visualizations. Its sub-objects
include the x-axis, y-axis, spines, and legends.
Anatomy of a Matplotlib Figure
• Spines: Lines connecting the axis tick marks
• Title: Text label of the whole Figure object
• Legend: Describes the content of the plot
• Grid: Vertical and horizontal lines used as an extension
of the tick marks
• X/Y axis label: Text labels for the X and Y axes below
the spines
• Minor tick: Small value indicators between the major
tick marks
• Minor tick label: Text label that will be displayed at
the minor ticks
• Major tick: Major value indicators on the spines
• Major tick label: Text label that will be displayed at
the major ticks
• Line: Plotting type that connects data points with a line
• Markers: Plotting type that plots every data point with
Figure 5.2: Anatomy of a Matplotlib Figure a defined marker
Pyplot Basics
pyplot contains a simpler interface for creating visualizations that allow the users to plot the data without explicitly
configuring the Figure and Axes themselves. They are automatically configured to achieve the desired output. It is handy to
use the alias plt to reference the imported submodule, as follows:
import matplotlib.pyplot as plt
Creating Figures
By default, the Figure has a width of 6.4 inches and a height of 4.8 inches with a dpi (dots per inch) of 100. To change
the default values of the Figure, we can use the parameters figsize and dpi.
To close a specific Figure, you can either provide a reference to a Figure instance or provide the Figure number. To
find the number of a Figure object, we can make use of the number attribute, as follows:
plt.gcf().number
The plt.close('all') command is used to close all active Figures. The following example shows how a Figure can be
created and closed:
plt.show() is used to display a Figure The plt.savefig(fname) saves the current Figure. There are
or multiple Figures. To display Figures some useful optional parameters you can specify, such as dpi,
within a Jupyter Notebook, simply set format, or transparent.
the %matplotlib inline command at
the beginning of the code.
If you forget to use plt.show(), the plot
won't show up
Labels
Matplotlib provides a few label functions that we can use for setting labels to the x- and y-axes. The plt.xlabel() and
plt.ylabel() functions are used to set the label for the current axes. The set_xlabel() and set_ylabel() functions are used
to set the label for specified axes.
Titles
A title describes a particular chart/graph. The titles are placed above the axes in the center, left edge, or right edge.
There are two options for titles – you can either set the Figure title or the title of an Axes. The suptitle() function sets
the title for the current and specified Figure. The title() function helps in setting the title for the current and specified
axes.
plt.title('Title', fontsize=16)
Annotations
Annotations are used to annotate some features of the plot. In annotations, there are two locations to consider: the
annotated location, xy, and the location of the annotation, text xytext. It is useful to specify the parameter arrowprops,
which results in an arrow pointing to the annotated location.
Example:
ax.annotate('Example of Annotate', xy=(4,2), xytext=(8,4),arrowprops=dict(facecolor='green', shrink=0.05))
Legends
Legend describes the content of the plot. To add a legend to your Axes, we have to specify the label parameter at the
time of plot creation. Calling plt.legend() for the current Axes or Axes.legend() for a specific Axes will add the legend.
The loc parameter specifies the location of the legend.
Example:
plt.plot([1, 2, 3], label='Label 1')
plt.plot([2, 4, 3], label='Label 2')
plt.legend()
Important parameters:
• x: Specifies the x coordinates of the bars
• height: Specifies the height of the bars
• width (optional): Specifies the width of all bars; the default is 0.8
Example:
plt.bar(['A', 'B', 'C', 'D'], [20, 25, 40, 10])