0% found this document useful (0 votes)
2 views25 pages

11 Matplotlib

Python Library

Uploaded by

sshamshawa
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views25 pages

11 Matplotlib

Python Library

Uploaded by

sshamshawa
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

ADVANCED PROGRAMMING TECHNIQUES

LIBRARIES – MATPLOTLIB
[Link] TIZIANA D’ALESSANDRO (PHD), UNIVERSITÀ DEGLI STUDI DI CASSINO E DEL LAZIO MERIDIONALE
[Link]@[Link]
CONTENTS

 Introduction to Matplotlib
 Best Practices
 examples
 Conclusions

2
MATPLOTLIB

3
MATPLOTLIB

Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in Python. s
a low level graph plotting library in python that serves as a visualization utility.
It was created by John D. Hunter, and it is mostly written in python, a few segments are written in C,
Objective-C and Javascript for Platform compatibility.
Installation: C:\Users\Your Name>pip install matplotlib
Import: import [Link] as plt

It provides high quality Why useful?


publication output. - Modular Artist architecture
Users can personalise - Deep integration with Numpy and Pandas
the visual contents - Open source and highly extensible
THE POWER OF VISUAL INSIGHTS

Pattern Recognition Data Cleaning Communication

Human brains process visual data Visualization quickly highlights Presenting findings to
60,000 times faster than text, outliers, missing values, and stakeholders requires
enabling instant trend detection. anomalies in raw datasets. professional-grade charts for
maximum impact.
NP ARRAYS

 If you provide a single list, Matplotlib assumes they are Y-values and generates X-values (0, 1, 2...)
automatically.
NP ARRAYS

 The plot() function is used to draw points (markers) in a diagram.


 By default, the plot() function draws a line from point to point.
 The function takes parameters for specifying points in the diagram:
• Parameter 1 is an array containing the points on the x-axis.
• Parameter 2 is an array containing the points on the y-axis.
NP ARRAYS – LINE PLOT

 If we need to plot a line from (1, 3) to (8, 10), we have to pass two arrays [1, 8] and [3, 10] to the
plot function.
NP ARRAYS – PLOTTING WITHOUT LINE

 To plot only the markers, you can use shortcut string notation parameter 'o', which means 'rings’
 Draw two points in the diagram, one at position (1, 3) and one in position (8, 10):
NP ARRAYS – MULTIPLE POINTS

 You can plot as many points as you like, just make sure you have the same number of points in both axis.
 Draw a line in a diagram from position (1, 3) to (2, 8) then to (6, 1) and finally to position (8, 10)

Note: If we do not specify the points on the x-axis, they will get
the default values 0, 1, 2, 3 etc., depending on the length of the
y-points.
MARKERS

 You can use the keyword argument marker to emphasize each point with a specified marker:
MARKERS
Marker Symbol Description Marker Symbol Description

'p' Pentagon
'o' Circle
'H' Hexagon (horizontal)
'*' Star
'h' Hexagon (vertical)
'.' Point
'v' Triangle Down
',' Pixel
'^' Triangle Up

'x' X '<' Triangle Left

'X' X (filled) '>' Triangle Right

'1' Tri Down


'+' Plus
'2' Tri Up
'P' Plus (filled)
'3' Tri Left
's' Square
'4' Tri Right
'D' Diamond
'|' Vline (Vertical Line)

'd' Diamond (thin) '_' Hline (Horizontal Line)


LINES

Line Description
Syntax
'-' Solid line
':' Dotted line

'--' Dashed line


'-.' Dashed/dotted line
COLORS

Color Syntax Description


'r' Red
'g' Green

'b' Blue
'c' Cyan
'm' Magenta
'y' Yellow
'k' Black
'w' White
MARKERS – FACES, EDGES AND SIZES

 You can set the facecolor (mfc), edgecolor (mec) and the size of markers
FORMAT STRINGS FMT

 You can also use the shortcut string notation parameter to specify the marker. This parameter is
also called fmt, and is written with this syntax: marker line color
SINGLE LIST PLOT

 If you provide a single list, Matplotlib assumes they are Y-values and generates X-values (0, 1, 2...)
automatically.
(X,Y) CORDINATES - 2 LISTS PLOT

 Providing two lists maps specific coordinates. The lists must have identical lengths to avoid value
errors.
DICTIONARIES – MAPPING LABELS TO VALUES

 Dictionaries are ideal for Categorical Data. You can easily map keys to labels and values to heights.
PANDAS – THE DATAFRAME ADVANTAGE

 Pandas DataFrames provide a structured way to handle complex datasets with labeled rows and
columns.

Direct Access: Plot columns using df['col'].plot().

Time Series: Automatic handling of date indexes.

Filtering: Plotting subsets of data effortlessly.


PANDAS – CORE PYPLOT REFERENCE

Function Purpose Typical Usage

[Link]() Line charts Trends over time or sequences

[Link]() Point plots Correlation between two variables

[Link]() Vertical bars Categorical comparisons

[Link]() Distribution Frequency analysis of numeric ranges


PANDAS – ESSENTIAL CHART TYPES

Line Plot Scatter Plot Histogram

Default plot type. Connects points Individual markers. Essential for Binned data view. Shows the
with lines. Best for continuous identifying clusters and density and skewness of
trends. correlation. distribution.
ANATOMY OF A PROFESSIONAL PLOT

Labels: [Link]() and [Link]() define the context.

Title: [Link]() provides a clear narrative for the chart.

Legend: [Link]() identifies multiple series.

Grid: [Link](True) aids in reading precise values.

Styles: Use [Link]('ggplot') for instant aesthetics.


CONCLUSIONS – Q&A

Questions?
THANK YOU
[Link]@[Link]

You might also like