Plotting and Visualization using
Matplotlib - 1
Matplotlib
• Matplotlib is a powerful and versatile open-source plotting library
for Python, designed to help users visualize data in a variety of
formats. It is used for creating static, animated, and interactive
visualizations and provides full control over plots, making it highly
customizable.
• To install the library – ‘pip install matplotlib’
• To import the matplotlib after installation:
import [Link] as plt
Parts of a Matplotlib Figure
•Figure: The overarching container that holds all plot
elements, acting as the canvas for visualizations.
•Axes: The areas within the figure where data is plotted;
each figure can contain multiple axes.
•Axis: Represents the x-axis and y-axis, defining limits,
tick locations, and labels for data interpretation.
•Lines and Markers: Lines connect data points to show
trends, while markers denote individual data points in
plots like scatter plots.
•Title and Labels: The title provides context for the plot,
while axis labels describe what data is being represented
on each axis.
Figure
• In Matplotlib, a Figure is the top-level container for all plot elements. It
acts as a canvas where one or more subplots(axes) are placed.
• A figure can be empty or can have multiple subplots.
• If we create a Matplotlib Figure with 0 Axes, it means the figure exists,
but there are no plots (subplots) inside it
Matplotlib measures figure size in inches, and the resolution
is defined by dots per inch (DPI).
Subplots
• A subplot is a smaller plot (graph) that
exists inside the canvas, i.e known as
Figure. It allows us to display multiple
plots in the same figure, arranged in
rows and columns. It is also known as
axes.
• In simple words, subplot refers to the
individual plots inside a figure.
• Multiple plots can be created inside a
figure using subplot() function.
Subplots
Advantages of using
Subplots:
1. Compare multiple
datasets in one figure
2. Save space instead of
creating multiple separate
figures
3. Easier analysis of related
graphs as subplot allows
the graphs to be placed
side by side.
Colors
• Matplotlib allows us to specify colors in various ways when plotting
graphs. For example: In a line graph we can specify or change the
color of the ‘line’ using the command color.
• Matplotlib supports multiple ways to specify colors:
Named Colors: 'red', 'blue', 'green’, ‘orange’ , ‘magenta’ etc.
Hex Codes: #FF5733 (RGB in hex format).
RGB Tuples(R, G, B) : The values of (R,G,B) lies between 0 to 1.
Grayscale: A value between 0 (black) and 1 (white) will be considered.
Short color codes: Matplotlib also provides short color codes. For eg: ‘w’ for
white, ‘b’ for blue, ‘k’ for black.
Colors
Markers
• Markers in Matplotlib are used to display individual data points on a plot.
We can customise the shape, size, and color of markers to make the plots
more informative and readable. We can also change the marker size, face
color, edge color, and width using various commands
Marker Description Symbol
‘.’ Point
‘o’ Circle
‘s’ Square
‘p’ Pentagon
‘*’ Star
‘+’ Plus
^,v,<,> Triangle Up/Down/Left/Right
Line Styles
• In matplotlib, the
default linestyle while plotting
data is solid line. We can
change this linestyle by
using linestyle or ls argument
of the plot() method. The
various types of lifestyles are
solid line, dashed line, dotted
line, dash dot line.
Color, Markers and Linestyles
Ticks and Tick Labels
• Ticks are the marks on the axes(x-axis and y-axis) of a plot that denote specific data points.
By default, Matplotlib automatically generates these ticks based on the data being plotted.
However, in many cases, users may want to customize these ticks to better reflect the data
or improve clarity.
• Ticks are mainly considered of two types:
Major Ticks: Primary reference points on an axis.
Minor Ticks: Finer ticks for additional detail.
• We can manually define the ticks and tick labels using the following:
set_xticks() Once the tick positions are set, we can use set_xticklabels() and set_yticklabels()
set_yticks() to assign custom labels to the ticks.
• In the context of graphs and plotting, ticks are the small lines that show the scale of x and y-axes,
providing a clear representation of the value associated with each tick. Whereas tick labels are the
textual or numeric annotations associated with each tick on an axis, providing a clear
representation of the value associated with each tick.
Legends
• In Matplotlib, a legend is used to
describe the elements of a plot, making
it easier to understand different data
series. In the Matplotlib library, there’s a
function called legend() which is used to
place a legend on the axes. It gives a way
to label and differentiate between
multiple plots in the same figure.
• By default, the legend appears in the
best location of the graph, but we can
specify the location by using ‘loc’.