SlideShare a Scribd company logo
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Ø MATPLOTLIB ARCHITECTURE
Ø ANATOMY OF A MATPLOTLIB FIGURE
Ø PYPLOT INTERFACE
Ø OBJECT ORIENTED INTERFACE
PREPARED BY :
BHASKAR JYOTI ROY
bhaskarjroy1605@gmail.com
REFERENCES :
• Matplotlib Release 3.4.2, May 08, 2021
• Mastering Matplotlib by Duncan M. McGreggor
• StackOverflow and others
1
A COMPLETE REVIEW (Abridged Version)
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
• Matplotlib Introduction
• Topics we will cover
• Matplotlib Architecture – Three layers
• Relationship of layers to Data and Figure
• Deep Dive into Backend Layer
• Deep Dive into Artist Layer
• Anatomy of a Figure
• Hierarchy of Artists
• Scripting Layer
• Pyplot Interface
• Frequently Used Pyplot Functions
• For the Figure, Axes, Axis
• For Charting
• For Multiple plots, Layout Customisation
• Code Snippet for use of subplot function
• For fine tuning plot appearance
• rc Params , rcParams for global changes
• rcParams dictionary variables to revert back settings
• StyleSheets
• StyleSheets – Showcasing All styles
2
INDEX – MATPLOTLIB API
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
INDEX – OO INTERFACE
• OO interface
• Create a Figure instance
• Use Figures to create one or more axes or subplot
• Demo of fig.subplots() and enumerate, flatten
• Demo of fig.add_subplots() without and with GridSpec
• Demo of fig.add_axes()
• Use Axes helper methods to add artists to respective containers within the Axes object
• Axes Helper Methods to create Primitives
• Axes Methods to create Different Plots and text
• Example Illustrations of various plots – Piecharts, Histograms, bar charts, Grouped bars charts
• Example Illustrations of various plots – Line Chart, Bidirectional Chart, Marginal Histograms, Diverging Lollypop
chart
• Use Axes/Axis Helper methods to access and set x-axis and y-axis tick, tick labels and axis labels
• Axes Helper Methods for Appearance, Axis Limits
• Axes Helper Methods for Ticks and Tick Labels, GridLines
• Axes Helper Methods for Axis Labels, Title and legend
• Axis Helper Methods for Formatters and Locators, Axis Label
• Axis Helper Methods for Ticks, Tick Labels
• Axis Helper Methods – X Axis, Y Axis Specific
• Tick Params Method – Bulk property Setter, A Note on Practical Implementation
• Special Note on Tickers – Locator Class, Demo 1, Demo 2
3
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
• Special Note on Tickers – Formatter Class
• Tickers – Using Simple Formatting, Formatter Class
• Tickers for Time Series Plotting, Date Locator and Date Formatter
• Concise date Formatter Example
• Use Axes Methods (Accessors) to access artists through attributes
• Accessing Artists by Assigning to a Variable when creating
• Accessing Artists through Attributes - Approach
• Useful Methods and Python Built in Methods
• Accessing Artists through Attributes (Artist Hierarchy)
• Set/modify the Artist properties after accessing the artists
• Mapping the Journey – From Axes Instance to Artist Instance Properties
• Properties Common to All Artists
• Setting Artist Properties, Inspecting Artist Properties
• Additional Properties specific to Line 2D , text
• Special look into Patch Rectangle
• Additional Properties Rectangle Specific
• Usecases – Modifying the Rectangle patch properties
• Methods of Rectangle Patch
• Creating User Defined function to Stylize Axes
• Creating User Defined function to Add data labels in bar plots
• Demo – Using UDF to stylize Axes and add data labels
INDEX – OO INTERFACE
4
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
• Transformations
• Transformation Framework
• Transformation Framework – The Coordinate systems
• Transformation Framework – Transformation Objects
• Blended Transformations Example
• Colors
• Specifying Colors – Acceptable Formats
• Exploring the Color Charts – Use Case Scenario
• Base Colors : ‘b’, ‘g’, ’r’, ‘c’, ‘m’, ‘y’, ‘k’, ‘w’
• Tableau Palette
• CSS colors
• XKCD Colors – xkcd:black – xkcd:dirty orange
• XKCD Colors – xkcd:medium brown – xkcd:sunny yellow
• XKCD Colors – xkcd:parchment – xkcd:leaf
• XKCD Colors – xkcd:kermit green– xkcd:light seafoam
• XKCD Colors – xkcd:dark mint– xkcd:ocean blue
• XKCD Colors – xkcd:dirty blue– xkcd:ultra marine
• XKCD Colors – xkcd:purpleish blue – xkcd:light plum
• XKCD Colors – xkcd:pale magenta – xkcd:salmon pink
INDEX – OO INTERFACE
5
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
• ColorMaps - Overview
• Colormaps Classification
• Range of Matplotlib Colormaps
• Sequential colormaps
• Diverging colormaps
• Cyclic colormaps
• Qualitative colormaps
• Miscellaneous colormaps
• Lightness of Matplotlib colormaps
• Accessing Colors from a Matplotlib Colormap
• Creating Listed Colormap from Existing Matplotlib Colormaps
• Code snippet to create listed colormaps and accessing colors
• Add legend and set title to the axes
• Aspects of Legend
• Adding Legend to Axes Call Signatures, Important terminologies
• Automatic detection of elements to be shown in the legend
• Explicitly defining the elements in the legend
• With labels explicitly defined in call signature
• With labels omitted in call signature
• Creating Proxy Artists and Adding in the legend (Code demo)
• Controlling Legend Location, Placement – Concept, Demo
INDEX – OO INTERFACE
6
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
• Epilogue
• Data Structures Overview
• Pseudo Random Number Generation (in Python, Numpy)
• Handling Date and Time
• Statistics with Python
• Appendix
• References
INDEX – END
7
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
VISUAL INDEX – 01
8
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
VISUAL INDEX – 02
9
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
MATPLOTLIB INTRODUCTION A comprehensive library for creating static, animated, and
interactive visualizations in Python.
Widely used plotting library for the Python programming language
A core component of the scientific Python stack – a group of
scientific computing tools, alongwith Numpy, Scipy and IPython
Original Design Philosophy
Matplotlib is designed with the philosophy that you should be able
to create simple plots with just a few commands, or just one! If you
want to see a histogram of your data, you shouldn't need to
instantiate objects, call methods, set properties, and so on; it should
just work.
Initial Release in 2003
Created by neurobiologist John D. Hunter to plot data of electrical
activity in the brains of epilepsy patients, but today is used in number of
fields
libraries like pandas and Seaborn are “wrappers” over matplotlib -
providing high level API’s that allow access to a number of
matplotlib's methods with less code. For instance, pandas’
.plot()combines multiple matplotlib methods into a single method,
thus plotting in a few lines.
Two ways to use Matplotlib :
• Pyplot Interface inspired from MATLAB is fast and convenient
• Pythonic OO way is flexible and being explicit gives finely grained
control
• ipython : : : For interactive work.
• numpy : : : For working with vectors and arrays.
• scipy : : : All the essential scientific algorithms, including those for basic statistics.
• matplotlib : : : The de-facto standard module for plotting and visualization.
• pandas : : : Adds DataFrames (imagine powerful spreadsheets) to Python.
• statsmodels : : : For statistical modeling and advanced analysis.
• seaborn : : : For visualization of statistical data.
• Scikit extensions to scipy for specific fields like machine learning, , deep learning, x-ray,
image processing and many more
• Additional Libraries are Plotly, Bokeh, TensorFlow, Keras, XGBoost
STRUCTURE OF THE IMPORTANT PYTHON PACKAGES
FOR DATA SCIENCE Code
10
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
TOPICS WE WILL COVER
Three Layers of matplotlib
architecture
Anatomy of a figure
Artist Hierarchy
Pyplot API
Object oriented API - for control
and Customisation
•Backend layer
•Artist layer
•Scripting layer
• Overview of Data Structures
• Pseudo Random Number Generation
• Handling Date and Time
• Statistics with Python
• Appendix – Matplotlib plot examples
• References
11
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
*https://aosabook.org/en/matplotlib.html
*Matplotlib Release 3.4.2, May 08, 2021, Page No. 460
*Mastering Matplotlib by Duncan M. McGreggor, Pg no.15
Matplotlib
Hierarchy
MATPLOTLIB ARCHITECTURE
ü Enable the users to create, render, and update the figure
objects.
ü The Three layers can be visualised as stack from bottom to
top
ü The Three layers perform distinct role at different levels of
awareness.
Backend Backend
Artist
Backend
Artist
Scripting
ü Backend layer is not
aware of the Artist
and the Scripting
layer. It knows its own
interfaces only.
ü Artist Layer knows
how to talk with the
Backend Layer.
ü Scripting Layer knows
how to talk to both
the Artist Layer and
the Backend Layer.
Logical Separation into Three Layers
ISOLATION OF THE COMPLEXITIES IN EACH LAYER
12
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
*https://aosabook.org/en/matplotlib.html
*Matplotlib Release 3.4.2, May 08, 2021, Page No. 460
*Mastering Matplotlib by Duncan M. McGreggor, Pg no.15
These objects are then rendered by the backend,
which ultimately provides the configured toolkit
with the raw data necessary to place an image on
the toolkit's canvas
The data is transformed into various objects in
the artist layer; it is adjusted as scripted
This data is either created or loaded in the
scripting layer
The user creates either the data that he/she wants
to plot or the functions that generate this data
Matplotlib
Hierarchy
RELATIONSHIP OF THE LAYERS TO
DATA AND THE FIGURE OBJECT FOR
A GIVEN PLOT
13
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
*https://aosabook.org/en/matplotlib.html
*Matplotlib Release 3.4.2, May 08, 2021, Page No. 460
*Mastering Matplotlib by Duncan M. McGreggor, Pg no.15
DEEP DIVE INTO BACKEND LAYER
Backend Layer refers to matplotlib capabilities to render plots for
different use cases and output formats.
Use Matplotlib interactively
from the python shell and have
plotting windows pop up when
they type commands.
Some people run Jupyter
notebooks and draw inline
plots for quick data analysis.
Others embed Matplotlib into
graphical user interfaces like
PyQt or PyGObject to build
rich applications.
Some people use Matplotlib in
batch scripts to generate
postscript images from
numerical simulations
Others run web application
servers to dynamically serve
up graphs.
Matplotlib
Hierarchy
DIFFERENT USE CASES
14
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
*https://aosabook.org/en/matplotlib.html
*Matplotlib Release 3.4.2, May 08, 2021, Page No. 460
*Mastering Matplotlib by Duncan M. McGreggor, Pg no.15
DEEP DIVE INTO BACKEND LAYER
Backend Layer refers to matplotlib capabilities to render plots for
different use cases and output formats.
Matplotlib
Hierarchy
DIFFERENT OUTPUT FORMATS
VECTOR OR RASTER BASED
Vector Graphics use clean
lines and shapes that can be
scaled to any size
Raster Graphics use pixels
arranged in grid formation to
represent an image
15
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
*https://aosabook.org/en/matplotlib.html
*Matplotlib Release 3.4.2, May 08, 2021, Page No. 460
*Mastering Matplotlib by Duncan M. McGreggor, Pg no.15
DEEP DIVE INTO BACKEND LAYER
Backend Layer refers to matplotlib capabilities to render plots for
different use cases and output formats.
Renderer Output File Types Description
ADD PNG raster graphics -- high quality images using
the Anti-Grain Geometry engine
PDF PDF vector graphics -- Portable Document Format
PS PS, EPS vector graphics -- Postscript output
SVG SVG vector graphics -- Scalable Vector Graphics
PGF PGF, PDF vector graphics -- using the pgf package
CAIRO PNG, PS, PDF, SVG raster or vector graphics -- using the Cairo
library
Matplotlib
Hierarchy
DIFFERENT OUTPUT FORMATS
VECTOR OR RASTER BASED
16
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
*https://aosabook.org/en/matplotlib.html
*Matplotlib Release 3.4.2, May 08, 2021, Page No. 460
*Mastering Matplotlib by Duncan M. McGreggor, Pg no.15
Hardcopy backends to
make static image files (PNG,
SVG, PDF, PS)
DEEP DIVE INTO BACKEND LAYER
User Interface Backends
For use in
• GTK 2.x and GTK 3.x
• wxWidgets
• Tk
• Qt4 and Qt5
• Mac OS X Cocoa
Backend Layer refers to matplotlib capabilities to render plots for
different use cases and output formats.
Matplotlib
Hierarchy
17
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
*https://aosabook.org/en/matplotlib.html
*Matplotlib Release 3.4.2, May 08, 2021, Page No. 460
*Mastering Matplotlib by Duncan M. McGreggor, Pg no.15
DEEP DIVE INTO ARTIST LAYER
Matplotlib
Hierarchy The Artist layer constitutes the bulk of what matplotlib actually
does — the generation of the plots.
Most work in the artist layer is performed by a number of classes,
most of which are derived from the Artist base class.
Let’s run through the sub classes in the Artist Layer…
18
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
*https://aosabook.org/en/matplotlib.html
*Matplotlib Release 3.4.2, May 08, 2021, Page No. 460
*Mastering Matplotlib by Duncan M. McGreggor, Pg no.15
*Matplotlib Release 3.4.2,
Chapter 18.36 matplotlib.patches, Pg No. 2326
Primitives
Circle
Rectangle Ellipse
Polygon
Fancybox Patch
Path Patch
Wedge
Arrow
text
Line 2D
DEEP DIVE INTO ARTIST LAYER
The matplotlib artist
primitives are classes of
standard graphical
objects that are
supposed to be painted
on a figure's canvas.
Matplotlib
Hierarchy
19
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
*https://aosabook.org/en/matplotlib.html
*Matplotlib Release 3.4.2, May 08, 2021, Page No. 460
*Mastering Matplotlib by Duncan M. McGreggor, Pg no.15
*Matplotlib Release 3.4.2,
Chapter 18.36 matplotlib.patches, Pg No. 2326
Primitives – Patches Classes
Circle
Rectangle Ellipse
Polygon
Fancybox Patch
Path Patch
Wedge
Arrow
DEEP DIVE INTO ARTIST LAYER
The matplotlib artist
primitives are classes of
standard graphical
objects that are
supposed to be painted
on a figure's canvas.
Matplotlib
Hierarchy
20
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
*https://aosabook.org/en/matplotlib.html
*Matplotlib Release 3.4.2, May 08, 2021, Page No. 460
*Mastering Matplotlib by Duncan M. McGreggor, Pg no.15
DEEP DIVE INTO ARTIST LAYER
The matplotlib artist
primitives are classes of
standard graphical
objects that are
supposed to be painted
on a figure's canvas.
Containers offer a useful
abstraction to gather
primitives.
Matplotlib
Hierarchy
Figure - Top level Artist, which
holds all plot elements
21
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
*https://aosabook.org/en/matplotlib.html
*Matplotlib Release 3.4.2, May 08, 2021, Page No. 460
*Mastering Matplotlib by Duncan M. McGreggor, Pg no.15
DEEP DIVE INTO ARTIST LAYER
The matplotlib artist
primitives are classes of
standard graphical
objects that are
supposed to be painted
on a figure's canvas.
Containers offer a useful
abstraction to gather
primitives.
Collections are the classes
that provide for the
efficient drawing of large
numbers of similar objects.
Matplotlib
Hierarchy Ellipse Collection
22
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
*https://aosabook.org/en/matplotlib.html
*Matplotlib Release 3.4.2, May 08, 2021, Page No. 460
*Mastering Matplotlib by Duncan M. McGreggor, Pg no.15
DEEP DIVE INTO ARTIST LAYER
The matplotlib artist
primitives are classes of
standard graphical
objects that are
supposed to be painted
on a figure's canvas.
Containers offer a useful
abstraction to gather
primitives.
Collections are the classes
that provide for the
efficient drawing of large
numbers of similar objects.
Matplotlib
Hierarchy
23
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
*https://aosabook.org/en/matplotlib.html
*Matplotlib Release 3.4.2, May 08, 2021, Page No. 460
*Mastering Matplotlib by Duncan M. McGreggor, Pg no.15
DEEP DIVE INTO ARTIST LAYER
Create a Figure instance.
Use the Figure to create one or more Axes or
Subplot instances
Use the Axes instance helper methods to create the
primitives
The Standard Approach to Plotting
using Artists
Matplotlib
Hierarchy
24
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
We will explore
the approach to plotting in more detail…
But prior to that, it is important to know
the elements (artists) of a matplotlib figure.
25
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
• Matplotlib Release 3.4.2, May 08, 2021, Pg No. 7-11
• https://dev.to/skotaro/artist-in-matplotlib---something-i-wanted-to-know-before-spending-tremendous-hours-on-googling-how-tos--31oo
ANATOMY OF A FIGURE
Line
(line plot)
Y
axis
label
Figure
X axis label
Spines
Major tick label
Y axis label
X axis label
Line
(line plot)
Markers
(scatter plot)
Axes
Grid
Major tick
Minor tick
Minor tick label
Legend
Figure is top level container for all
plot elements. It means the whole
window in user interface.
The Axes contains most of the
figure elements: Axis, Tick, Line2D,
Text, Polygon, etc., and sets the
coordinate system.
Figure can contain axes or subplots.
Title
26
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
• Matplotlib Release 3.4.2, May 08, 2021, Pg No. 7-11
• https://dev.to/skotaro/artist-in-matplotlib---something-i-wanted-to-know-before-spending-tremendous-hours-on-googling-how-tos--31oo
ANATOMY OF A FIGURE
Line
(line plot)
Figure
Spines
Major tick label
Y axis label
X axis label
Line
(line plot)
Markers
(scatter plot)
Axes
Grid
Major tick
Minor tick
Minor tick label
Legend
Figure is top level container for all
plot elements. It means the whole
window in user interface.
The Axes contains most of the
figure elements: Axis, Tick, Line2D,
Text, Polygon, etc., and sets the
coordinate system.
Figure can contain axes or subplots.
Title
27
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
• Matplotlib Release 3.4.2, May 08, 2021, Pg No. 7-11
• https://dev.to/skotaro/artist-in-matplotlib---something-i-wanted-to-know-before-spending-tremendous-hours-on-googling-how-tos--31oo
ANATOMY OF A FIGURE
Line
(line plot)
Figure
Spines
Major tick label
Y axis label
X axis label
Line
(line plot)
Markers
(scatter plot)
Axes
Grid
Major tick
Minor tick
Minor tick label
Legend
Figure is top level container for all
plot elements. It means the whole
window in user interface.
The Axes contains most of the
figure elements: Axis, Tick, Line2D,
Text, Polygon, etc., and sets the
coordinate system.
Figure can contain axes or subplots.
Title
28
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
• Matplotlib Release 3.4.2, May 08, 2021, Pg No. 7-11
• https://dev.to/skotaro/artist-in-matplotlib---something-i-wanted-to-know-before-spending-tremendous-hours-on-googling-how-tos--31oo
ANATOMY OF A FIGURE
Basically, everything you can see on the figure is an
artist (even the Figure, Axes, and Axis objects).
This includes Text objects, Line2D objects,
collections objects, Patch objects..
When the figure is rendered, all of the artists are
drawn to the canvas.
Most Artists are tied to an Axes; such an Artist
cannot be shared by multiple Axes, or moved from
one to another.
Line
(line plot)
Figure
Spines
Major tick label
Y axis label
X axis label
Line
(line plot)
Markers
(scatter plot)
Axes
Grid
Major tick
Minor tick
Minor tick label
Legend
Title
29
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
• Matplotlib Release 3.4.2, May 08, 2021, Pg No. 7-11
• https://dev.to/skotaro/artist-in-matplotlib---something-i-wanted-to-know-before-spending-tremendous-hours-on-googling-how-tos--31oo
Containers
Primitives
Let’s run down through the hierarchy
of Artists…
Representing Containers by yellow
colored boxes.
Representing Primitives by Salmon
colored boxes.
30
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
• Matplotlib Release 3.4.2, May 08, 2021, Pg No. 7-11
• https://dev.to/skotaro/artist-in-matplotlib---something-i-wanted-to-know-before-spending-tremendous-hours-on-googling-how-tos--31oo
Figure
Containers
Primitives
Figure is the Top Level container
Figure can contain other Containers and Primitives.
Figure can contain Axes and subplots.
Artist Type Color Code
The figure keeps track of all the child Axes, a
smattering of ‘special’ artists (titles, figure legends,
etc), and the canvas
31
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
• Matplotlib Release 3.4.2, May 08, 2021, Pg No. 7-11
• https://dev.to/skotaro/artist-in-matplotlib---something-i-wanted-to-know-before-spending-tremendous-hours-on-googling-how-tos--31oo
Figure
Containers
Primitives
Legend
Rectangle
Line2D
Text
Image
Axes
Artist Type Color Code
The figure keeps track of all the child Axes, a
smattering of ‘special’ artists (titles, figure legends,
etc), and the canvas
32
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
• Matplotlib Release 3.4.2, May 08, 2021, Pg No. 7-11
• https://dev.to/skotaro/artist-in-matplotlib---something-i-wanted-to-know-before-spending-tremendous-hours-on-googling-how-tos--31oo
Figure
Containers
Primitives
Legend
Rectangle
Line2D
Text
Image
Axes
Artist Type Color Code
Let us focus on Axes.
The Axes class is one of the most important.
It is the primary mover and shaker in the artist layer.
The reason for this is simple—the Axes instances
are where most of the matplotlib objects go (both
primitives and other containers)
Note the term ‘Axes‘ is not related to term ‘axis’.
33
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
• Matplotlib Release 3.4.2, May 08, 2021, Pg No. 7-11
• https://dev.to/skotaro/artist-in-matplotlib---something-i-wanted-to-know-before-spending-tremendous-hours-on-googling-how-tos--31oo
Figure
Containers
Primitives
YAxis
Line2D
PolyCollection
Legend
Rectangle
Text
Axes XAxis
Artist Type Color Code
“the Axes instances are where most of
the matplotlib objects go (both
primitives and other containers)“
34
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
• Matplotlib Release 3.4.2, May 08, 2021, Pg No. 7-11
• https://dev.to/skotaro/artist-in-matplotlib---something-i-wanted-to-know-before-spending-tremendous-hours-on-googling-how-tos--31oo
Figure
Containers
Primitives
YAxis
Line2D
PolyCollection
Legend
Rectangle
Text
Axes XAxis
Artist Type Color Code
In addition to the creation of primitives,
the methods of axes class can prepare
the supplied data to :
§ create primitives such as lines and
shapes
§ add primitives to the appropriate
containers, and
§ draw artists when called by some
other objects.
35
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
• Matplotlib Release 3.4.2, May 08, 2021, Pg No. 7-11
• https://dev.to/skotaro/artist-in-matplotlib---something-i-wanted-to-know-before-spending-tremendous-hours-on-googling-how-tos--31oo
Figure
Containers
Primitives
YAxis
Line2D
PolyCollection
Legend
Rectangle
Text
Axes XAxis
Artist Type Color Code
In addition to the creation of primitives,
the methods of axes class can prepare
the supplied data to :
§ create primitives such as lines and
shapes
§ add primitives to the appropriate
containers, and
§ draw artists when called by some
other objects.
36
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
• Matplotlib Release 3.4.2, May 08, 2021, Pg No. 7-11
• https://dev.to/skotaro/artist-in-matplotlib---something-i-wanted-to-know-before-spending-tremendous-hours-on-googling-how-tos--31oo
XAxis Label
XAxis Tick
Tick Major Line
Tick Minor Line
Tick Major Label
Tick Minor Label
Grid Line
Let’s focus on the two important containers
inside Axes : the XAxis and YAxis
The Axis instances handle the drawing of :
• tick lines
• grid lines
• tick labels
• axis label
Figure
Containers
Primitives
YAxis
Axes XAxis
Artist Type Color Code
YAxis Tick
Tick Major Line
Tick Minor Line
Tick Major Label
Tick Minor Label
Grid Line
YAxis Label
37
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
• Matplotlib Release 3.4.2, May 08, 2021, Pg No. 7-11
• https://dev.to/skotaro/artist-in-matplotlib---something-i-wanted-to-know-before-spending-tremendous-hours-on-googling-how-tos--31oo
XAxis Label
XAxis Tick
Tick Major Line
Tick Minor Line
Tick Major Label
Tick Minor Label
Grid Line
The Axis instances also store Locator and
Formatter instances which control :
• where the ticks are placed and
• how they are represented as strings.
Figure
Containers
Primitives
YAxis
Axes XAxis
Artist Type Color Code
YAxis Tick
Tick Major Line
Tick Minor Line
Tick Major Label
Tick Minor Label
Grid Line
YAxis Label
38
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Having reviewed the Artists Hierarchy…
We shall move on to the “Scripting Layer”
39
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Two ways to use Matplotlib:
• Pyplot API to automatically create and manage the
figures and axes, and use pyplot functions for plotting
• Explicitly create figures and axes, and call methods on
them (the "object-oriented (OO) style")
Matplotlib
Hierarchy
SCRIPTING LAYER – DUAL INTERFACE
TO MATPLOTLIB API
Scripting Layer is the User Facing Interface from where User
can make calls to the Artist layer and backend layer.
40
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
PYPLOT INTERFACE TO MATPLOTLIB
API
Pyplot Interface is a Stateful Interface
styled on MATLAB
matplotlib.pyplot is a collection of functions that
make matplotlib work like MATLAB.
• Each pyplot function makes some change to a figure.
• Various states are preserved across function calls, so
that pyplot keeps track of things like the current
figure and plotting area.
• All plotting functions apply to the current axes.
import matplotlib.pyplot as plt
plt.plot([1, 2, 3, 4])
plt.ylabel('some numbers’)
plt.show()
Plot y versus x as lines and/or markers
Set the label for the y-axis
Display all open figures
41
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Functions Description
plot(*args[, scalex, scaley, data]) Plot y versus x as lines and/or markers.
axes([arg]) Add an axes to the current figure and make it the current axes.
axis(*args[, emit]) Convenience method to get or set some axis properties.
xlabel(xlabel[, fontdict, labelpad, loc]) Set the label for the x-axis.
ylabel(ylabel[, fontdict, labelpad, loc]) Set the label for the y-axis.
xticks([ticks, labels]) Get or set the current tick locations and labels of the x-axis.
yticks([ticks, labels]) Get or set the current tick locations and labels of the y-axis.
grid Configure the grid lines
xlim(*args, **kwargs) Get or set the x limits of the current axes.
ylim(*args, **kwargs) Get or set the y-limits of the current axes.
legend(*args, **kwargs) Place a legend on the Axes.
figlegend(*args, **kwargs) Place a legend on the figure.
savefig(*args, **kwargs) Save the current figure.
show(*[, block]) Display all open figures.
FREQUENTLY USED MATPLOTLIB.PYPLOT FUNCTIONS For the Figure, Axes, Axis
The plt.axis() method allows you to set the x and y limits with a single call,
by passing a list that specifies [xmin, xmax, ymin, ymax].
https://matplotlib.org/stable/api/pyplot_summary.html?highlight=pyplot#pyplot-function-overview
42
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
FREQUENTLY USED MATPLOTLIB.PYPLOT FUNCTIONS For the Figure, Axes, Axis
Functions Description
suptitle(t, **kwargs) Add a centered suptitle to the figure.
annotate(text, xy, *args, **kwargs) Annotate the point xy with text text.
text(x, y, s[, fontdict]) Add text to the Axes.
title(label[, fontdict, loc, pad, y]) Set a title for the Axes.
figtext(x, y, s[, fontdict]) Add text to figure.
Text
https://matplotlib.org/stable/api/pyplot_summary.html?highlight=pyplot#pyplot-function-overview
43
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
FREQUENTLY USED MATPLOTLIB.PYPLOT FUNCTIONS For the Figure, Axes, Axis
Functions Description
axhline([y, xmin, xmax]) Add a horizontal line across the axis.
axvline([x, ymin, ymax]) Add a vertical line across the Axes.
axhspan(ymin, ymax[, xmin, xmax]) Add a horizontal span (rectangle) across the Axes.
axvspan(xmin, xmax[, ymin, ymax]) Add a vertical span (rectangle) across the Axes.
Line,
Span
https://matplotlib.org/stable/api/pyplot_summary.html?highlight=pyplot#pyplot-function-overview
44
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Functions Description
bar(x, height[, width, bottom, align, data]) Make a bar plot.
barh(y, width[, height, left, align]) Make a horizontal bar plot.
bar_label(container[, labels, fmt, ...]) Label a bar plot.
scatter(x, y[, s, c, marker, cmap, norm, ...]) A scatter plot of y vs. x
colorbar([mappable, cax, ax]) Add a colorbar to a plot
hist(x[, bins, range, density, weights, ...]) Plot a histogram.
hist2d(x, y[, bins, range, density, ...]) Make a 2D histogram plot.
boxplot(x[, notch, sym, vert, whis, ...]) Make a box and whisker plot.
violinplot(dataset[, positions, vert, ...]) Make a violin plot.
stackplot(x, *args[, labels, colors, ...]) Draw a stacked area plot.
pie(x[, explode, labels, colors, autopct, ...]) Plot a pie chart.
stem(*args[, linefmt, markerfmt, basefmt, ...]) Create a stem plot.
step(x, y, *args[, where, data]) Make a step plot.
hexbin(x, y[, C, gridsize, bins, xscale, ...]) Make a 2D hexagonal binning plot of points x, y.
table([cellText, cellColours, cellLoc, ...]) Add a table to an Axes.
FREQUENTLY USED MATPLOTLIB.PYPLOT FUNCTIONS For Charting
https://matplotlib.org/stable/api/pyplot_summary.html?highlight=pyplot#pyplot-function-overview
45
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
FREQUENTLY USED MATPLOTLIB.PYPLOT FUNCTIONS For Charting
Functions Description
hlines(y, xmin, xmax[, colors, linestyles, ...]) Plot horizontal lines at each y from xmin to xmax.
vlines(x, ymin, ymax[, colors, linestyles, ...]) Plot vertical lines at each x from ymin to ymax.
fill(*args[, data]) Plot filled polygons.
fill_between(x, y1[, y2, where, ...]) Fill the area between two horizontal curves.
fill_betweenx(y, x1[, x2, where, step, ...]) Fill the area between two vertical curves.
Line,
Area
https://matplotlib.org/stable/api/pyplot_summary.html?highlight=pyplot#pyplot-function-overview
46
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Functions Description
gcf() Get the current figure.
gca(**kwargs) Get the current Axes, creating one if necessary.
gci() Get the current colorable artist.
sca() Set the current Axes to ax and the current Figure to the parent of ax.
cla() Clear the current axes.
clf () Clear the current figure.
delaxes([ax]) Remove an Axes (defaulting to the current axes) from its figure.
findobj Find artist objects.
getp(obj, *args, **kwargs) Return the value of an Artist's property, or print all of them.
box([on]) Turn the axes box on or off on the current axes.
get(obj, *args, **kwargs) Return the value of an Artist's property, or print all of them.
setp(obj, *args, **kwargs) Set one or more properties on an Artist, or list allowed values.
tick_params([axis]) Change the appearance of ticks, tick labels, and gridlines.
locator_params([axis, tight]) Control behavior of major tick locators.
minorticks_off() Remove minor ticks from the axes.
minorticks_on() Display minor ticks on the axes
FREQUENTLY USED MATPLOTLIB.PYPLOT FUNCTIONS Helper Methods
Current
Axes,
Figure
https://matplotlib.org/stable/api/pyplot_summary.html?highlight=pyplot#pyplot-function-overview
Inspecting,
Changing
Properties
47
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Functions Description
autumn() Set the colormap to 'autumn'.
bone() Set the colormap to 'bone'.
cool() Set the colormap to 'cool'.
copper() Set the colormap to 'copper'.
flag() Set the colormap to 'flag'.
gray() Set the colormap to 'gray'.
hot() Set the colormap to 'hot'.
hsv() Set the colormap to 'hsv'.
inferno() Set the colormap to 'inferno'.
jet() Set the colormap to 'jet'.
magma() Set the colormap to 'magma'.
nipy_spectral() Set the colormap to 'nipy_spectral'.
pink() Set the colormap to 'pink'.
FREQUENTLY USED MATPLOTLIB.PYPLOT FUNCTIONS
For Colormaps,
Pseudo Colorplots
Functions Description
pcolor(*args[, shading, alpha,
norm, cmap, ...])
Create a pseudocolor plot with a non-
regular rect- angular grid.
pcolormesh(*args[, alpha,
norm, cmap, ...])
Create a pseudocolor plot with a non-
regular rect- angular grid.
imread(fname[, format]) Read an image from a file into an array.
imsave(fname, arr, **kwargs) Save an array as an image file.
imshow(X[, cmap, norm, aspect,
...])
Display data as an image, i.e., on a 2D
regular raster.
https://matplotlib.org/stable/api/pyplot_summary.html?highlight=pyplot#pyplot-function-overview
48
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Functions Description
subplot(*args, **kwargs) Add an Axes to the current figure or retrieve an existing
Axes.
subplots([nrows, ncols, sharex, sharey, ...]) Create a figure and a set of subplots.
subplot2grid(shape, loc[, rowspan, colspan,
fig])
Create a subplot at a specific location inside a reg- ular
grid.
subplots_adjust([left, bottom, right, top, ...]) Adjust the subplot layout parameters.
tight_layout(*[, pad, h_pad, w_pad, rect]) Adjust the padding between and around subplots.
FREQUENTLY USED MATPLOTLIB.PYPLOT FUNCTIONS For Multiple plots, Layout Customization
https://matplotlib.org/stable/api/pyplot_summary.html?highlight=pyplot#pyplot-function-overview
49
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
names = ['group_a', 'group_b', 'group_c']
values = [1, 10, 100]
plt.figure(figsize=(9, 3))
plt.subplot(131)
plt.bar(names, values)
plt.subplot(132)
plt.scatter(names, values)
plt.subplot(133)
plt.plot(names, values)
plt.suptitle('Categorical Plotting’)
plt.savefig('Categorical Plotting.png’, 
bbox_inches = "tight", 
pad_inches = 0.15, dpi=300)
plt.show()
Code Snippet
plt.subplot adds axes to current figure.
Everytime it is called, it resets the current axes.
131 means in a grid of one row by three columns,
the subplot at index position 1 is set as the current axes.
Subsequent pyplot functions will be applicable to current axes.
132 means in a grid of one row by three columns,
the subplot at index position 2 is set as the current axes.
A Bar chart is plotted on the current axes.
A Scatter chart is plotted on the current axes.
The current axes is reset.
Current Axes is reset and a Line plot is drawn.
Centered suptitle is set in the Figure.
Figure is saved as png file.
Figure is displayed in user screen
Subplot function to add/change current axes
Pyplot function on current axes
50
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
names = ['group_a', 'group_b', 'group_c']
values = [1, 10, 100]
plt.figure(figsize=(9, 3))
plt.subplot(131)
plt.bar(names, values)
plt.subplot(132)
plt.scatter(names, values)
plt.subplot(133)
plt.plot(names, values)
plt.suptitle('Categorical Plotting’)
plt.savefig('Categorical Plotting.png’, 
bbox_inches = "tight", 
pad_inches = 0.15, dpi=300)
plt.show()
Code Snippet
Subplot function to add/change current axes
Pyplot function on current axes
51
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
CONFIGURATIONS
Functions Description
rc(group, **kwargs) Set the current rcParams. group is the grouping for the rc, e.g., for
lines.linewidth the group is lines, for axes.facecolor, the group is axes, and
so on. Group may also be a list or tuple of group names, e.g., (xtick, ytick).
kwargs is a dictionary attribute name/value pairs,
rc_context([rc, fname]) Return a context manager for temporarily changing rcParams.
rcdefaults() Restore the rcParams from Matplotlib's internal default style.
FREQUENTLY USED MATPLOTLIB.PYPLOT FUNCTIONS For Fine Tuning your Plot Appearance
Customizing Matplotlib Configurations
Change
rcParams
settings
*rc refers to Matplotlib runtime configurations
import matplotlib as mpl
mpl.rcParams['lines.linewidth']
import matplotlib as mpl
mpl.rcParams['lines.linewidth’] = 2
Inspect
Change
*Matplotlib Release 3.4.2, May 08, 2021, Customizing Matplotlib with style sheets and rcParams, Pg No.84, 25
52
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
FREQUENTLY USED MATPLOTLIB.PYPLOT FUNCTIONS “rcParams” – dict like Global variable
Stores settings
Dictionary Like Variable that stores rc settings and Global to Matplotlib package
Functions Description
mpl.rcParamsDefaults Stores the default rcParams.
mpl.rcParamsOrig Stores the rcParams in original matplotlibrc file
mpl.rcParams Stores the current rcParams
Settings
Stored
*Matplotlib Release 3.4.2, May 08, 2021, Customizing Matplotlib with style sheets and rcParams, Pg No.86
53
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Import matplotlib.pyplot as plt
from matplotlib import cycler
colors = cycler('color’, ['#EE6666', '#3388BB’,
'#9988DD', '#EECC55’, '#88BB44’,
'#FFBBBB’])
plt.rc('axes', facecolor='#E6E6E6’,
edgecolor='none', axisbelow=True,
grid=True,
prop_cycle=colors)
plt.rc('grid', color='w', linestyle='solid’)
plt.rc('xtick', direction='out', color='gray’)
plt.rc('ytick', direction='out', color='gray’)
plt.rc('patch', edgecolor='#E6E6E6’)
plt.rc('lines', linewidth=2)
Code Snippet for customizing configurations Before Customizing
After Customizing
rcParams function for global changes
*Python Data Science Handbook by Jake VanderPlas, Page No.284
54
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Code Snippet
Before Reverting Back
After restoring defaults
mpl.rcParams.update(mpl.rcParamsOrig)
mpl.rcParams.update(mpl.rcParamsDefaults)
*https://stackoverflow.com/questions/26899310/python-seaborn-to-reset-back-to-the-matplotlib
OR
rcParams dictionary variables to revert
back settings
55
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
import matplotlib as mpl
my_params = mpl.rcParams
# apply some change to the rcparams here
mpl.rcParams.update(my_params)
Code Snippet
mpl.rcParams.update(mpl.rcParamsOrig)
mpl.rcParams.update(mpl.rcParamsDefaults)
*https://stackoverflow.com/questions/26899310/python-seaborn-to-reset-back-to-the-matplotlib
Best Practices
OR
rcParams dictionary variables to revert
back settings
Store current rcParams as a variable prior to any change.
Restore the rcParams with the earlier stored variable.
Do some changes in rcParams, plot using new settings
56
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
FREQUENTLY USED MATPLOTLIB.PYPLOT FUNCTIONS
STYLE SHEETS
plt.style.use('stylename') stylename from any of the available style names :
['Solarize_Light2', '_classic_test_patch', 'bmh', 'classic', 'dark_background ',
'fast', 'fivethirtyeight', 'ggplot', 'grayscale', 'seaborn', 'seaborn- bright',
'seaborn-colorblind', 'seaborn-dark', 'seaborn-dark-palette', 'seaborn-
darkgrid', 'seaborn-deep', 'seaborn-muted', 'seaborn-notebook', 'seaborn-
paper', 'seaborn-pastel', 'seaborn-poster', 'seaborn-talk', 'seaborn-ticks',
'seaborn-white', 'seaborn-whitegrid', 'tableau-colorblind10 ']
Use plt.style.available to get the available style names list
*Matplotlib Release 3.4.2, May 08, 2021, Customizing Matplotlib with style sheets and rcParams, Pg No.84
For Fine Tuning your Plot Appearance
Using StyleSheets
Use plt.style.context to apply style temporarily.
https://matplotlib.org/stable/gallery/style_sheets/style_sheets_reference.html
57
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
fig = plt.figure(dpi=100, figsize=(24, 20),
tight_layout=True)
available = ['default'] + plt.style.available
x = ['Product A','Product B', 'Product C']
y = [100,130,200]
for i, style in enumerate(available):
with plt.style.context(style):
ax = fig.add_subplot(6,5 , i + 1)
ax.bar(x,y)
ax.set_title(style)
plt.savefig('All styles.png’,
bbox_inches = "tight",
pad_inches = 0.05, dpi=300)
plt.show()
Code Snippet for showcasing all styles
All Available Stylesheets
Code
58
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Zooming in - First set of 6# Stylesheets
Styles in this slide
'default’
'Solarize_Light2'
'_classic_test_patch'
'bmh'
'classic'
'dark_background '
'fast'
'fivethirtyeight'
'ggplot'
'grayscale'
'seaborn'
'seaborn- bright'
'seaborn-colorblind'
'seaborn-dark'
'seaborn-dark-palette'
'seaborn-darkgrid'
'seaborn-deep'
'seaborn-muted'
'seaborn-notebook'
'seaborn-paper'
'seaborn-pastel'
'seaborn-poster'
'seaborn-talk'
'seaborn-ticks'
'seaborn-white'
'seaborn-whitegrid'
'tableau-colorblind10 '
Styles on
Right
59
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Styles in this slide
'default’
'Solarize_Light2'
'_classic_test_patch'
'bmh'
'classic'
'dark_background '
'fast'
'fivethirtyeight'
'ggplot'
'grayscale'
'seaborn'
'seaborn- bright'
'seaborn-colorblind'
'seaborn-dark'
'seaborn-dark-palette'
'seaborn-darkgrid'
'seaborn-deep'
'seaborn-muted'
'seaborn-notebook'
'seaborn-paper'
'seaborn-pastel'
'seaborn-poster'
'seaborn-talk'
'seaborn-ticks'
'seaborn-white'
'seaborn-whitegrid'
'tableau-colorblind10 '
Styles on
Right
Zooming in - Second set of 6# Stylesheets
60
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Styles in this slide
'default’
'Solarize_Light2'
'_classic_test_patch'
'bmh'
'classic'
'dark_background '
'fast'
'fivethirtyeight'
'ggplot'
'grayscale'
'seaborn'
'seaborn- bright'
'seaborn-colorblind'
'seaborn-dark'
'seaborn-dark-palette'
'seaborn-darkgrid'
'seaborn-deep'
'seaborn-muted'
'seaborn-notebook'
'seaborn-paper'
'seaborn-pastel'
'seaborn-poster'
'seaborn-talk'
'seaborn-ticks'
'seaborn-white'
'seaborn-whitegrid'
'tableau-colorblind10 '
Styles on
Right
Zooming in - Third set of 6# Stylesheets
61
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Styles in this slide
'default’
'Solarize_Light2'
'_classic_test_patch'
'bmh'
'classic'
'dark_background '
'fast'
'fivethirtyeight'
'ggplot'
'grayscale'
'seaborn'
'seaborn- bright'
'seaborn-colorblind'
'seaborn-dark'
'seaborn-dark-palette'
'seaborn-darkgrid'
'seaborn-deep'
'seaborn-muted'
'seaborn-notebook'
'seaborn-paper'
'seaborn-pastel'
'seaborn-poster'
'seaborn-talk'
'seaborn-ticks'
'seaborn-white'
'seaborn-whitegrid'
'tableau-colorblind10 '
Styles on
Right
Zooming in - Fourth set of 6# Stylesheets
62
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Styles in this slide
'default’
'Solarize_Light2'
'_classic_test_patch'
'bmh'
'classic'
'dark_background '
'fast'
'fivethirtyeight'
'ggplot'
'grayscale'
'seaborn'
'seaborn- bright'
'seaborn-colorblind'
'seaborn-dark'
'seaborn-dark-palette'
'seaborn-darkgrid'
'seaborn-deep'
'seaborn-muted'
'seaborn-notebook'
'seaborn-paper'
'seaborn-pastel'
'seaborn-poster'
'seaborn-talk'
'seaborn-ticks'
'seaborn-white'
'seaborn-whitegrid'
'tableau-colorblind10 '
Zooming in - Fifth set of 3# Stylesheets
Styles on
Right
63
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
OO INTERFACE TO MATPLOTLIB
ü A Stateless Interface
ü Does not rely on the concept of current axes and current figure
ü Explicitly object instances are named and methods invoked
ü Anatomy of a figure and the Hierarchy of artists is respected
ü Consider Pyplot Interface as
a brief interlude
ü It’s a good starting point
ü Hands-on and convenient
ü But to gain finely grained
control over every aspect
of plot
ü OO is the way
ü We will take it step by step
ü Review matplotlib landscape
ü Build on our learnings
ü Partly incremental and partly
undertake few leaps
ü This is an attempt to bring a structure, build intuition
to approach the plotting into breakable tasks
ü The goal is also to be the go-to reference guide for
the major part of business analytics related EDA
ü Understand Matplotlib API
ü Understand OO interface and settle on
mix of OO and pyplot
ü Eventually use OO interface seamlessly
with Seaborn, Pandas, GeoPandas
BROADER
GOAL
SPECIFIC
OUTCOMES
ENTRY POINT INFLECTION POINT ACTIONABLES
64
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add artists to
respective containers within the Axes
object.
3
Create a Figure instance
1
Use Figure to create one or more Axes or
Subplot
2
Use Axes Helper Methods (Accessors) to
access artists through attributes.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes Helper Methods to access x-axis
and y-axis tick, tick labels and axis labels.
4
data = {'Water':465,
'Alchoholic Beverages':271,
'Carbonated soft drinks': 224,
'Milk and Dairy products': 260,
'New Drinks': 75,
'Fruits and Vegetable Juices': 86}
x = list(data.keys())
y = list(data.values())
import matplotlib.pyplot as plt
%matplotlib inline
#Creating figure and axes instances
fig, ax = plt.subplots(figsize = (10,5), frameon = True)
#Creating barplot using axes.bar() method
colors = [ plt.cm.Dark2(x) for x in np.linspace(0, 1, len(x))]
ax.bar(x,y, color = colors)
#Setting y limits
yupperlimit = ax.get_ylim()[1]*1.1
ax.set_ylim(0,yupperlimit)
#Making x-tick lines invisible
[ ticks.set_visible(False] for ticks in ax.get_xticklines() ]
#Rotating and aligning the xtick labels
for labels in ax.get_xticklabels():
labels.set(size = 12, rotation = 15, rotation_mode = 'anchor’, ha = 'right')
# Adding Annotations to place data labels
for p in ax.patches:
ax.text(x = p.get_x()+p.get_width()/2,
y = ax.get_ylim()[1]*0.01+ p.get_height(),
s = p.get_height(),
ha = 'center', size = 12)
#Setting the properties of rectangular patch in axes and figure
ax.patch.set(facecolor = 'white')
fig.patch.set(facecolor = 'white’, edgecolor = 'grey', lw = 4)
ax.grid(True)
#Setting axes title
StrTitle = 'Consumption of Packed beverages in billion liters,n by Beverage Type, Global, 2019’
ax.set_title(StrTitle, size = 16)
#Adding Legend labels and handles
[p.set_label(x[i]) for i,p in zip(range(len(x)), ax.patches)]
ax.legend(bbox_to_anchor = (1.04,.5), loc="upper left",borderaxespad=0)
#Saving the figure locally
fig.savefig('Consumption_2.png', dpi=300, format='png', bbox_inches='tight’)
PREPARING THE DATA
CODE SCRIPTING
PLOTTING OUTPUT
Add Legend and Set Title to the Axes
7
OO APPROACH – A COMPLETE RUNDOWN
65
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add artists to
respective containers within the Axes
object.
3
Create a Figure instance
1
Use Figure to create one or more Axes or
Subplot
2
Use Axes Helper Methods (Accessors) to
access artists through attributes.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes Helper Methods to access x-axis
and y-axis tick, tick labels and axis labels.
4
Add Legend and Set Title to the Axes
7
data = {'Water':465,
'Alchoholic Beverages':271,
'Carbonated soft drinks': 224,
'Milk and Dairy products': 260,
'New Drinks': 75,
'Fruits and Vegetable Juices': 86}
x = list(data.keys())
y = list(data.values())
PREPARING THE DATA
OO APPROACH – PREPARING THE DATA
66
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Add Legend and Set Title to the Axes
7
Set/Modify the Artist properties after
accessing the artists.
6
Use Axes Helper Methods (Accessors) to
access artists through attributes.
5
Use Axes Helper Methods to access and
set x-axis and y-axis tick, tick labels and
axis labels.
4
Use Axes Helper Methods to add artists to
respective containers within the Axes
object.
3
Use Figure to create one or more Axes or
Subplot
2
Create a Figure instance
1
import matplotlib.pyplot as plt
%matplotlib inline
#Creating figure and axes instances
fig, ax = plt.subplots(figsize = (10,5), frameon = True)
#Creating barplot using axes.bar() method
colors = [ plt.cm.Dark2(x) for x in np.linspace(0, 1, len(x))] #Accessing colors from colormap
ax.bar(x,y, color = colors)
#Setting y limits
yupperlimit = ax.get_ylim()[1]*1.1
ax.set_ylim(0,yupperlimit)
#Making x-tick lines invisible
[ ticks.set_visible(False] for ticks in ax.get_xticklines() ]
#Rotating and aligning the xtick labels
for labels in ax.get_xticklabels():
labels.set(size = 12, rotation = 15, rotation_mode = 'anchor’, ha = 'right')
# Adding Annotations to place data labels
for p in ax.patches:
ax.text(x = p.get_x()+p.get_width()/2,
y = ax.get_ylim()[1]*0.01+ p.get_height(),
s = p.get_height(),
ha = 'center', size = 12)
#Setting the properties of rectangular patch in axes and figure
ax.patch.set(facecolor = 'white’) #OO style to set properties
plt.setp(fig.patch, facecolor = 'white’, edgecolor = 'grey', lw = 4). #Pyplot style to set properties
ax.grid(True)
# Setting axes title
StrTitle = 'Consumption of Packed beverages in billion liters,n by Beverage Type, Global, 2019’
ax.set_title(StrTitle, size = 16)
# Adding Legend labels and handles
[p.set_label(x[i]) for i,p in zip(range(len(x)), ax.patches)]
ax.legend(bbox_to_anchor = (1.04,.5), loc="upper left",borderaxespad=0)
fig.savefig('Consumption_2.png', dpi=300, format='png', bbox_inches='tight’)
OO APPROACH – CODE SCRIPTING
Check slide
67
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Add Legend and Set Title to the Axes
7
Set/Modify the Artist properties after
accessing the artists.
6
Use Axes Helper Methods (Accessors) to
access artists through attributes.
5
Use Axes Helper Methods to access and
set x-axis and y-axis tick, tick labels and
axis labels.
4
Use Axes Helper Methods to add artists to
respective containers within the Axes
object.
3
Use Figure to create one or more Axes or
Subplot
2
Create a Figure instance
1
PLOTTING OUTPUT
OO APPROACH – PLOTTING OUTPUT
Check slide
68
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
CREATING FIGURE AND AXES
Method Description Page No.
fig, ax = plt.subplots() Instantiate a figure and axes 2650
fig = plt.figure() Instantiate a figure 2528
METHODS TO CREATE NEW SUBPLOTS
fig.subplots(nrows, ncols) Primary function to create and place all axes on the figure
at once by dividing figure into grids
2117
fig.add_subplot( (nrows, ncols, index) Figure method to add axes by specifying the index in a grid
layout pattern
2077,
2117
fig.add_gridspec( nrows, ncols) Figure method to return gridspec that specifies the
geometry of the grid that a subplot will be placed.
2076,
2174
fig.add_subplot(SubplotSpec) Figure method to add axes by specifying the location of
subplot in a GridSpec
2076
2174
fig.add_axes(rect) Figure method to add axes at any location within the Figure2074
fig.delaxes(axes object) Figure method to delete an axes
ax.remove() Axes method to remove an axes
CUSTOMIZE SUBPLOT
fig.subplots_adjust(left, bottom, right,
top, wspace, hspace )
Adjust the subplot layout parameters. 2119
tight_layout(pad, h_pad, w_pad, rect) Adjust the padding between and around subplots. 2124
ax.set_anchor(anchor) Define the anchor location. Anchor values can be ‘C’, N, NW,W,
SW,S, SE,E,NE
fig.align_labels() Align the labels of subplots in the same subplot column if
label alignment is being done automatically
2080-
2082
fig.align_xlabels()
fig.align_ylabels()
OO APPROACH – A DEEP DIVE
69
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
#Using fig.subplots to place multiple
axes in the figure
import matplotlib.pyplot as plt
plt.style.use('seaborn’)
%matplotlib inline
fig = plt.figure()
ax = fig.subplots(2,2)
#Using fig.subplots
import matplotlib.pyplot as plt
plt.style.use('seaborn')
%matplotlib inline
rows, cols = 2,2
fig = plt.figure()
ax = fig.subplots(rows,cols,
sharex = True,sharey = True)
#Adding text by looping through all the
subplots/axes
ax_iter = ax.flatten()
for i,axes in enumerate(ax_iter):
axes.text(0.5, 0.5,
f'subplot{rows,cols,i}',
ha='center', va='center',
size=20, color="steelblue")
axes.grid(False)
DEMO OF FIG.SUBPLOTS()
DEMO OF FIG.SUBPLOTS() AND ENUMERATE
70
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
#Using add_subplots to add axes
import matplotlib.pyplot as plt
plt.style.use('seaborn’)
%matplotlib inline
fig = plt.figure()
ax1 = fig.add_subplot(2,3,1)
ax2 = fig.add_subplot(2, 3, (3,6))
#Using Gridspec with add_subplots
import matplotlib.gridspec as gridspec
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(12, 8))
spec = gridspec.GridSpec(nrows = 2,
ncols = 2,
wspace = 0.2,
hspace=0.1)
ax1 = fig.add_subplot(spec[0, 0])
ax2 = fig.add_subplot(spec[1, 0])
ax3 = fig.add_subplot(spec[:, 1])
ax1
ax2
ax1
ax2
ax3
Note : Indexing is 1 based.
Note : Indexing is 0 based.
71
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
#Using fig.add_axes() to add subplots
import matplotlib.pyplot as plt
plt.rcdefaults()
fig = plt.figure()
ax1 = fig.subplots()
ax2 = fig.add_axes([0.3,0.7,0.4,0.1])
ax2.tick_params(labelsize=8, length=0)
*Note : This method used to Add an axes at
position rect[left, bottom,width, height]
where all quantities are in fractions of figure
width and height.
72
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
AXES HELPER METHODs TO CREATE PRIMITIVES
Axes helper method Artist Container
annotate - text annotations Annotation ax.texts
bar - bar charts Rectangle ax.patches and Barcontainer
errorbar - error bar plots Line2D and Rectangle ax.lines and ax.patches
fill - shared area Polygon ax.patches
hist - histograms Rectangle ax.patches
imshow - image data AxesImage ax.images
legend - Axes legends Legend ax.legend()
plot - xy plots Line2D ax.lines
scatter - scatter charts PolyCollection ax.collections
text - text Text ax.texts
PRIMITIVES
PRIMITIVES
AXES HELPER
METHOD
CONTAINER
CONTAINER
ü To get a list of all containers in the axes, use ax.containers
ü To get list of all artists in the axes, use ax.get_children()
*Check Artist Tutorial
73
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
Category Method Description
Page
No.
Basic
Axes.plot Plot y versus x as lines and/or markers. 1241
Axes.errorbar Plot y versus x as lines and/or markers with attached errorbars. 1241
Axes.scatter A scatter plot of y vs. 1241
Axes.plot_date Plot coercing the axis to treat floats as dates. 1241
Axes.step Make a step plot. 1241
Axes.loglog Make a plot with log scaling on both the x and y axis. 1241
Axes.semilogx Make a plot with log scaling on the x axis. 1241
Axes.semilogy Make a plot with log scaling on the y axis. 1241
Axes.fill_between Fill the area between two horizontal curves. 1241
Axes.fill_betweenx Fill the area between two vertical curves. 1241
Axes.bar Make a bar plot. 1241
Axes.barh Make a horizontal bar plot. 1241
Axes.bar_label Label a bar plot. 1241
Axes.stem Create a stem plot. 1242
Axes.eventplot Plot identical parallel lines at the given positions. 1242
Axes.pie Plot a pie chart. 1242
Axes.stackplot Draw a stacked area plot. 1242
Axes.broken_barh Plot a horizontal sequence of rectangles. 1242
Axes.vlines Plot vertical lines at each x from ymin to ymax. 1242
Axes.hlines Plot horizontal lines at each y from xmin to xmax. 1242
Axes.fill Plot filled polygons. 1242
AXES METHODS TO CREATE DIFFERENT PLOTS AND TEXT (1/3)
74
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
Category Method Description
Page
No.
Spans
Axes.axhline Add a horizontal line across the axis. 1296
Axes.axhspan Add a horizontal span (rectangle) across the Axes. 1296
Axes.axvline Add a vertical line across the Axes. 1296
Axes.axvspan Add a vertical span (rectangle) across the Axes. 1296
Axes.axline Add an infinitely long straight line. 1296
Spectral Axes.xcorr Plot the cross correlation between x and y. 1307
Statistics
Axes.boxplot Make a box and whisker plot. 1334
Axes.violinplot Make a violin plot. 1334
Axes.violin Drawing function for violin plots. 1334
Axes.bxp Drawing function for box and whisker plots. 1334
Binned
Axes.hexbin Make a 2D hexagonal binning plot of points x, y. 1344
Axes.hist Plot a histogram. 1344
Axes.hist2d Make a 2D histogram plot. 1344
Axes.stairs A stepwise constant function as a line with bounding edges
or a filled plot.
1344
Text and
annotations
Axes.annotate Axes.annotate(self, text, xy, *args, **kwargs) Annotate the
point xy with text text.
1399
Axes.text Annotate the point xy with text text. 1399
Axes.table Add text to the Axes. 1399
Axes.arrow Add a table to an Axes. 1399
Axes.inset_axes Add an arrow to the Axes. 1399
Axes.indicate_inset Add a child inset Axes to this existing Axes. 1399
Axes.indicate_inset_z
oom
Add an inset indicator to the Axes. 1399
Axes.secondary_xaxisAdd an inset indicator rectangle to the Axes based on the
axis limits for an inset_ax and draw connectors between
inset_ax and the rectangle.
1399
Axes.secondary_yaxisAdd a second x-axis to this Axes. 1399
AXES METHODS TO CREATE DIFFERENT PLOTS AND TEXT (2/3)
75
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
Category Method Description
Page
No.
2D Arrays
Axes.imshow Display data as an image, i.e., on a 2D regular raster. 1369
Axes.matshow Plot the values of a 2D matrix or array as color-coded image. 1369
Axes.pcolor Create a pseudocolor plot with a non-regular rectangular grid. 1369
Axes.pcolorfast Create a pseudocolor plot with a non-regular rectangular grid. 1369
Axes.pcolormesh Create a pseudocolor plot with a non-regular rectangular grid. 1369
Axes.spy Plot the sparsity pattern of a 2D array. 1369
https://matplotlib.org/stable/api/axes_api.html?highlight=axes#module-matplotlib.axes
AXES METHODS TO CREATE DIFFERENT PLOTS AND TEXT (3/3)
76
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
matplotlib.collections.PolyCollection matplotlib.patches.Wedge
Axes.patches Axes.texts
matplotlib.patches.Rectangle
Axes.patches
Barcontainer
matplotlib.legend.Legend
matplotlib.patches.Rectangle
Axes.patches
Code Code
Code
Code
77
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
Code
Code
78
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
VISUALIZING MULTI DIMENSIONAL DISTRIBUTIONS
Code
79
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
Source : https://www.machinelearningplus.com/plots/top-50-matplotlib-visualizations-the-
master-plots-python/#13.-Diverging-Lollipop-Chart-with-Markers
Code
80
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
TICK
TICK LABELS
GRIDLINES
81
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
Category Method Description
Page
No.
Appearance
✓ Axes.axis Convenience method to get or set some axis properties. 1432
✓ Axes.set_axis_off Turn the x- and y-axis off. 1432
✓ Axes.set_axis_on Turn the x- and y-axis on. 1432
✓ Axes.set_frame_on Set whether the axes rectangle patch is drawn. 1432
Axes.get_frame_on Get whether the axes rectangle patch is drawn. 1432
✓ Axes.set_axisbelow Set whether axis ticks and gridlines are above or below
most artists.
1432
Axes.get_axisbelow Get whether axis ticks and gridlines are above or below
most artists.
1432
✓ Axes.grid Configure the grid lines. 1432
Axes.get_facecolor Get the facecolor of the Axes. 1432
✓ Axes.set_facecolor Set the facecolor of the Axes. 1432
Property Cycle ✓✓ Axes.set_prop_cycle Set the property cycle of the Axes. 1440
Axis / limits
✓ Axes.get_xaxis Return the XAxis instance. 1442
✓ Axes.get_yaxis Return the YAxis instance. 1442
Axis limits and
direction
Axes.invert_xaxis Invert the x-axis. 1442
Axes.xaxis_inverted Return whether the xaxis is oriented in the "inverse"
direction.
1442
✓ Axes.invert_yaxis Invert the y-axis. 1442
Axes.yaxis_inverted Return whether the yaxis is oriented in the "inverse"
direction.
1442
✓✓ Axes.set_xlim Set the x-axis view limits. 1442
✓ Axes.get_xlim Return the x-axis view limits. 1442
✓✓ Axes.set_ylim Set the y-axis view limits. 1442
✓ Axes.get_ylim Return the y-axis view limits. 1442
AXES HELPER METHODS FOR APPEARANCE, AXIS LIMITS
✓ indicates frequency and importance of usage of the method
82
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
Category Method Description
Page
No.
Ticks
and
Tick Labels
Axes.set_xticks Set the xaxis' tick locations. 1491
Axes.get_xticks Return the xaxis' tick locations in data coordinates. 1491
✓ Axes.set_xticklabels Set the xaxis' labels with list of string labels. 1491
✓✓ Axes.get_xticklabels Get the xaxis' tick labels. 1491
✓ Axes.get_xmajorticklabels Return the xaxis' major tick labels, as a list of Text. 1491
✓ Axes.get_xminorticklabels Return the xaxis' minor tick labels, as a list of Text. 1491
✓ Axes.get_xgridlines Return the xaxis' grid lines as a list of Line2Ds. 1491
✓ Axes.get_xticklines Return the xaxis' tick lines as a list of Line2Ds. 1491
Axes.xaxis_date Set up axis ticks and labels to treat data along the xaxis
as dates.
1491
Axes.set_yticks Set the yaxis' tick locations. 1491
Axes.get_yticks Return the yaxis' tick locations in data coordinates. 1491
✓ Axes.set_yticklabels Set the yaxis' labels with list of string labels. 1491
✓✓ Axes.get_yticklabels Get the yaxis' tick labels. 1491
✓ Axes.get_ymajorticklabels Return the yaxis' major tick labels, as a list of Text. 1491
✓ Axes.get_yminorticklabels Return the yaxis' minor tick labels, as a list of Text. 1491
✓ Axes.get_ygridlines Return the yaxis' grid lines as a list of Line2Ds. 1491
✓ Axes.get_yticklines Return the yaxis' tick lines as a list of Line2Ds. 1491
Axes.yaxis_date Set up axis ticks and labels to treat data along the yaxis
as dates.
1491
✓ Axes.minorticks_off Remove minor ticks from the axes. 1491
✓ Axes.minorticks_on Display minor ticks on the axes. 1491
Axes.ticklabel_format Configure the ScalarFormatter used by default for linear
axes.
1491
✓✓ Axes.tick_params Change the appearance of ticks, tick labels, and gridlines. 1491
✓ Axes.locator_params Control behavior of major tick locators. 1491
AXES HELPER METHODS FOR TICKS AND TICK LABELS, GRID LINES
✓ indicates frequency and importance of usage of the method
Also, can use plt.yticks([ticks, labels]), plt.xticks([ticks,labels]) to set ticks and labels in one call.
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
Category Method Description Page
No.
Axis labels,
title,
and legend
✓ Axes.set_xlabel Set the label for the x-axis. 1445
✓ Axes.get_xlabel Get the xlabel text string. 1445
✓ Axes.set_ylabel Set the label for the y-axis. 1445
✓ Axes.get_ylabel Get the ylabel text string. 1445
✓ Axes.set_title Set a title for the Axes. 1445
Axes.get_title Get an Axes title. 1445
✓ Axes.legend Place a legend on the Axes. 1445
Axes.get_legend Return the Legend instance, or None if no legend is defined. 1445
Axes.get_legend_handl
es_labels
Return handles and labels for legend 1445
✓ Axes.set_xlabel Set the label for the x-axis. 1445
Axes.get_xlabel Get the xlabel text string. 1445
✓ Axes.set_ylabel Set the label for the y-axis. 1445
Axes.get_ylabel Get the ylabel text string. 1445
✓ Axes.set_title Set a title for the Axes. 1445
Twinning
and
sharing
Axes.twinx Create a twin Axes sharing the xaxis. 1513
Axes.twiny Create a twin Axes sharing the yaxis. Check link. 1513
Axes.sharex Share the x-axis with other. 1513
Axes.sharey Share the y-axis with other. 1513
Axes.get_shared_x_axesReturn a reference to the shared axes Grouper object for x
axes.
1513
Axes.get_shared_y_axes Return a reference to the shared axes Grouper object for y
axes.
1513
AXES HELPER METHODS FOR AXIS LABELS, TITLE AND LEGEND
✓ indicates frequency and importance of usage of the method
84
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
AXIS HELPER METHODS (1/3)
Category Method Description
Page
No.
Formatters
and Locators
Axis.get_major_formatter Get the formatter of the major ticker. 1540
Axis.get_major_locator Get the locator of the major ticker. 1540
Axis.get_minor_formatter Get the formatter of the minor ticker. 1540
Axis.get_minor_locator Get the locator of the minor ticker. 1540
✓ Axis.set_major_formatter Set the formatter of the major ticker. 1540
✓ Axis.set_major_locator Set the locator of the major ticker. 1540
✓ Axis.set_minor_formatter Set the formatter of the minor ticker. 1540
✓ Axis.set_minor_locator Set the locator of the minor ticker. 1540
Axis.remove_overlapping_locs If minor ticker locations that overlap with
major ticker locations should be trimmed.
1540
Axis Label Axis.set_label_coords Set the coordinates of the label. 1545
✓ Axis.set_label_position Set the label position (top or bottom) 1545
Axis.set_label_text Set the text value of the axis label. 1545
✓ indicates frequency and importance of usage of the method
85
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
AXIS HELPER METHODS (2/3)
Category Method Description
Page
No.
Ticks,
tick labels,
Offset text,
GridLines
✓ Axis.get_major_ticks Return the list of major Ticks. 1547
✓ Axis.get_majorticklabels Return this Axis' major tick labels, as a list of Text. 1547
✓ Axis.get_majorticklines Return this Axis' major tick lines as a list of Line2Ds. 1547
Axis.get_majorticklocs Return this Axis' major tick locations in data coordinates. 1547
Axis.get_minor_ticks Return the list of minor Ticks. 1547
Axis.get_minorticklabels Return this Axis' minor tick labels, as a list of Text. 1547
Axis.get_minorticklines Return this Axis' minor tick lines as a list of Line2Ds. 1547
Axis.get_minorticklocs Return this Axis' minor tick locations in data coordinates. 1547
Axis.get_offset_text Return the axis offsetText as a Text instance. 1547
Axis.get_tick_padding 1547
✓ Axis.get_ticklabels Get this Axis' tick labels. 1547
✓ Axis.get_ticklines Return this Axis' tick lines as a list of Line2Ds. 1547
✓ Axis.get_ticklocs Return this Axis' tick locations in data coordinates. 1547
✓ Axis.get_gridlines Return this Axis' grid lines as a list of Line2Ds. 1547
✓ Axis.grid Configure the grid lines. 1547
✓✓ Axis.set_tick_params Set appearance parameters for ticks, ticklabels, and
gridlines.
1547
Axis.axis_date Set up axis ticks and labels to treat data along this Axis
as dates.
1547
✓ indicates frequency and importance of usage of the method
86
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
AXIS HELPER METHODS (3/3)
Category Method Description
Page
No.
XAxis Specific
XAxis.axis_name Read-only name identifying the axis. 1559
XAxis.get_text_heights Return how much space should be reserved for text
above and below the axes, as a pair of floats.
1559
XAxis.get_ticks_position Return the ticks position ("top", "bottom", "default", or
"unknown").
1559
✓ XAxis.set_ticks_position Set the ticks position. 1559
✓ XAxis.tick_bottom Move ticks and ticklabels (if present) to the bottom of the
axes.
1559
✓ XAxis.tick_top Move ticks and ticklabels (if present) to the top of the
axes.
1559
YAxis Specific
YAxis.axis_name Read-only name identifying the axis. 1563
YAxis.get_text_widths 1563
YAxis.get_ticks_position Return the ticks position ("left", "right", "default", or
"unknown").
1563
YAxis.set_offset_position 1563
✓ YAxis.set_ticks_position Set the ticks position. 1563
✓ YAxis.tick_left Move ticks and ticklabels (if present) to the left of the
axes.
1563
✓ YAxis.tick_right Move ticks and ticklabels (if present) to the right of the
axes.
1563
Other ✓ Axis.axes The Axes instance the artist resides in, or None. 1563
Use with
Caution*
Axis.set_ticks Set this Axis' tick locations. 1564
Axis.set_ticklabels Set the text values of the tick labels. 1565
✓ indicates frequency and importance of usage of the method
*Warning :This method should only be used after fixing the tick positions using Axis.set_ticks. Otherwise, the labels may end up in
unexpected positions.
87
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
TICK PARAMS METHOD – BULK PROPERTY SETTER
Tick Params to change the appearance of ticks, tick labels, and gridlines.
Call signature : Axes.tick_params(), axis.set_tick_params()
Parameters Description
axis : {'x', 'y', 'both'}, default: 'both' The axis to which the parameters are applied.
which : {'major', 'minor', 'both'},
default: 'major'
The group of ticks to which the parameters are applied.
reset : bool, default: False Whether to reset the ticks to defaults before updating
them.
direction : {'in', 'out', 'inout'} Puts ticks inside the axes, outside the axes, or both.
length : float Tick length in points.
width : float Tick width in points.
color : color Tick color.
pad : float Distance in points between tick and label.
labelsize : float or str Tick label font size in points or as a string (e.g., 'large').
labelcolor : color Tick label color.
colors : color Tick color and label color.
zorder: float Tick and label zorder.
bottom, top, left, right : bool Whether to draw the respective ticks.
labelbottom, labeltop, labelleft,
labelright :bool
Whether to draw the respective tick labels.
labelrotation : float Tick label rotation
grid_color : color Gridline color.
grid_alpha : float Transparency of gridlines: 0 (transparent) to 1 (opaque).
grid_linewidth : float Width of gridlines in points.
grid_linestyle : str Any valid Line2D line style spec.
Enter length = 0 if tick lines are not desired
Matplotlib Release 3.4.2 document, Pg No. 1491, 1504-1506
88
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
A Note on Practical implementation
Matplotlib Release 3.4.2 document, Pg No. 1491, 1504-1506 | Tick_Params | GridLines | Example_Demo |
1. Multiple ways to set the different parameters of ticks, tick labels and grid lines
2. Tick_params is a bulk property setter allowing all the ticks to be modified. Frequent use cases are :
• Direction of the tick either inwards or outwards
• Making tick lengths equal to zero
• Placing Tick labels inside the axes
• Adjusting the padding between the tick and labels
• Setting the label color and label size
3. However, note that Tick_params does not have horizontal alignment or vertical alignment argument.
4. Use Axes.Axis.get_ticklabels() to access tick labels as text instances within a list.
[In]: type(ax1.xaxis.get_ticklabels()[0])
[Out]: matplotlib.text.Text
5. We can Iterate/Loop over all the tick labels with or without conditions and then use plt.setp or
artist.set() method to modify text instance properties.
for labels in ax.yaxis.get_ticklabels():
plt.setp(labels, size = 14, rotation_mode = 'anchor’,
ha = 'right’)
ax.yaxis.set_tick_params(rotation =0, labelsize = 15 )
[ticks.set(alpha = 0.5) for ticks in ax.get_xticklabels()]
[ticks.set(alpha = 0.5) for ticks in ax.get_yticklabels()]
6. Grid lines can be set by using Axes.grid(b = None, which = ‘major’, axis = ‘both’, **kwargs) or
Axes.Axis.grid(b = None, which = ‘major’,**kwargs).
7. Use Axes.set_axisbelow(True) to place gridlines/tick lines behind or below all the artists.
8. Use plt.xticks([ticks, labels]), plt.yticks([ticks, labels]) to place ticks and labels in one call. This is helpful
for simple use-cases.
9. Beyond simple usecases, it is preferable to use Tick Locator/Formatter pairs to customize the tick
positions and the string formatting of the ticks. For general tick locator/formatter, check
matplotlib.ticker module. For datetime tick locator/formatter, check matplotlib.dates module.
Check
Examples
Check
Examples
89
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
TICK
LOCATORS
FORMATTERS
90
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
Locator class Description
NullLocator No ticks
FixedLocator Tick locations are fixed
IndexLocator Locator for index plots (e.g., where x = range(len(y)))
LinearLocator Evenly spaced ticks from min to max
LogLocator Logarithmically ticks from min to max
MultipleLocator Ticks and range are a multiple of base
MaxNLocator Finds up to a max number of ticks at nice locations
AutoLocator (Default.) MaxNLocator with simple defaults.
AutoMinorLocator Locator for minor ticks
SPECIAL NOTE ON TICKERS – LOCATOR CLASS
*Matplotlib Release, 3.4.2, Page No. 2812
*https://jakevdp.github.io/PythonDataScienceHandbook/04.10-customizing-ticks.html
Tick locators define the position of the ticks.
Use tick locator/formatter pairs for controlling tick position and string representation
91
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
SPECIAL NOTE ON TICKERS – LOCATORs - DEMO 1
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
fig, ax = plt.subplots(2,2,figsize= (10,5), sharey = True)
ax1,ax2,ax3,ax4 = ax.flatten() #Unpacking numpy array of axes
[ax.set_ylim(0,20) for ax in fig.axes] #Setting ylim as (0,20)
ax1.xaxis.set_major_locator(ticker.NullLocator())
ax2.xaxis.set_major_locator(ticker.MultipleLocator(2))
ax3.xaxis.set_major_locator(ticker.FixedLocator([0,1,2,8,9]))
ax4.xaxis.set_major_locator(ticker.LinearLocator(5))
92
code
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
SPECIAL NOTE ON TICKERS – LOCATORs - DEMO 2
93
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
SPECIAL NOTE ON TICKERS – FORMATTER CLASS
*Matplotlib Release, 3.4.2, Page No. 2814
*https://jakevdp.github.io/PythonDataScienceHandbook/04.10-customizing-ticks.html
Formatter Class Description
NullFormatter No labels on the ticks
IndexFormatter Set the strings from a list of labels
FixedFormatter Set the strings manually for the labels
FuncFormatter User-defined function sets the labels
FormatStrFormatter Use a format string for each value
ScalarFormatter (Default.) Formatter for scalar values
LogFormatter Default formatter for log axes
Tick formatters define how the numeric value associated with a
tick on an axis is formatted as a string.
Use tick locator/formatter pairs for controlling tick position and string representation
94
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
SPECIAL NOTE ON TICKERS – USING SIMPLE FORMATTING
Code
95
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
SPECIAL NOTE ON TICKERS – USING FORMATTER CLASS
Code
96
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
TICKERS FOR TIME SERIES PLOTTING (1/3)
The dateutil module provides additional code to
handle date ticking, making it easy to place ticks on
any kinds of dates.
dateutil
datetime
The datetime module is python’s built-in module that
supplies classes for manipulating dates and times.
Ø Matplotlib handles dates in its own format.
Ø Helper Methods are available to convert datetime objects and timedelta
objects to matplotlib dates and vice-versa.
powerful extensions to datetime
Python’s built-in module to handle date/ time
date
only year, month
and day
datetime
All components of
time and date
timedelta
An amount of time
with maximum unit
of days
time
only time in hours,
minutes, seconds
and microseconds
Check Link
Four Main Classes
tzinfo
timezone
TZ Awareness
97
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
TICKERS FOR TIME SERIES PLOTTING (2/3)
Helper methods in matplotlib.dates for conversion between datetime objects and
Matplotlib dates
METHOD DESCRIPTION
datestr2num Convert a date string to a datenum using dateutil.parser.parse
date2num Convert datetime objects to Matplotlib dates.
num2date Convert Matplotlib dates to datetime objects.
num2timedelta Convert number of days to a timedelta object.
drange Return a sequence of equally spaced Matplotlib dates.
set_epoch Set the epoch (origin for dates) for datetime calculations.
get_epoch Get the epoch used by dates.
datetime
object
timedelta
object
matplotlib
date
date2num
num2date
Go to
Section
98
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
DATE LOCATORS
MicrosecondLocator Locate microseconds.
SecondLocator Locate seconds.
MinuteLocator Locate minutes.
HourLocator Locate hours.
DayLocator Locate specified days of the month.
WeekdayLocator Locate days of the week, e.g., MO, TU.
MonthLocator Locate months, e.g., 7 for July.
YearLocator Locate years that are multiples of base.
RRuleLocator Locate using a matplotlib.dates.rrulewrapper. rrulewrapper is a
simple wrapper around dateutil's dateutil.rrule which allow
almost arbitrary date tick specifications. See rrule example.
AutoDateLocator On autoscale, this class picks the best DateLocator (e.g.,
RRuleLocator) to set the view limits and the tick locations.
DATE FORMATTERS
AutoDateFormatter attempts to figure out the best format to use. This is most
useful when used with the AutoDateLocator.
ConciseDateFormatter also attempts to figure out the best format to use, and to make
the format as compact as possible while still having complete
date information. This is most useful when used with the
AutoDateLocator.
DateFormatter use strftime format strings.
IndexDateFormatter date plots with implicit x indexing.
TICKERS FOR TIME SERIES PLOTTING (3/3)
Tick locators and formatters from matplotlib.dates module
99
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
CONCISE DATE FORMATTER EXAMPLE
https://matplotlib.org/stable/gallery/ticks_and_spines/date_concise_fo
rmatter.html?highlight=date
import datetime
import matplotlib.pyplot as plt
import matplotlib.dates as mdates # For Date locators and formatters
import numpy as np
base = datetime.datetime(2005, 2, 1)
dates = [base + datetime.timedelta(hours=(2 * i)) for i in range(732)]
N = len(dates)
#Fixing random state for reproducibility
np.random.seed(19680801)
y = np.cumsum(np.random.randn(N))
#Limits for the three subplots/axes
lims = [(np.datetime64('2005-02'), np.datetime64('2005-04')),
(np.datetime64('2005-02-03'), np.datetime64('2005-02-15')),
(np.datetime64('2005-02-03 11:00'), np.datetime64('2005-02-04 13:20'))]
fig, axs = plt.subplots(3, 1, constrained_layout=True, figsize=(6, 6))
for nn, ax in enumerate(axs):
locator = mdates.AutoDateLocator(minticks=3, maxticks=7)
formatter = mdates.ConciseDateFormatter(locator)
ax.xaxis.set_major_locator(locator)
ax.xaxis.set_major_formatter(formatter)
ax.plot(dates, y)
ax.set_xlim(lims[nn])
axs[0].set_title('Concise Date Formatter’)
plt.show()
100
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
OO STYLE ESSENCE
101
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
• In OO style, we want to access the artists first.
• This would allow us to access the attributes and
properties of the artist.
• We can then modify the properties using setter
methods.
• We can also inspect into the attributes, add
primitives into them and modify using appropriate
methods.
102
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
One way to gain access to the primitives is to assign it to a
variable when it is created using Axes helper method.
Instead of :
ax.bar(x,y) #returns rectangular patch objects
we should assign it to a variable as below :
bars = ax.bar(x,y)
Now, bars variable contain the rectangular patch objects that
were returned by the ax.bar() method.
We can use below methods to inspect the children of the
bars variable:
str(bars)
After finding the index location of Barcontainer object, we
will access the rectangular patches :
bars[0].get_children()
ACCESSING ARTISTS
Ø BY ASSIGNING TO A VARIABLE WHEN CREATING
103
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
The Second way is to access the attributes.
In the current example, we will access the patches attribute
of ax object.
ax.bar(x,y)
ax.patches
We can then iterate through the patches in ax.patches.
In a similar manner, we have access to other axes attributes
ax.patch
ax.patches
ax.lines
ax.texts
ax.spines
ax.legend()
ax.collections
ax.containers
ACCESSING ARTISTS
Ø THROUGH ATTRIBUTES (1/2)
104
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
ITERATING OVER THE ARTIST ATTRIBUTES CONTAINERS
bars = [rect for rect in ax.get_children() if
isinstance(rect, mpl.patches.Rectangle)]
[p.set_label(x[i]) for i,p in zip(range(len(x)), ax.patches)]
[p.set_label(x[i]) for i,p in enumerate(ax.patches)]
ACCESSING ARTISTS
Ø THROUGH ATTRIBUTES (2/2)
105
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
USEFUL METHODS AND PYTHON BUILT IN METHODS
FOR INSPECTION
• print(dir(object))
• vars(ax) or ax.__dict__
• str(ax)
• type(object)
• len(object)
• isinstance(obj, class)
• get_text()
• hasattr(), getattr(), setattr()
ARTIST OBJECT METHODS
• matplotlib.artist.getp(artistobject)
• plt.setp(artistobject)
• artist.get_children()
• artist.findobj()
FOR LOOPING/ ITERATION
• enumerate()
• zip()
• flatten()
• ravel()
• list comprehension
• dict.key()
• dict.values()
• list()
• Lambda function
Check
106
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
Axes attribute Description
artists A list of Artist instances
patch Rectangle instance for Axes background
collections A list of Collection instances
images A list of AxesImage
legends A list of Legend instances
lines A list of Line2D instances
patches A list of Patch instances
texts A list of Text instances
xaxis A matplotlib.axis.XAxis instance
yaxis A matplotlib.axis.YAxis instance
Figure attribute Description
axes A list of Axes instances (includes Subplot)
patch The Rectangle background
images A list of FigureImage patches - useful for raw pixel display
legends A list of Figure Legend instances (different
from Axes.legends)
lines A list of Figure Line2D instances (rarely used, see Axes.lines)
patches A list of Figure Patchs (rarely used, see Axes.patches)
texts A list Figure Text instances
Tick attribute Description
tick1line A Line2D instance
tick2line A Line2D instance
gridline A Line2D instance
label1 A Text instance
label2 A Text instance
*Each of these is accessible directly as an attribute of the Tick.
*Matplotlib Release, 3.4.2, Page No. 7-11, 113-120
FIGURE
AXES
TICK
ACCESSING ARTISTS THROUGH ATTRIBUTES
AXIS Use Axis Accessor/Helper Methods to access axis labels, tick lines, tick
labels, tick locators
107
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
AXES
MAPPING THE JOURNEY
FROM AXES INSTANCE TO ARTIST INSTANCE PROPERTIES
ARTISTS
PROPERTIES
ATTRIBUTES
COMMON
PROPERTIES
ARTIST SPECIFIC
PROPERTIES
Use setter and
getter methods
Use dot operator to
access attribute
Change Multiple
properties
• Artist.set()
• plt.setp()
RECTANGLE
PATCH
LINE2D
CLASS INSTANCES
ATTRIBUTES
PROPERTIES
LABEL GUIDE
Attributes are list of
Artists objects.
TEXT
108
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists through attributes.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend and Set Title to the Axes
7
PROPERTIES COMMON TO ALL ARTISTS (1/2)
PROPERTY DESCRIPTION
alpha The transparency - a scalar from 0-1
animated A boolean that is used to facilitate animated drawing
axes The Axes that the Artist lives in, possibly None
clip_box The bounding box that clips the Artist
clip_on Whether clipping is enabled
clip_path The path the artist is clipped to
contains A picking function to test whether the artist contains the
pick point
figure The figure instance the artist lives in, possibly None
label A text label (e.g., for auto-labeling)
picker A python object that controls object picking
transform The transformation
visible A boolean whether the artist should be drawn
zorder A number which determines the drawing order
rasterized Boolean; Turns vectors into raster graphics (for
compression & EPS transparency)
*Matplotlib Release, 3.4.2, Page No. 113-120
Ø “Each of the properties is accessed with setter or getter“
Ø (The exception being axes property which can be accessed by
dot operator)
ax.patch.get_alpha( )
ax.patch.set_alpha( ax.patch.get_apha()*1.2 )
ax.patch.set_alpha( 0.5 )
109
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists through attributes.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend and Set Title to the Axes
7
PROPERTIES COMMON TO ALL ARTISTS (2/2)
USE CASE SCENARIO ARTIST PROPERTY
For User-defined function figure, axes in conjunction with plt.gcf(),
plt.gca(), plt.scf(), plt.sca()
For Adding artist to Legend label
Setting ticks, axis, spines or
even axes(for multiple
subplots) invisible
visible
If multiple artists overlap such
as axhspan, axvspan,
fillbetween
zorder
If text, patch, arrows,
annotation etc. are to be
added either in data
coordinates or in figure
coordinates
transform : ax.transData | ax.transAxes |
fig.transfigure | fig. dpi_scale_trans
where ax is an axes instance, fig is a figure
instance. Refer to official docs for full list.
110
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
• Use setp function of Pyplot Interface
• Use Artist.set() method to set properties of this Axes object.
SETTING ARTIST PROPERTIES
# Preparing the Data
group_names = ['Nexus', 'Chiffron', 'Pendos', 'Forresters']
group_data = [105000, 125000, 70500, 90000]
plt.rcdefaults()
fig, ax = plt.subplots(figsize=(8, 4)) #Instantiating fig and axes
ax.barh(group_names, group_data, height = 0.8)
# Accessing the artist using axes accessor method
labels = ax.get_xticklabels()
# Setting the artist property using plt.setp method
plt.setp(labels, rotation=15, ha = 'right', fontsize = 10)
# Setting the artist property using artist.set() method
ax.set(xlim=[-10000, 140000],
xlabel='Total Revenue', ylabel='Company',
title='Company Revenue')
# Setting the artist property using artist.set_p() method
[bar.set_facecolor('xkcd:water blue') for bar in ax.patches]
fig.savefig("Setting property.png",dpi=300, format='png', bbox_inches='tight')
111
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
• matplotlib.artist.getp(artistobject)
• plt.setp(artistobject)
INSPECTING ARTIST PROPERTIES
import matplotlib.pyplot as plt
lines = plt.plot([1, 2, 3])
plt.setp(lines) # list of settable line properties
marker: marker style string, `~.path.Path` or `~.markers.MarkerStyle`
markeredgecolor or mec: color
markeredgewidth or mew: float
markerfacecolor or mfc: color
markerfacecoloralt or mfcalt: color
markersize or ms: float
markevery: None or int or (int, int) or slice or List[int] or float or (float, float) or List[bool]
path_effects: `.AbstractPathEffect`
picker: unknown pickradius: float
rasterized: bool or None
sketch_params: (scale: float, length: float, randomness: float)
snap: bool or None
solid_capstyle: {'butt', 'round', 'projecting’}
solid_joinstyle: {'miter', 'round', 'bevel’}
transform: `matplotlib.transforms.Transform`
url: str
visible: bool
xdata: 1D array
ydata: 1D array
zorder: float
agg_filter: a filter function, which takes a (m, n, 3) float array and a
dpi value, and returns a (m, n, 3) array
alpha: float or None
animated: bool
antialiased or aa: bool
clip_box: `.Bbox`
clip_on: bool
clip_path: Patch or (Path, Transform) or None
color or c: color
contains: unknown
dash_capstyle: {'butt', 'round', 'projecting’}
dash_joinstyle: {'miter', 'round', 'bevel’}
dashes: sequence of floats (on/off ink in points) or (None, None)
data: (2, N) array or two 1D arrays
drawstyle or ds: {'default', 'steps', 'steps-pre', 'steps-mid', 'steps-
post'}, default: 'default’
figure: `.Figure`
fillstyle: {'full', 'left', 'right', 'bottom', 'top', 'none’}
gid: str
in_layout: bool
label: object linestyle or ls: {'-', '--', '-.', ':', '', (offset, on-off-seq),
...}
linewidth or lw: float
#matplotlib.artist.getp also returns a listing of properties for a given object.
import matplotlib.pyplot as plt
import matplotlib.artist as mpa
lines = plt.plot([1, 2, 3])
mpa.getp(lines)
*Matplotlib, Release 3.4.2, Pg No. 33,76,111
112
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
PROPERTY DESCRIPTION
agg_filter a filter function, which takes a (m, n, 3) float array and a dpi
value, and returns a (m, n, 3) array
antialiased or aa [True | False]
color or c any matplotlib color
dash_capstyle [‘butt’ | ‘round’ | ‘projecting’]
dash_joinstyle [‘miter’ | ‘round’ | ‘bevel’]
dashes sequence of on/off ink in points
drawstyle [‘default’ | ‘steps’ | ‘steps-pre’ | ‘steps-mid’ | ‘steps-post’]
fillstyle [‘full’ | ‘left’ | ‘right’ | ‘bottom’ | ‘top’ | ‘none’]
gid an id string
linestyle or ls [‘solid’ | ‘dashed’, ‘dashdot’, ‘dotted’ | (offset, on-off-dash-
seq) | '-' | '--' | '-.' | ':' | 'None' | ' ' | '']
linewidth or lw float value in points
marker A valid marker style
markeredgecolor or mec any matplotlib color
markeredgewidth or mew float value in points
markerfacecolor or mfc any matplotlib color
markerfacecoloralt or
mfcalt
any matplotlib color
markersize or ms float
markevery [None | int | length-2 tuple of int | slice | list/array of int |
float | length-2 tuple of float]
path_effects AbstractPathEffect
pickradius float distance in points
solid_capstyle [‘butt’ | ‘round’ | ‘projecting’]
solid_joinstyle [‘miter’ | ‘round’ | ‘bevel’]
url a url string
xdata 1D array
ydata 1D array
*Matplotlib Release, 3.4.2, Page No. 2260-61
ADDITIONAL PROPERTIES SPECIFIC TO LINE2D
113
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
PROPERTY DESCRIPTION
agg_filter a filter function, which takes a (m, n, 3) float array and a dpi value,
and returns a (m, n, 3) array
backgroundcolor color
bbox dict with properties for patches.FancyBboxPatch
color or c color
fontfamily or family {FONTNAME, 'serif', 'sans-serif', 'cursive', 'fantasy', 'monospace'}
fontproperties or font or
font_properties
font_manager.FontProperties or str or pathlib.Path
fontsize or size float or {'xx-small', 'x-small', 'small', 'medium', 'large', 'x-large', 'xx-
large'}
fontstretch or stretch horizontal condensation or expansion
{a numeric value in range 0-1000, 'ultra-condensed', 'extra-
condensed', 'condensed', 'semi-condensed', 'normal', 'semi-
expanded', 'expanded', 'extra-expanded', 'ultra-expanded'}
fontstyle or style {'normal', 'italic', 'oblique'}
fontvariant or variant {'normal', 'small-caps'}
fontweight or weight {a numeric value in range 0-1000, 'ultralight', 'light', 'normal',
'regular', 'book', 'medium', 'roman', 'semibold', 'demibold', 'demi',
'bold', 'heavy', 'extra bold', 'black'}
gid str
*Matplotlib Release, 3.4.2, Page No. 2795-2796, 326-329
ADDITIONAL PROPERTIES SPECIFIC TO TEXT (1/2)
114
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
PROPERTY DESCRIPTION
horizontalalignment or ha {'center', 'right', 'left'}
in_layout bool
linespacing float (multiple of font size)
math_fontfamily str
multialignment or ma {'left', 'right', 'center'}
path_effects AbstractPathEffect
position (float, float)
rotation float or {'vertical', 'horizontal'}
rotation_mode {None, 'default', 'anchor'}
text object
transform_rotates_text bool
url str
usetex bool or None
verticalalignment or va {'center', 'top', 'bottom', 'baseline', 'center_baseline'}
visible bool
wrap bool
x float
y float
zorder float
url str
usetex bool or None
*Matplotlib Release, 3.4.2, Page No. 2795-2796
ADDITIONAL PROPERTIES SPECIFIC TO TEXT (2/2)
115
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
TEXT INSTANCE – HORIZONTAL ALIGNMENT DEMO
Code
116
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
SPECIAL LOOK INTO PATCH RECTANGLE
(x,y)
width
height
(x,y) [(float, float)] The anchor point
width [float] Rectangle width
height [float] Rectangle height
angle [float, default: 0] Rotation in degrees anti-
clockwise about xy
*Matplotlib Release, 3.4.2, Page No. 2409
Note: the float values are in the coordinate system for the data,
controlled by xlim and ylim.
RECTANGLE PARAMETERS
117
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
PROPERTY DESCRIPTION
agg_filter a filter function, which takes a (m, n, 3) float array and a dpi
value, and returns a (m, n, 3) array
antialiased or aa unknown
capstyle CapStyle or {'butt', 'projecting', 'round'}
color color
edgecolor or ec color or None or 'auto'
facecolor or fc color or None
fill bool
gid str
hatch {'/', '', '|', '-', '+', 'x', 'o', 'O', '.', '*'}
in_layout bool
joinstyle JoinStyle or {'miter', 'round', 'bevel'}
linestyle or ls {'-', '--', '-.', ':', '', (offset, on-off-seq), ...}
linewidth or lw float or None
path_effects AbstractPathEffect
sketch_params (scale: float, length: float, randomness: float)
snap bool or None
url str
ADDITIONAL PROPERTIES PATCH RECTANGLE SPECIFIC
118
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
USE CASES – MODIFYING THE RECTANGLE PATCH PROPERTIES
Patch instance Use Case Scenario
fig.patch Changing color of figure background
ax.patch Changing color of axes background
ax.patches Adding data labels on top of the bars in a bar plot
ax.patches Assigning label values to each of the patches of bar plot
for adding to legend
import matplotlib.pyplot as plt
import numpy as np
fig, (ax1, ax2) = plt.subplots(1,2, sharey = True)
x = np.random.randint(1,20,20)
#Set the face color of figure background
rect = fig.patch
rect.set(facecolor = 'aliceblue’)
#Set the face color of the axes backgrounds
ax1.patch.set(facecolor = 'salmon')
ax2.patch.set(facecolor = 'aquamarine')
ax2.plot(x)
plt.show()
119
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
METHODS OF RECTANGLE PATCH
Rectangle Patch Method Description
__str__(self ) Return str(self).
get_bbox(self ) Return the Bbox.
get_height(self ) Return the height of the rectangle.
get_patch_transform(self ) Return the Transform instance mapping patch
coordinates to data coordinates.
get_path(self ) Return the vertices of the rectangle.
get_width(self ) Return the width of the rectangle.
get_x(self ) Return the left coordinate of the rectangle.
get_xy(self ) Return the left and bottom coords of the
rectangle as a tuple.
get_y(self ) Return the bottom coordinate of the rectangle.
set_bounds(self, *args) Set the bounds of the rectangle as left, bottom,
width, height. The values may be passed as
separate parameters or as a tuple:
set_height(self, h) Set the height of the rectangle.
set_width(self, w) Set the width of the rectangle.
set_x(self, x) Set the left coordinate of the rectangle.
*Matplotlib Release, 3.4.2, Page No. 2409
USE CASE SCENARIO LABEL PROPERTY
For Adding data labels on top of
the bars in bar plot
get_x(), get_y(), get_height(), get_width()
For User defined function axes property, figure property in user defined
function to retrieve the axes and figure
respectively.
120
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.spines as spines
colors = [ plt.cm.Dark2(x) for x in np.linspace(0, 1,
len(x))]
plt.rcdefaults()
%matplotlib inline
fig, ax = plt.subplots(figsize = (10,6), frameon = True)
bars = ax.bar(x,y, color = colors)
StrTitle = 'Consumption of Packed beverages in
billion liters,n by Beverage Type, Global, 2019'
ax.set_title(StrTitle, size = 16)
Add_data_labels(ax.patches)
stylize_axes(ax)
fig.savefig('Patches.png', dpi=300, format='png',
bbox_inches='tight')
data = {'Water':465,
'Alchoholic Beverages':271,
'Carbonated soft drinks': 224,
'Milk and Dairy products': 260,
'New Drinks': 75,
'Fruits and Vegetable Juices': 86}
x = list(data.keys())
y = list(data.values())
DEMO EXAMPLE – APPLYING LEARNINGS TILL NOW
PREPARING SAMPLE DATA CODE SCRIPT PLOT OUTPUT
• User defined function to
stylize the axes.
• Uses Axes helper
methods to access and
modify artist properties
• User defined function to
add data labels
• Uses Axes helper
methods to access the
individual bars and use
its properties to add
texts
Check slide
121
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
def stylize_axes(ax):
#Making the axes background light gray
ax.set_facecolor('.85’)
# Setting the y limits
ax.set_ylim(0,ax.get_ylim()[1]*1.1)
#Making the tick lines disppear
ax.tick_params(length=0)
#Making the xtick labels rotated
for labels in ax.get_xticklabels() :
labels.set(size = 13,rotation = 15,
rotation_mode = 'anchor’,
ha = 'right’)
#Setting grid lines to white color
ax.grid(True, axis='x', color='white’)
#Setting grid lines below all the artists
ax.set_axisbelow(True)
#Making all the spines invisible
[spine.set_visible(False) for spine in ax.spines.values()]
CREATING USER DEFINED FUNCTION
TO STYLIZE AXES
# Adding Annotations to place data labels
def Add_data_labels(rect): #rect is ax.patches object
for p in rect:
#Retrieving the Axes container of patch object
ax = rect[0].axes
#Adding text iteratively on top of each bar
#Using get and set on x,y parameters of rectangle patch
ax.text( x = p.get_x()+p.get_width()/2,
y = ax.get_ylim()[1]*0.01+ p.get_height(),
s = p.get_height(),
ha = 'center', size = 12)
CREATING USER DEFINED FUNCTION TO ADD DATA LABELS IN BAR
PLOTS
122
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
# Doing the necessary imports
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.spines as spines
plt.rcdefaults()
%matplotlib inline
#create a list of rgba colors from a matplotlib.cm colormap
colors = [ plt.cm.Dark2(x) for x in np.linspace(0, 1, len(x))]
#Instantiating figure, axes
fig, ax = plt.subplots(figsize = (10,6), frameon = True)
#Creating a bar plot by ax.bar method
# Note the colors passed as the color argument
bars = ax.bar(x,y, color = colors)
#Setting the title of plot
StrTitle = 'Consumption of Packed beverages in billion liters,n by
Beverage Type, Global, 2019'
ax.set_title(StrTitle, size = 16)
#Using the user defined functions – to add data labels and stylize the axes
Add_data_labels(ax.patches)
stylize_axes(ax)
fig.savefig('Patches.png', dpi=300, format='png', bbox_inches='tight')
DEMO - USERDEFINED FUNCTIONS TO STYLIZE PLOTS AND ADD LABELS
123
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
TRANSFORMATIONS
124
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
Coordinates Transformation object Description
"data" ax.transData The coordinate system for the data, controlled by xlim and ylim.
"axes" ax.transAxes The coordinate system of the Axes; (0, 0) is bottom left of the axes,
and (1, 1) is top right of the axes.
"subfigure" subfigure.transSubfigure The coordinate system of the SubFigure; (0, 0) is bottom left of the
subfigure, and (1, 1) is top right of the subfigure. If a figure has no
subfigures, this is the same as transFigure.
"figure" fig.transFigure The coordinate system of the Figure; (0, 0) is bottom left of the figure,
and (1, 1) is top right of the figure.
"figure-
inches"
fig.dpi_scale_trans The coordinate system of the Figure in inches; (0, 0) is bottom left of
the figure, and (width, height) is the top right of the figure in inches.
"display" None,
or IdentityTransform()
The pixel coordinate system of the display window; (0, 0) is bottom
left of the window, and (width, height) is top right of the display
window in pixels.
"xaxis",
"yaxis"
ax.get_xaxis_transform(),
ax.get_yaxis_transform()
Blended coordinate systems; use data coordinates on one of the axis
and axes coordinates on the other.
*Matplotlib Release, 3.4.2, Page No. 227
*https://aosabook.org/en/matplotlib.html
TRANSFORMATION FRAMEWORK
Use Transformation Objects to easily move between coordinate systems
125
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
TRANSFORMATION FRAMEWORK – THE COORDINATE SYSTEMS
The commonly used coordinate systems are :
• Data
• Axes
• Figure
All the above will be converted to Display coordinates by appropriate transformation
objects.
Code
126
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
Userland data
coordinate system
axes coordinate
System
figure coordinate
System
display coordinate
System
ax.transData
ax.transAxes
fig.transFigure
figure inches System
fig.dpi_scale_trans
display coordinate
System
Ø All of the transformation objects in the table above take inputs in their coordinate
system and transform the input to the display coordinate system.
Ø transform is the property common to all artists.
Ø While it works under the hood not requiring to be explicitly mentioned, but for
customization and more control, it is immensely powerful.
Ø Use case :
Ø Adding annotations/text
Ø Adding horizontal/vertical spans
Ø Legend positioning
TRANSFORMATION FRAMEWORK – TRANSFORMATION OBJECTS
Transformation Objects to easily move between coordinate systems
127
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
BLENDED TRANSFORMATIONS - EXAMPLE
x to be in data
coordinates
y to span from 0 -1 in
axes coords.
import numpy as np
import matplotlib.transforms as transforms
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
fig, ax = plt.subplots(figsize = (6,5))
x = np.random.randn(1000)
ax.hist(x, 30)
ax.set_title(r'$sigma=1 / dots / sigma=2$', fontsize=16)
# the x coords of this transformation are data, and the y
coord are axes
trans = transforms.blended_transform_factory( ax.transData,
ax.transAxes)
# highlight the 1..2 std dev region with a span.
# x to be in data coordinates
# y to span from 0..1 in axes coords.
rect = mpatches.Rectangle((1, 0), width=1, height=1,
transform=trans,
color='yellow', alpha=0.5)
ax.add_patch(rect)
ax.text(0.5,0.9, s = "Blended Coordinates Transformation -
Demo", transform = fig.transFigure,
ha = 'center', fontsize = 15,
fontweight = 'medium’)
fig.patch.set(fc = 'salmon', alpha = 0.1)
plt.tight_layout(rect = [0,0,1,0.9])
https://matplotlib.org/stable/tutorials/advanced/transforms_tutorial.html
128
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
COLORS
129
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
Format Example
1.
RGB or RGBA (red, green, blue, alpha) tuple of float values in a
closed interval [0, 1].
• (0.1, 0.2, 0.5)
• (0.1, 0.2, 0.5, 0.3)
2. Case-insensitive hex RGB or RGBA string.
• '#0f0f0f'
• '#0f0f0f80'
3.
Case-insensitive RGB or RGBA string equivalent hex shorthand of
duplicated characters.
• '#abc' as '#aabbcc'
• '#fb1' as '#ffbb11'
4.
String representation of float value in closed interval [0, 1] for black
and white, respectively.
• '0.8' as light gray
• '0' as black
• '1' as white
5.
Single character shorthand notation for shades of colors.
Note :
The colors green, cyan, magenta, and yellow do not coincide with
X11/CSS4 colors.
• 'b' as blue
• 'g' as green
• 'r' as red
• 'c' as cyan
• 'm' as magenta
• 'y' as yellow
• 'k' as black
• 'w' as white
6. Case-insensitive X11/CSS4 color name with no spaces.
• 'aquamarine'
• 'mediumseagreen'
7.
Case-insensitive color name from xkcd color
survey with 'xkcd:'prefix.
• 'xkcd:sky blue'
• 'xkcd:eggshell'
Specifying Colors – Acceptable Formats
130
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
Format Example
8. Case-insensitive Tableau Colors from 'T10' categorical palette.
Note : This is the default color cycle.
'tab:blue'
'tab:orange'
'tab:green'
'tab:red'
'tab:purple'
'tab:brown'
'tab:pink'
'tab:gray'
'tab:olive'
'tab:cyan'
9. "CN" color spec where 'C' precedes a number acting as an index into the
default property cycle.
Note : Matplotlib indexes color at draw time and defaults to black if cycle
does not include color.
The default property cycle is
rcParams["axes.prop_cycle"] (default: cycler('color',['#1f77b4', '#ff7f0e', '#2ca
02c', '#d62728', '#9467bd','#8c564b', '#e377c2', '#7f7f7f', '#bcbd22', '#17becf
']))
'C0'
'C1'
Ø The alpha for an Artist controls opacity. It indicates how the RGB color
of the new Artist combines with RGB colors already on the Axes. zorder
of the artist also becomes important.
131
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
Ø Basic (8 colors)
Ø Tableau Palette (10 colors)
Ø CSS colors (140+ colors)
Ø xkcd colors (954 colors)
https://colorhunt.co/palette/
Exploring the Color Charts
Handy go to reference to
pick colors
USE CASE SCENARIO ARTIST PROPERTY
Assign face color, edge color of figure, axes edgecolor (ec) , facecolor (fc)
Assign color to texts, axis labels, tick labels color or c
Assign color to marker edge and face in line plot Markeredgecolor (mec),
markerfacecolor(mfc)
Initialize the color in bar plots, pie charts. Use
Tableau palette. If more number of categories
present, then colormaps and external libraries like
[palettable] and [colorcet].
edgecolor (ec) , facecolor (fc)
Setting the property cycle of the axes
(Note : The property cycle controls the style
properties such as color, marker and linestyle of
future plot commands. The style properties of data
already added to the Axes are not modified.)
ax.set_prop_cycle(color=['red',
'green', 'blue'])
132
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
133
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
Axis accessor method Description
get_scale The scale of the Axis, e.g., 'log' or 'linear'
get_view_interval The interval instance of the Axis view limits
get_data_interval The interval instance of the Axis data limits
get_gridlines A list of grid lines for the Axis
get_label The Axis label - a Text instance
get_offset_text The Axis offset text - a Text instance
get_ticklabels A list of Text instances - keyword minor=True|False
get_ticklines A list of Line2D instances - keyword minor=True|False
get_ticklocs A list of Tick locations - keyword minor=True|False
get_major_locator The ticker.Locator instance for major ticks
get_major_formatter The ticker.Formatter instance for major ticks
get_minor_locator The ticker.Locator instance for minor ticks
get_minor_formatter The ticker.Formatter instance for minor ticks
get_major_ticks A list of Tick instances for major ticks
get_minor_ticks A list of Tick instances for minor ticks
grid Turn the grid on or off for the major or minor ticks
134
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
http://cng.seas.rochester.edu/CNG/docs/x11color.html
135
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
http://cng.seas.rochester.edu/CNG/docs/x11color.html
136
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
137
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
138
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
139
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
140
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
141
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
Code
142
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Additional
Colormaps
External libraries like [palettable] and [colorcet]
Use Case Scenarios
For categorical bar plots and pie charts, use qualitative colormaps such as
tab10 (10 colors) , tab20, tab20b, tab20c (20 colors), Paired (12 colors)
Accessing a Colour
map using
plt.cm.colormapname(np.linspace(0,1, number of colors))
colors attribute of listed colormap followed by indexing if required
Classes of
colormaps
Sequential, Diverging, Cyclic ,Qualitative
What are color
maps ?
Built-in colormaps accessible via matplotlib.cm.get_cmap to map data on to
color values.
Note : Colormaps is a very rich immersive topic. Check out the official matplotlib website to dwelve deeper.
143
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Note : Colormaps is a very rich immersive topic. Check out the official matplotlib website to dwelve deeper.
Classes of colormaps Based on Function
Colormap Category Use Case
Sequential: change in lightness and often saturation of
color incrementally, often using a single hue;
for representing information that has ordering.
Diverging: change in lightness and possibly saturation
of two different colors that meet in the middle at an
unsaturated color
when the information being plotted has a critical
middle value, such as topography or when the
data deviates around zero.
Cyclic: change in lightness of two different colors that
meet in the middle and beginning/end at an
unsaturated color;
for values that wrap around at the endpoints,
such as phase angle, wind direction, or time of
day.
Qualitative: often are miscellaneous colors to represent information which does not have
ordering or relationships.
https://matplotlib.org/stable/tutorials/colors/colormaps.html
https://matplotlib.org/stable/api/pyplot_summary.html?highlight=pyplot
144
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Range of Matplotlib
Colormaps
145
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Note : All colormaps can be reversed by appending _r.
146
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Note : All colormaps can be reversed by appending _r.
147
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Lightness of Matplotlib
Colormaps
148
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
149
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
150
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
151
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm #matplotlib.colors module
from matplotlib.colors import ListedColormap, LinearSegmentedColormap
#Get 8 colours from viridis colormap
viridis = cm.get_cmap('viridis', 8)
OR
plt.cm.viridis(np.linspace(0, 1, 8))
# Get first 4# colors (0th – 3rd index) from the colormaps
# Using range function to get colour from particular indices
viridis(range(4))
OR
viridis(np.linspace(0, 0.5, 4))
# Get 4# colors (4th-7th index) from the colormaps
viridis(range(4,8))
OR
viridis(np.linspace(0.5, 1, 4))
plt.cm.viridis(np.linspace(start = 0, stop = 1, num = 8 ))
0 1
ACCESSING COLORS FROM A MATPLOTLIB
COLORMAP
COLORMAP is a 1D Array of colours.
matplotlib.colors module provides functions and classes for color
specification conversions, and mapping data/numbers onto colours on a
colormap.
0.14 0.56
0.42
0.28 0.70 0.85
0.00 1
*Colormap instances are used to convert data values (floats) from the interval [0, 1] to
the RGBA color that the respective Colormap represents. For scaling of data into
the [0, 1] interval see matplotlib.colors.Normalize.
152
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
# Necessary imports – Especially ListedColormap Class
[1] : import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.colors import ListedColormap
from matplotlib.colors import LinearSegmentedColormap
# Note : cm.get_cmap() returns a colormap
[2] : type(cm.get_cmap('YlGn', 256))
[2] : matplotlib.colors.LinearSegmentedColormap
[3] : type(cm.get_cmap(‘viridis’, 20))
[3] : matplotlib.colors.ListedColormap
[4] : type(plt.cm.viridis(np.linspace(0, 1, 8))) #returns numpy array
[4] : numpy.ndarray
# Creating a Listed Colormap
[5] : YlGn_New = cm.get_cmap('YlGn', 256)
[6] : ListedColormap(YlGn_New(np.linspace(0,1,10)))
[6] : <matplotlib.colors.ListedColormap at 0x7ffdd4625700>
[7] : viridis_short = cm.get_cmap(‘viridis’,10)
ListedColormap(viridis_short(np.linspace(0,1,10)))
[8] : <matplotlib.colors.ListedColormap at 0x7ffdd4626700>
CREATING LISTED COLORMAP FROM
EXISTING MATPLOTLIB COLORMAPs
Linear Segmented
ColorMaps
Listed ColorMaps
• Perceptually uniform
sequential
• Qualitative
• Sequential
• Diverging
• Cyclic
USE CASE SCENARIO PARAMETER/PROPERTY
Setting cmap in scatter charts cmap kwd argument
Initialize the color in bar plots, pie
charts with colors of listed
colormap. Check out tab10,
tab20. Also, External libraries like
[palettable] and [colorcet].
facecolor property can take a
single color or list of colors
#colors = plt.cm.tab10.colors[:8]
#colors = plt.cm.Paired.colors
Setting the property cycle of the
axes
ax.set_prop_cycle(color=plt.cm.ta
b10.colors)
Colormap object generated from a list of colors.
Ø most useful when indexing directly into a colormap
Ø used to generate special colormaps for ordinary
mapping.
153
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
from matplotlib.colors import ListedColormap
colarray = plt.cm.plasma(range(256))
newcmap = ListedColormap(colarray)
#Colormap can be called with an integer array, or with a float array
between 0 and 1.
newcmap.colors #colors attribute of a listed colormap
# Above returns colors in RGBA tuples of float [0,1]. Shape (NX4 Array)
newcmap(range(100,150)) #Returns colors from index 100 to 150
newcmap(np.linspace(0,1,20)) #Returns 20 colors corresponding to
equally spaced positions in between the specified start and end
newcmap([0.0, 0.1, 0.4, 0.8]) #Returns colors from specified locations
considering the first color in newcmap is considered 0 and last colour
is 1.
#colors = [ plt.cm.tab20c(x) for x in np.linspace(0, 1, len(dic.values()) ]
cmap = ListedColormap(["darkorange", "gold", "lawngreen",
"lightseagreen"])
CODE CHUNK TO CREATE LISTED COLORMAPS AND ACCESSING COLORS
154
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
# Doing the necessary imports
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.colors import ListedColormap
plt.rcdefaults()
%matplotlib inline
#
colarray = plt.cm.plasma(range(256))
newcmp = ListedColormap(colarray)
#Creating the 1X256 Numpy Array (Matrix in Matlab) of float values from 0..1
Z = np.linspace(0,1,256)[np.newaxis,:]
fig, (ax0, ax1) = plt.subplots(2, 1, figsize = (10,3))
#Creating a pseudocolor plot without distinguishable edge between values
c = ax0.pcolor(Z,cmap = newcmp)
ax0.set_title('default: no edges', fontsize = 20)
#Creating a pseudocolor plot with distinguishable edge between the values
c = ax1.pcolor(Z, cmap = newcmp, edgecolors='k', linewidths=1)
ax1.set_title('thick edges',fontsize = 20)
fig.tight_layout()
fig.savefig('Plasma_256_colours.png',dpi=300, format='png’,
bbox_inches='tight')
#colors = plt.cm.Dark2(range(15))
#colors = plt.cm.tab20(range(15))
#colors = plt.cm.nipy_spectral(np.linspace(0,0.9,len(dic.values())))
#colors = plt.cm.CMRmap(np.linspace(0.2,1,len(dic.values())))
#colors = [ plt.cm.viridis(x) for x in np.linspace(0, 1, len(dic.values()))]
#colors = [ plt.cm.Set3(x) for x in np.linspace(0, 1, len(dic.values()))]
.colors attribute for listed colormaps
#colors = plt.cm.tab10.colors
#colors = plt.cm.Paired.colors
COLORS FROM EXTERNAL LIBRARY PALETTABLE
from palettable.cartocolors.qualitative import Bold_10
#colors = Bold_10.mpl_colors[:5]
colors = Bold_10.mpl_colors
Ø Refer to below Code from Matplotlib Gallery. Note slight tweaking was done to
change the shape of the rectangular grid by using np.newaxis
Ø https://matplotlib.org/stable/gallery/images_contours_and_fields/pcolor_demo.html
#sphx-glr-gallery-images-contours-and-fields-pcolor-demo-py
CODE SCRIPT TO CREATE PSEUDO COLOR PLOT
READY TO USE CODE TO ACCESS COLORS FROM CMAPS
155
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
156
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
CONTROLLING THE LEGEND ENTRIES
Matplotlib, Release 3.4.2, Page No. 2567, 2245, 1469
https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.legend.html?highlight=legend#matplotlib.axes.Axes.legend
https://matplotlib.org/stable/tutorials/intermediate/legend_guide.html, Legend Demo
CONTROLLING THE LEGEND LOCATION, PLACEMENT
DEFINING CUSTOM LEGEND HANDLERS
• The legend handler map specifies how to create legend
handles from artists (lines, patches, etc.) in the axes or figures.
• Custom legend handlers can be defined to support arbitrary
objects not covered by default legend handlers.
CREATING PROXY ARTISTS SPECIFICALLY FOR THE LEGEND
• loc
• bbox_to_anchor
• bbox_transform
• legend entry
• legend handles
• legend labels
• legend keys
• m.patches
• m.lines
• Not all handles can be turned into legend entries automatically,
so it is often necessary to create an artist which can.
• Legend handles don't have to exist on the Figure or Axes in
order to be used.
• Next up is specifying the location of the legend
• Two parameters to control this :
• loc
• bbox_to_anchor
h,l = ax.get_legend_handles_labels()
• The legend module defines the Legend class responsible for
drawing legends associated with axes and/or figures.
• The Legend class is a container of legend handles and legend
texts.
1
2
3
4
ASPECTS OF LEGEND
157
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
handles [list of Artist]
A list of Artists (lines, patches) to be added to the legend.
labels [list of str]
A list of labels to show next to the artists. The length of
handles and labels should be the same. If they are not,
they are truncated to the smaller of both lengths.
Read more on legend entry, key, label and handle
Adding Legend to Axes
Call signatures:
• legend()
• legend(labels)
• legend(handles, labels)
ax.legend() Automatic detection of elements
to be shown in the legend
• Suffices for most use-cases
• Requires explicitly labeling the artist either at
the time of creation or before calling legend
• Artist whose label either starts with
underscore or does not have label property
set will be ignored from the Legend
ax.legend(labels) Labeling existing plot elements This method is discouraged owing to an
implicit ordering of labels and artists.
ax.legend(handles,
labels)
Explicitly defining the elements
in the legend
• Gives Total Control over the artists displayed
in the legend
• This method is explicit and will accept the
handles and labels at face value.
• This implies the labels will override the label
names assigned at the time of creation.
• Also, one can pass additional artists or
subset of artist to the handles list.
Matplotlib, Release 3.4.2, Page No. 2567, 2245, 1469
https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.legend.html?highlight=legend#matplotlib.axes.Axes.legend
https://matplotlib.org/stable/tutorials/intermediate/legend_guide.html, Legend Demo
158
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
legend entry A legend is made up of one or more legend
entries. An entry is made up of exactly one key
and one label.
legend key The colored/patterned marker to the left of
each legend label.
legend label The text which describes the handle
represented by the key.
legend handle The original object which is used to generate an
appropriate entry in the legend.
Important Terminologies
Matplotlib, Release 3.4.2, Page No. 2567, 2245, 1469
https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.legend.html?highlight=legend#matplotlib.axes.Axes.legend
https://matplotlib.org/stable/tutorials/intermediate/legend_guide.html, Legend Demo
159
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
plt.rcdefaults()
np.random.seed(450)
x = ['A', 'B','C','D']
y = np.random.randint(100,400,4)
fig, ax = plt.subplots()
p = ax.bar(x,y, color = plt.cm.Set2.colors)
#Setting labels to each of the bars
[bar.set(label = text) for text,bar in zip(x,p)]
print(ax.get_legend_handles_labels())
#Calling the legend function for automatic
detection of elements with labels to be shown
ax.legend()
Automatic detection of elements to be shown in the legend
Artists with valid labels are automatically detected.
To check the list of handles detected, use ax.get_legend_handles_labels()
Matplotlib, Release 3.4.2, Page No. 2567, 2245, 1469
https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.legend.html?highlight=legend#matplotlib.axes.Axes.legend
https://matplotlib.org/stable/tutorials/intermediate/legend_guide.html, Legend Demo
160
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
Explicitly defining the elements in the legend
For full control of which artists have a legend entry, it is possible to pass an
iterable of legend artists followed by an iterable of legend labels
respectively:
import matplotlib.pyplot as plt
import numpy as np
plt.rcdefaults()
np.random.seed(450)
x = ['A', 'B','C','D']
y = np.random.randint(100,400,4)
fig, ax = plt.subplots()
p = ax.bar(x,y, color = plt.cm.Set2.colors)
#Setting labels to each of the bars
[bar.set(label = text) for text,bar in zip(x,p)]
handles = []
for bar in p:
if bar.get_label() not in [‘A',’D']:
handles.append(bar)
print(handles) #Check the handles list
ax.legend(handles = handles)
fig.savefig('Explicitly_Passing_Iterable of 
handles.png',dpi=300, format='png',
bbox_inches='tight')
Context :
EXCLUDE FROM LEGEND
ARTISTs WITH LABELS ’A’ or ‘D’
Note : Its okay to omit labels from the call
signature if all the handles have valid labels.
Infact, either give the correct desired labels in this
call corresponding to all the handles or omit
labels altogether.
Labels Omitted
161
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
For full control of which artists have a legend entry, it is possible to pass an
iterable of legend artists followed by an iterable of legend labels
respectively:
import matplotlib.pyplot as plt
import numpy as np
plt.rcdefaults()
np.random.seed(450)
x = ['A', 'B','C','D']
y = np.random.randint(100,400,4)
fig, ax = plt.subplots()
p = ax.bar(x,y, color = plt.cm.Set2.colors)
#Setting labels to each of the bars
[bar.set(label = text) for text,bar in zip(x,p)]
handles = []
for bar in p:
if bar.get_label() in ['B','C’]: #filter ‘B’, ‘C’
handles.append(bar)
labels = ['Brazil', 'Chile’] #Creating labels
ax.legend(handles = handles, labels = labels)
fig.savefig('Explicitly_Passing_Iterable_ 
handles_labels.png',dpi=300, format='png',
bbox_inches='tight')
Context :
• INCLUDE IN LEGEND, ARTISTs WITH LABELS
’B’ or ‘C’
• TWEAK ‘B’ AND ‘C’ LABELS IN LEGEND
Note : If labels is included in call signature, then
labels in the labels list will override all pre-
existing labels of the handles.
Labels Included
162
Explicitly defining the elements in the legend
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Creating Proxy Artists and Adding in the legend
1. Use ax.get_legend_handles_labels() to get existing handles and labels
2. Create instances of patch and Line2D without drawing them on the
axes. Also, add label in initialization itself
3. Add the patch and Line2D to the list of handles from step 1
4. Call the axes.legend() method with below signature :
ax.legend(handles=handles, **kwargs)
ax.legend(handles=handles, labels = labels , **kwargs)
import matplotlib.patches as mpatches
import matplotlib.lines as mlines
plt.rcdefaults()
fig, ax = plt.subplots(figsize = (7,4))
# where some data has already been plotted to ax
# manually define a new patch
patch = mpatches.Patch(color='grey', label='Manual Label patch style’)
# manually define a new line withot any marker
line = mlines.Line2D([],[],color='blue', label='Manual Label line style')
# manually define a new line with marker '*'
marker1 = mlines.Line2D([],[],marker = '*', color='green’,
label='Manual Label Marker style')
# manually define a new line with linestyle None
marker2 = mlines.Line2D([],[],ls = '',marker = '.', color='red’,
label='Manual Label Marker without linestyle')
# handles is a list, so append manual patch
[handles.append(obj) for obj in [patch, line, marker1, marker2]]
# plot the legend
ax.legend(handles=handles,
loc='upper right', bbox_to_anchor = (1,1), title = 'Legend')
leg = ax.get_legend() #fetches/gets the legend object
#Use print(dir(leg)) to get list of all attributes and method of legend class
leg._legend_title_box._text.set_color('Brown')
fig.savefig('Legend_Manual_positioning.png',dpi=150, format='png’,
bbox_inches='tight')
Demo
Steps
Matplotlib, Release 3.4.2, Page No. 2567, 2245, 1469
https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.legend.html?highlight=legend#matplotlib.axes.Axes.legend
https://matplotlib.org/stable/tutorials/intermediate/legend_guide.html, Legend Demo
163
handles, labels = ax.get_legend_handles_labels()
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
Controlling Legend Location, Placement
• Use loc in conjunction with bbox_to_anchor
Note : Use transform object in bbox_transform argument to be explicit about the coordinate system for the
bbox_to_anchor arguments
upper left upper center upper right
center left center center right
lower left lower center lower right
Specifies the location
of the legend
2 Tuple of
floats
4 Tuple of
floats
Represents a point at
which the legend
corner specified by loc
will be anchored.
Represents a bounding box within a corner of
which the legend will be placed. Corner is
specified by loc.
Legend
--- A
* B
loc = “upper left”
bbox(x, y, width, height)
to place legend
Conceptual
OR
bbox – a box
bbox - a point
164
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Use Axes Helper Methods to add
artists to respective containers within
the Axes object.
3
Create a Figure instance
1
Use Figure to create one or more Axes
or Subplot
2
Use Axes Methods (Accessors) to
access artists.
5
Set/Modify the Artist properties after
accessing the artists
6
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick,
tick labels and axis labels.
4
Add Legend, Annotations if any and
Set Title to the Axes
7
#Setting labels to each of the bars
[bar.set(label = text) for text,bar in zip(x,p)]
#Creating handles list
handles = []
for bar in p:
if bar.get_label() in ['B','C']:
handles.append(bar)
#Creating labels list
labels = ['Brazil', 'Chile’]
#Creating Axes Legend
ax.legend(handles = handles, labels = labels,
loc = 'upper left',
bbox_to_anchor = (0,1),
bbox_transform = ax.transAxes)
leg = ax.get_legend()
leg.set_title('South America')
#Creating Figure Legend
fig.legend(handles = handles, labels = labels,
loc = 'upper right’,
bbox_to_anchor = (1,1))
fig.patch.set(fc = 'lightyellow’)
fig.savefig('Legend_location.png',dpi=300,
format='png', bbox_inches='tight')
Demo
import matplotlib.pyplot as plt
import numpy as np
plt.rcdefaults()
np.random.seed(450)
x = ['A', 'B','C','D']
y = np.random.randint(100,400,4)
fig, ax = plt.subplots()
p = ax.bar(x,y, color = plt.cm.Set2.colors)
Controlling Legend Location, Placement
Axes Legend
loc = 'upper left’
bbox_to_anchor = (0,1)
Figure Legend
loc = 'upper right’
bbox_to_anchor = (1,1)
165
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
EPILOGUE
We are at the end of this review of matplotlib.
We have covered lots of ground. Indeed, the learning curve is steep, As there is a very large code base teeming with both stateless
(pyplot) and stateful (OO style) interfaces.
However, certain fundamentals remain the same. That being, Every element in the plot is an artist that knows how to render itself on
the figure canvas. Every Artist instance has properties, parameters and possibly attributes. Axes and Figure are the most important
Artist/Containers in the matplotlib universe. It is through the the Axes instance, one can access almost all the artists in the figure.
Once having accessed the specific artist instance, the properties can be modified using artist.set method. At any moment one can
chose to inspect the artist objects.
Deeper Understanding of Matplotlib
A deeper understanding of matplotlib can emerge by understanding “Anatomy of a Figure” and the hierarchy of artists. This
should be followed by sections - “the Lifecycle of a Plot” and “the Artist Tutorial” in Matplotlib Release, 3.4.2. Without
understanding of these two foundational concepts, we would be limiting our capability and fail to leverage the full range of
matplotlib functions and different class modules. Further, most efforts in debugging and coding would materialize into suboptimal
coding practices.
In terms of the resources, the latest official documentation “Matplotlib Release, 3.4.2” pdf is a veritable treasure trove along with
the examples gallery. Though, it may take some time to establish a pattern to search the required information. It also requires a
threshold knowledge on the part of the user. One section to look out for is – “WHAT'S NEW IN MATPLOTLIB 3.4.0” for the latest
developments and feature rollouts. Further, I also would suggest to check Matplotlib Release, 2.0.2 version especially for the
matplotlib examples section. Apart from the matplotlib official site, StackOverflow is the go to site for all queries with an active
community.
166
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
As we have limited our focus here on becoming comfortable on OO style, we have not covered numerous topic such as color bars,
Tight Layout guide, Constraint layout guide, Different Patches and Advanced tutorials such as image tutorials, path tutorials. The
official documentation has a comprehensive coverage on those.
Seaborn, Pandas and Matplotlib
Depending on specific use case, we shall go for the best combination. For sophisticated plots like heat maps or even faceted grids,
Seaborn churns out beautiful charts with minimal code. For time series, Pandas has a definitive edge. Use matplotlib axes instance
methods to customize them further. In that sense, the whole is indeed greater than the sum of parts.
From R, ggplot2 to Matplotlib and Python
For R Users, matplotlib can be analogous to R base graphics. And very likely, one used to the elegant graphics of ggplot2 can find the
default matplotlib graphics code verbose and plot outputs very basic or “bare bones” similar to plots in R base graphics. But, it’s only
a matter of time before one gets comfortable to the extent of customizing the plots even to the minutest level of setting orientation,
thickness, number, padding of ticks. There is immense flexibility and clarity that is on offer within a clear OO framework of matplotlib.
While I would rather not choose sides between ggplot2 and Python, instead am in favor of viewing them as tools for analysis to be
used depending on particular use contexts. And while ggplot2 is easy to get started with but there is learning curve to gain control
there in as well.
167
If you are majorly a Seaborn User, this review will let you finetune the seaborn plots. As Seaborn is a high-level wrapper for Matplotlib,
in essence, the OO interface will work there as well. While Seaborn plots take very less code and is intuitive from the moment go, this
comes at the cost of lesser flexibility unless complemented by matplotlib. For instance, in a facetgrid object, modifying the row titles,
column titles or the placement of figure legends, subplot legends becomes fairly easy with understanding of OO style. Same goes
with Pandas plotting. Pandas has well developed charting capabilities. Pandas has powerful functionalities to deal with time series
data. Using the three in conjunction particularly in multiple subplots will let you extract the best from each of the modules. Yes, this is
the way.
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Having advocated the matplotlib skills for Python work environment, a suitable learning journey for R user can be becoming
comfortable with Python basics, the basic data structures, built in functions, Numpy, Pandas followed by the pyplot interface and
Seaborn. Pyplot interface, Seaborn and Pandas would be good entry points to be culminated with matplotlib.
A Note on Excel
And for Excel users, a reason for learning matplotlib and even ggplot2 can be the vast library of visualizations that is simply on offer.
Excel spreadsheet shall always remain relevant and has its own place in analysis. But it is no match for advanced data analysis
workflow with tidyverse packages in R; and numpy, Pandas in Python. For quick Exploratory data analysis, you have ggplot2 in R and
matplotlib/Seaborn/pandas in Python. Also, there are some basic limitations in the charts you can plot in Excel. Trellis chart is not
possible unless some tricks in terms of arranging the data in separate columns with blanks is employed. But this is only a trick/
manoeuvre around an inherent limitation. To give another limitation, there is no fill and span feature. If daily sales data is plotted
across months and I want to highlight particular days and weekends, it’s a hard ask. This and lot of other limitations are easily handled
in R and Python.
One definitive edge of R and Python over excel is the reproducibility of the plots. While excel is mainly drag and drop, point and click
type GUI, so every time you are repeating an analysis, there are number of tasks that have to be redone. Data cleaning and
aggregation followed by pivoting and then copying/pasting the relevant outputs. And then doing some EDA and then some graphs if
found useful. This entire flow can of course be automated by VBA but that would boil down to individual expertise and additional
time of code development and debugging. And if the data is huge, excel is no longer conducive even for pivoting. In comparison, R
and Python being code based have a much simpler workflow, less error prone, shorter throughput and exact reproducibility. For a
Excel user to get started on matplotlib, the learning journey would be same as suggested previously for R users. To get a headstart,
check out below link for comparison of Pandas data structures and spreadsheet :
https://pandas.pydata.org/pandas-docs/stable/getting_started/comparison/comparison_with_spreadsheets.html
168
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Back to this Review
Coming back to matplotlib, I would also emphasize being thorough with the basics of the color module, transformation framework
and legend guide. Also, few basics like modifying tick lines, tick labels, spines, gridlines, fig patch, axes patch can elevate the visual
quality of plots. You also have rcparams and style sheets for global changes. Adding subplots using with and without gridspec also
requires due consideration. For modifying font properties, using font dictionary is useful.
There are also args and kwargs that you will frequently encounter with every artist instance. It is convenient to create a dictionary
containing keyword argument and value pairings in the code script. And later use dictionary style unpacking (**dict) to pass those
arguments for setting the artist property. This helps in keeping code organized and avoid repetition of keyword arguments.
Certain properties are common to all artists. Check this useful link. And there are certain artist specific properties. In this review, I have
included the artist methods and instances with high likelihood of use. However, I would strongly recommend being familiar with the
official documentation and website for the complete list and final verdict.
It is also inevitable that we will be dealing with date and time. There are multiple classes to handle the various concepts of date and
time – either a single moment in time or a duration of time. To capture a moment in time, we have the datetime object in Native
Python, datetime64 in Numpy, timestamp in Pandas, matplotlib dates. For parsing strings and ISO 8601 date time formats, we have
dateutil parser; datetime strft, strfp; Pandas pd.to_pydatetime to name a few. TimeDelta objects are very handy and will be frequently
used in conjunction with the datetime equivalent objects. Eventually, we shall develop liking to a method/module/class and stick with
it.
169
Further, this review would be incomplete without coverage of basic data structures in Python, Numpy Array Attributes and Methods,
Pandas Objects Attributes and Methods, Pseudo Random number generation in Python and Numpy, Scipy for Statistics with python. I
have included short sections on them in the unabridged version to make this review self contained. However, I would recommend to
go to official documentation of Pandas and Numpy for details.
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
The Curtain Falls ! But The End is only the beginning !!
On a final note, I have included an appendix containing multiple examples of plots and code implementations with different use
cases. The intent here is to cover a gamut of plotting scenarios that can be always be referred to and specific code snippets be
adapted for specific contexts. The references/source links for code implementations wherever applicable has been mentioned.
There is also a references list at the very end presenting useful links related to matplotlib, python and data viz in general.
Feel free to go through it.
To reaffirm, Consider this review as a compilation of distilled personal learnings and a handy reference.
170
With this, I wrap up this first edition and wish you the very best !
Bhaskar
Aug’2021
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
THANK YOU !
bhaskarjroy1605@gmail.com
www.linkedin.com/in/bhaskar-j-roy-1605
Matplotlib Complete Review Link
Other Interests : Excel VBA | Documentary Film1 | Documentary Film2
171
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
DATA
STRUCTURES
OVERVIEW
172
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
These Containers are also called iterables. They are unidimensional.
Check
• Learning Python by Mark Lutz
• Fluent Python by Luciano Ramalho
• Python-based library for mathematical
computations and processing arrays.
• Homogenous Arrays of Fixed size.
• Vectorization and Broadcasting makes numpy
fast.
• Check Official Link, Scipy Lectures
• Check “Numerical Python: Scientific Computing
and Data Science Applications with Numpy,
SciPy and Matplotlib” by Robert Johansson
ndarray
(N dimensional Array)
List Tuples Dictionaries Sets
Series DataFrame Index
Indexing
Indexing is fundamental
to Pandas and is what
makes retrieval and
access to data much
faster compared to
other tools. It is crucial
to set an appropriate
index to optimize
performance
(Check A Python Data Analyst’s
Toolkit by Gayathri Rajagopalan)
NumericIndex
CategoricalIndex
IntervalIndex
The inbuilt data types and containers in Python cannot be restructured into more
than one dimension, and also do not lend themselves to complex computations.
For analyzing data and building models, arrays is a vital data structure.
MultiIndex
DatetimeIndex
TimedeltaIndex
PeriodIndex
Python
Numpy
Pandas
• Provides efficient implementation of a
DataFrame.
• DataFrames are essentially multidimensional
arrays with attached row and column labels, and
often with heterogeneous types and/or missing
data
• Powerful data wrangling operations similar to
ones in database and spreadsheet framework
Popular tool for data wrangling and analysis
• Grouping (Split-Apply-Combine)
• Handling missing data
• Input/output support
• Data Visualisation
• Integration with Numpy, Matplotlib, Scipy,
and Scikit-learn
• Check
• Pandas online documention
• Stackoverflow
• Python Data Science Handbook
Overview of Data Structures
• Python
• Numpy
• Pandas
Pandas
•Pandas is implemented on top of NumPy
arrays.
•A Pandas dataframe is used to store a
structured dataset. It is more like a
spreadsheet than an array..
NumPy provides:
•extension package to Python for multi-
dimensional arrays
•closer to hardware (efficiency)
•designed for scientific computation
(convenience)
•Also known as array oriented computing
Python objects:
•high-level number objects: integers,
floating point
•containers: lists (costless insertion and
append), dictionaries (fast lookup)
173
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Arithmetic Operators
Operator Name Example
+ Addition x + y
- Subtraction x - y
* Multiplication x * y
/ Division x / y
% Modulus x % y
** Exponentiation x ** y
// Floor division x // y
Assignment Operators
Operator Example Same As
= x = 5 x = 5
+= x += 3 x = x + 3
-= x -= 3 x = x - 3
*= x *= 3 x = x * 3
/= x /= 3 x = x / 3
%= x %= 3 x = x % 3
//= x //= 3 x = x // 3
**= x **= 3 x = x ** 3
&= x &= 3 x = x & 3
|= x |= 3 x = x | 3
^= x ^= 3 x = x ^ 3
>>= x >>= 3 x = x >> 3
<<= x <<= 3 x = x << 3
Comparison Operators
Operator Name Example
== Equal x == y
!= Not equal x != y
> Greater than x > y
< Less than x < y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y
Logical Operators : used to combine conditional statements
Operator Description Example
and Returns True if both statements are true x <15 and x > 5
or Returns True if one of the statements is true x < 5 or y <10
not Reverse the result, returns False if the result is true not(x < 5 or y <10)
Membership Operators : used to test if a sequence is presented in an objet
Operator Description Example
in Returns True if a sequence with the specified
value is present in the object
x in y
not in Returns True if a sequence with the specified
value is not present in the object
x not in y
Identity Operators : used to compare the objects, not if they are equal, but if they are
actually the same object, with the same memory location:
Operator Description Example
is Returns True if both variables are the same object x is y
is not
Returns True if both variables are not the same
object
x is not y
Difference between float division and floor division Link
/ Division x / y 101/25 = 4.04
(Python 3.X version)
Not consistent across different versions of
Python (referred to as floating point
division in number of versions)
% Modulus x % y 101/25 = 1 Return the remainder
// Floor
division
x // y 101//25 = 4 Always Return floored quotient of x and y.
(integer floor of division)
Operators in Python
174
Also, check slide no.339 for ceiling division
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Built-in Functions
abs() delattr() hash() memoryview() set()
all() dict() help() min() setattr()
any() dir() hex() next() slice()
ascii() divmod() id() object() sorted()
bin() enumerate() input() oct() staticmethod()
bool() eval() int() open() str()
breakpoint() exec() isinstance() ord() sum()
bytearray() filter() issubclass() pow() super()
bytes() float() iter() print() tuple()
callable() format() len() property() type()
chr() frozenset() list() range() vars()
classmethod() getattr() locals() repr() zip()
compile() globals() map() reversed() __import__()
complex() hasattr() max() round()
175
Back
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Data representation
•Series: 1D
•DataFrame: 2D
Loading data with *read_csv()*
•header
•names
•sep
•encoding
•converters
Getting information
•index: Series and DataFrame index
•columns: DataFrame column
•shape: Series and DataFrame dimensions
•info(): DataFrame informations
•values: Index and Series values
•unique(): Series unique values
•nunique(): Series number of unique values
Selecting data
•head() and tail()
•Column accessing
•Row accessing
•Fancy indexing
•Logical masking
Indexing and merging data
•*set_index()* and *reset_index()*
•*append()* and *concat()*
Computing over data
•sum()
•str.len(), str.startswith() and str.contains()
•*apply()* and lambda
•isin()
•div()
Organizing data
•transpose() or T
•*sort_values()*
•*groupby()*
•*pivot_table()* and *crosstab()*
Displaying data
•*plot()*
Check Link – PyParis 2017
Pandas DataFrame
Attributes
Indexing, Iteration
Function application, GroupBy &
window
Binary Operator functions
Computation/descriptive stats
Reindexing/selection/label
manipulation
Missing Data handling
Reshaping, sorting, transposing
Combining/comparing/joining/m
erging
Time Series related
Plotting
Serialization/IO/Conversion
Pandas Series
Attributes
Conversion
Indexing, Iteration
Function application, GroupBy &
window
Binary Operator functions
Computation/descriptive stats
Reindexing/selection/label
manipulation
Missing Data handling
Reshaping, sorting
Combining/comparing/joining/m
erging
Time Series related
Accessors
Datetime properties
Datetime methods
Period properties
Timedelta properties
Timedelta methods
String handling
Categorical Accessor
Plotting
Numpy Arrays
Attributes
Array Creation Routines
Shape Manipulation - shape,
resize, transpose
Calculation
Item Selection and Manipulation
Array Conversions
Time Series/Date Functionality
Group by: split-apply-combine
Windowing
Index Objects
Data manipulations
melt(frame[, id_vars, value_vars, var_name, …]) Unpivot a DataFrame from wide to long format, optionally leaving
identifiers set.
pivot(data[, index, columns, values]) Return reshaped DataFrame organized by given index / column values.
pivot_table(data[, values, index, columns, …]) Create a spreadsheet-style pivot table as a DataFrame.
crosstab(index, columns[, values, rownames, …]) Compute a simple cross tabulation of two (or more) factors.
cut(x, bins[, right, labels, retbins, …]) Bin values into discrete intervals.
qcut(x, q[, labels, retbins, precision, …]) Quantile-based discretization function.
merge(left, right[, how, on, left_on, …]) Merge DataFrame or named Series objects with a database-style join.
merge_ordered(left, right[, on, left_on, …]) Perform merge with optional filling/interpolation.
merge_asof(left, right[, on, left_on, …]) Perform an asof merge.
concat(objs[, axis, join, ignore_index, …]) Concatenate pandas objects along a particular axis with optional set
logic along the other axes.
get_dummies(data[, prefix, prefix_sep, …]) Convert categorical variable into dummy/indicator variables.
Top-level missing data
isna(obj) Detect missing values for an array-like object.
isnull(obj) Detect missing values for an array-like object.
notna(obj) Detect non-missing values for an array-like object.
notnull(obj) Detect non-missing values for an array-like object.
Top-level conversions
to_numeric(arg[, errors, downcast]) Convert argument to a numeric type.
Top-level dealing with datetimelike
to_datetime(arg[, errors, dayfirst, …]) Convert argument to datetime.
to_timedelta(arg[, unit, errors]) Convert argument to timedelta.
date_range([start, end, periods, freq, tz, …]) Return a fixed frequency DatetimeIndex.
bdate_range([start, end, periods, freq, tz, …]) Return a fixed frequency DatetimeIndex, with business day as the default
frequency.
period_range([start, end, periods, freq, name]) Return a fixed frequency PeriodIndex.
timedelta_range([start, end, periods, freq, …]) Return a fixed frequency TimedeltaIndex, with day as the default frequency.
infer_freq(index[, warn]) Infer the most likely frequency given the input index.
Pandas General Functions
176
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
PSEUDO
RANDOM NUMBER
GENERATION
(IN PYTHON, NUMPY)
177
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Seeding for Initializing the Pseudo Random Number Generator
What is normally called a random number sequence in reality is a "pseudo-
random" number sequence because the values are computed using a
deterministic algorithm and probability plays no real role.
The "seed" is a starting point for the sequence and the guarantee is that if
you start from the same seed you will get the same sequence of numbers.
This is very useful for example for debugging (when you are looking for an
error in a program you need to be able to reproduce the problem and study
it, a non-deterministic program would be much harder to debug because
every run would be different). Link
§ Python Stdlib Random Module
§ Numpy.random module
§ Legacy RandomState and
§ Generators
Library Module Class Generator Type
Python Standard
Library
random random.Random(seed) Mersenne Twister Generator
MT19937
Numpy random numpy.random.RandomStat
e(seed)
Mersenne Twister Generator
MT19937
Numpy random numpy.random.Generator(bi
t_generator)
Permuted Congruential
Generator PCG64 Link
#Setting the seed of the random number generator
>>>np.random.seed(123456)
#Sequence of numbers generated starting from the seed
position
>>>np.random.randint(10,50,5)
array([11, 37, 42, 33, 46])
#Note that internally the state of the generator has changed
and a new seed is set for the next function call
>>>np.random.randint(10,50,5)
#Output shall be based on deterministically created new seed
internally
array([18, 20, 20, 22, 21])
#Resetting the seed will produce the same sequence of
random numbers
>>>np.random.seed(123456)
>>>np.random.randint(10,50,5)
array([11, 37, 42, 33, 46])
Pseudo Random Number Generation in Python
Note : PRGN is a vast topic with wide applications in simulations (MonteCarlo), game
development, cryptography etc. We shall be covering a small part of it from the point
of view of generating random numbers as and when required for simple use cases.
Random number
Seed Initialize State
Next
Update State
Random number
Next
Update
Pseudo-Random Number Generator (PRNG) generates a sequence of random numbers based on a
seed. Using the same seed makes the PRNG produce the same sequence of random numbers.
*Source : Real-World Cryptography Version 12 by David Wong
Statistical Quality
Prediction Difficulty
Reproducible Results
Multiple Streams
Period
Useful Features
Time Perfomance
Space Usage
Code Size &
Complexity
k-Dimensional
Equidistribution
Evaluation Metrics
PRNG
178
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Random Module standard Library in Python
Bookkeeping functions
seed Initialize the random number generator
getstate Return an object capturing the current internal state of the generator.
setstate setstate() restores the internal state of the generator to what it was at the
time getstate() was called.
Functions for integers
randrange Return a randomly selected element from range(start, stop, step).
randint Return a random integer N such that a <= N <= b
Functions for sequences
choice Return a random element from the non-empty sequence seq.
choices Return a k sized list of elements chosen from the population with
replacement.
shuffle Shuffle the sequence x in place.
sample Return a k length list of unique elements chosen from the population
sequence or set. Used for random sampling without replacement
Real-valued distributions
random Return the next random floating point number in the range [0.0, 1.0).
uniform Return a random floating point number N such that a <= N <= b for a <=
b and b <= N <= a for b < a.
betavariate Beta distribution.
expovariate Exponential distribution.
gammavariate Gamma distribution.
gauss Gaussian distribution.
lognormvariate Log normal distribution.
normalvariate Normal distribution. mu is the mean, and sigma is the standard deviation.
paretovariate Pareto distribution. alpha is the shape parameter.
triangular Return a random floating point number N such that low <= N <= high and
with the specified mode between those bounds
vonmisesvariate von Mises distribution
weibullvariate Weibull distribution. alpha is the scale parameter and beta is the shape
parameter.
Alternative Generator
class random.Random([seed])Class that implements the default pseudo-
random number generator used by the random module.
Note : The functions supplied by Random module are actually bound
methods of a hidden instance of the random.Random class. You can
instantiate your own instances of Random to get generators that don’t
share state.
Functions supplied by the Random Module
Python uses the Mersenne Twister as the core generator.
PRGN by Mersenne Twister Generator
random.seed(a=None, version=2)
Initialize the random number generator. If a is omitted or None, the
current system time is used. [..]
>>>import random
>>>#random.seed(123456)
>>> rints = random.randint(low=0, high=10, size=(4,4))
179
>>> rchoice = random.choice(‘abcdefgh’)
Also, check discussion thread on generating random integers.
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
PRNG in Numpy.Random Module
PRNG by Mersenne Twister Generator
The BitGenerator has a limited set of
responsibilities. It manages state and provides
functions to produce random doubles and
random unsigned 32- and 64-bit values.
The random generator takes the bit generator-provided
stream and transforms them into more useful distributions,
e.g., simulated normal random values. This structure allows
alternative bit generators to be used with little code
duplication.
The Generator is the user-facing object that is nearly identical to the legacy RandomState. It accepts a bit
generator instance as an argument. The default is currently PCG64 but this may change in future versions. As
a convenience NumPy provides the default_rng function to hide these details:
# Construct a new Generator
import numpy as np
>>> rng = np.random.default_rng(123456)
>>> rints = rng.integers(low=0, high=10, size=(4,4))
>>> rints
array([[0, 6, 8, 3], [3, 0, 0, 9], [2, 9, 3, 4], [5, 2, 4, 6]])
>>> rng = np.random.default_rng(123456)
>>> rng.random(4)
array([0.63651375, 0.38481166, 0.04744542,
0.95525274])
>>> rng = np.random.default_rng(123456)
>>> rng.standard_normal(10)
array([ 0.1928212 , -0.06550702, 0.43550665,
0.88235875, 0.37132785, 1.15998882, 0.37835254, -
0.11718594, 2.20800921, 1.95324484])
using combinations of a BitGenerator to create sequences and a Generator to use those sequences to
sample from different statistical distributions
PRNG by PCG
180
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Seeding and State
get_state() Return a tuple representing the internal state of the
generator.
set_state(state) Set the internal state of the generator from a tuple.
seed(self[, seed]) Reseed a legacy MT19937 BitGenerator
Simple random data
rand(d0, d1, …, dn) Random values in a given shape.
randn(d0, d1, …, dn) Return a sample (or samples) from the “standard normal”
distribution.
randint(low[, high, size, dtype]) Return random integers from low (inclusive)
to high (exclusive).
random_integers(low[, high, size]) Random integers of type np.int_ between low and high,
inclusive.
random_sample([size]) Return random floats in the half-open interval [0.0, 1.0).
choice(a[, size, replace, p]) Generates a random sample from a given 1-D array
bytes(length) Return random bytes.
Permutations
shuffle(x) Modify a sequence in-place by shuffling its contents.
permutation(x) Randomly permute a sequence, or return a permuted
range.
Distributions
beta(a, b[, size]) Draw samples from a Beta distribution.
binomial(n, p[, size]) Draw samples from a binomial distribution.
chisquare(df[, size]) Draw samples from a chi-square distribution.
dirichlet(alpha[, size]) Draw samples from the Dirichlet distribution.
exponential([scale, size]) Draw samples from an exponential distribution.
f(dfnum, dfden[, size]) Draw samples from an F distribution.
gamma(shape[, scale, size]) Draw samples from a Gamma distribution.
geometric(p[, size]) Draw samples from the geometric distribution.
gumbel([loc, scale, size]) Draw samples from a Gumbel distribution.
hypergeometric(ngood, nbad, nsample[, s
ize])
Draw samples from a Hypergeometric distribution.
laplace([loc, scale, size]) Draw samples from the Laplace or double exponential distribution with
specified location (or mean) and scale (decay).
logistic([loc, scale, size]) Draw samples from a logistic distribution.
lognormal([mean, sigma, size]) Draw samples from a log-normal distribution.
logseries(p[, size]) Draw samples from a logarithmic series distribution.
multinomial(n, pvals[, size]) Draw samples from a multinomial distribution.
multivariate_normal(mean, cov[, size, …]) Draw random samples from a multivariate normal distribution.
negative_binomial(n, p[, size]) Draw samples from a negative binomial distribution.
noncentral_chisquare(df, nonc[, size]) Draw samples from a noncentral chi-square distribution.
noncentral_f(dfnum, dfden, nonc[, size]) Draw samples from the noncentral F distribution.
normal([loc, scale, size]) Draw random samples from a normal (Gaussian) distribution.
pareto(a[, size]) Draw samples from a Pareto II or Lomax distribution with specified shape.
poisson([lam, size]) Draw samples from a Poisson distribution.
power(a[, size]) Draws samples in [0, 1] from a power distribution with positive exponent a -
1.
rayleigh([scale, size]) Draw samples from a Rayleigh distribution.
standard_cauchy([size]) Draw samples from a standard Cauchy distribution with mode = 0.
standard_exponential([size]) Draw samples from the standard exponential distribution.
standard_gamma(shape[, size]) Draw samples from a standard Gamma distribution.
standard_normal([size]) Draw samples from a standard Normal distribution (mean=0, stdev=1).
standard_t(df[, size]) Draw samples from a standard Student’s t distribution with df degrees of
freedom.
triangular(left, mode, right[, size]) Draw samples from the triangular distribution over the interval [left, right].
uniform([low, high, size]) Draw samples from a uniform distribution.
vonmises(mu, kappa[, size]) Draw samples from a von Mises distribution.
wald(mean, scale[, size]) Draw samples from a Wald, or inverse Gaussian, distribution.
weibull(a[, size]) Draw samples from a Weibull distribution.
zipf(a[, size]) Draw samples from a Zipf distribution.
Random sampling (numpy.random)
Using RandomState Class (Legacy Random Generation)
• RandomState is a class in NumPy. We use an instance of this class to manage
random number generation.
• Random numbers are generated by methods in the class (e.g. the rand or randn
methods).
• Each instance of RandomState comes with its own specific random number
stream.
• The random number stream is initialized (“seeded”) when you create a
RandomState instance.
• You can reset the “seed” using the seed() method.
• We can use the RandomState objects to have different random streams or to
reset a stream.
181
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Accessing the Bit Generator - Attribute
bit_generator Gets the bit generator instance used by the generator
Simple random data - Methods
integers Return random integers from low (inclusive) to high(exclusive), or if
endpoint=True, low (inclusive) to high(inclusive).
random Return random floats in the half-open interval [0.0, 1.0).
choice Generates a random sample from a given array
bytes Return random bytes.
Permutations - Methods
shuffle Modify an array or sequence in-place by shuffling its contents.
permutation Randomly permute a sequence, or return a permuted range.
permuted Randomly permute x along axis axis.
Distributions - Methods
beta(a, b[, size]) Draw samples from a Beta distribution.
binomial(n, p[, size]) Draw samples from a binomial distribution.
chisquare(df[, size]) Draw samples from a chi-square distribution.
dirichlet(alpha[, size]) Draw samples from the Dirichlet distribution.
exponential([scale, size]) Draw samples from an exponential distribution.
f(dfnum, dfden[, size]) Draw samples from an F distribution.
gamma(shape[, scale, size]) Draw samples from a Gamma distribution.
geometric(p[, size]) Draw samples from the geometric distribution.
gumbel([loc, scale, size]) Draw samples from a Gumbel distribution.
hypergeometric(ngood, nbad, nsample[, size]) Draw samples from a Hypergeometric distribution.
laplace([loc, scale, size]) Draw samples from the Laplace or double exponential distribution with
specified location (or mean) and scale (decay).
logistic([loc, scale, size]) Draw samples from a logistic distribution.
lognormal([mean, sigma, size]) Draw samples from a log-normal distribution.
logseries(p[, size]) Draw samples from a logarithmic series distribution.
multinomial(n, pvals[, size]) Draw samples from a multinomial distribution.
multivariate_hypergeometric(colors, nsample) Generate variates from a multivariate hypergeometric distribution.
multivariate_normal(mean, cov[, size, …]) Draw random samples from a multivariate normal distribution.
negative_binomial(n, p[, size]) Draw samples from a negative binomial distribution.
noncentral_chisquare(df, nonc[, size]) Draw samples from a noncentral chi-square distribution.
noncentral_f(dfnum, dfden, nonc[, size]) Draw samples from the noncentral F distribution.
normal([loc, scale, size]) Draw random samples from a normal (Gaussian) distribution.
pareto(a[, size]) Draw samples from a Pareto II or Lomax distribution with specified
shape.
poisson([lam, size]) Draw samples from a Poisson distribution.
power(a[, size]) Draws samples in [0, 1] from a power distribution with positive
exponent a - 1.
rayleigh([scale, size]) Draw samples from a Rayleigh distribution.
standard_cauchy([size]) Draw samples from a standard Cauchy distribution with mode = 0.
standard_exponential([size, dtype, method, out]) Draw samples from the standard exponential distribution.
standard_gamma(shape[, size, dtype, out]) Draw samples from a standard Gamma distribution.
standard_normal([size, dtype, out]) Draw samples from a standard Normal distribution (mean=0, stdev=1).
standard_t(df[, size]) Draw samples from a standard Student’s t distribution with df degrees
of freedom.
triangular(left, mode, right[, size]) Draw samples from the triangular distribution over the
interval [left, right].
uniform([low, high, size]) Draw samples from a uniform distribution.
vonmises(mu, kappa[, size]) Draw samples from a von Mises distribution.
wald(mean, scale[, size]) Draw samples from a Wald, or inverse Gaussian, distribution.
weibull(a[, size]) Draw samples from a Weibull distribution.
Use numpy.random.default_rng for PRNG instead of the
legacy RandomState
numpy.random.default_rng() : Construct a new Generator with the default BitGenerator
(PCG64).
import numpy as np
#Using numpy.random.default_rng to instantiate a Generator with numpy’s
default BitGenerator
# We can specify a seed for reproducible results
>>> rng = np.random.default_rng(12345)
>>> rints = rng.integers(low=0, high=10, size=3)
>>> rints
array([6, 2, 7])
>>> type(rints[0])
<class 'numpy.int64'>
Instead of reseeding, instantiate a new Generator object using np.random.default_rng
Why So ?
Avoid the use of numpy.random.seed(seed) for fixing reproducibility as this operates
at the global state level. For multiple threads and projects with imports and scripts,
the results will get obscured in case of multiple declarations of the
np.random.seed(seed) with different seed arguments.
182
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
import numpy as np
import matplotlib.pyplot
plt.rcdefaults()
fig, axes = plt.subplots(1, 3, figsize=(12, 4))
#Instantiating a generator
rng = np.random.default_rng(12345)
axes[0].hist(rng.random(10000))
axes[0].set_title("random")
axes[1].hist(rng.standard_normal(10000))
axes[1].set_title("standard_normal")
axes[2].hist(rng.integers(low=1, high=10, size=10000), bins=9,
align='left')
axes[2].set_title("integers(low=1, high=10)")
fig.savefig('GeneratorObject.png', dpi = 300, transparent = True)
plt.show()
np.random.default_rng to generate random numbers Avoid np.random functions and instead use default_rng
183
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
HANDLING DATE AND TIME
184
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Classes/Objects to handle date and time
datetime time
timedelta date
datetime64 timedelta64
timestamp PeriodIndex
period datetimeindex
Pandas
Python
Numpy
Python, Numpy, Pandas, Matplotlib
date
Matplotlib
A Specific Date
and Time
With TimeZone support
timedelta timedeltaindex
tzinfo timezone
Absolute time
duration
dateoffset
The datetime module's datetime
object has microsecond precision
(one-millionth of a second).
NumPy's datetime64 object allows
you to set its precision from hours
all the way to attoseconds (10 ^ -
18).
Note : timedelta only holds
days, seconds, microseconds
internally
185
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Conversion Matrix between
datetime, Timestamp and datetime64
https://stackoverflow.com/questions/466345/converting-string-into-datetime/470303#470303
Across Pandas, numpy, Python, matplotlib, strings
Conversion
Matrix
Python Numpy Pandas matplotlib
To string datetime datetime64 timestamp matplotlib.date
Python string
strobj: string object
>>>dt.strptime(strobj) np.datetime64('2005-02-25')
#strings in ISO 8601 date or
datetime format.
pd.Timestamp('2017-01-
01T12’)
#datetime like string
datestr2num(strobj)
Python datetime
import datetime as dt
dtobj : datetime object
dtobj.strftime('%b %d,
%Y’)
dtobj.strftime('The date
is %b %d, %Y')
np.datetime64(dtobj) pd.Timestamp(dtobj) # or
pd.to_datetime(dtobj)
date2num(dtobj)
Numpy datetime64
import numpy as np
dt64 : datetime64 object
np.datetime_as_string(d,
unit='h’)
#Check datetime support
functions
>>> dt64 = np.datetime64('2017-10-24 05:34:20.123456')
>>> unix_epoch = np.datetime64(0, 's')
>>> one_second = np.timedelta64(1, 's')
>>> seconds_since_epoch = (dt64 - unix_epoch) /
one_second
>>> seconds_since_epoch
1508823260.123456
>>>
datetime.datetime.utcfromtimestamp(seconds_since_epoch)
datetime.datetime(2017, 10, 24, 5, 34, 20, 123456)
pd.Timestamp(dt64)
Pandas timestamp
import pandas as pd
ts : timestamp object
ts.dt.strftime('%b %d
%Y’)
#dt is datetime accessor.
Link
ts.to_pydatetime() ts.to_datetime64() # or
ts.to_numpy() # or
ts.asm8
date2num(ts.to_pydatetime())
matplotlib matplotlib.date* num2date(mdate)
From
Go to
Matplotlib.dates
*Currently Matplotlib time is only converted back to datetime objects, which have microsecond resolution, and years that only span 0000 to 9999. Check Link.
https://www.seehuhn.de/pages/pdate.html#unix
186
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Across Pandas, numpy, Python, matplotlib, strings
Conversion
Matrix
Platform Independent Conversion Matrix Platform Independent
To Unix epoch or Unix time or POSIX
time or Unix timestamp
From Unix epoch or Unix time or POSIX
time or Unix timestamp
Python string
strobj: string object
>>>datetime.strptime(string). timestamp()
>>> datetime.datetime(2021,8,1,0,0).timestamp()
1627756200.0
string >>> import datetime
>>> timestamp = datetime.datetime.fromtimestamp(1600000000)
>>> print(timestamp.strftime('%Y-%m-%d %H:%M:%S’))
2020-09-13 17:56:40
Python datetime
import datetime as dt
dtobj : datetime
object
datetime.timestamp()
Check link
datetime >>> dt.datetime.fromtimestamp(1172969203.1)
datetime.datetime(2007, 3, 4, 6, 16, 43, 100000)
Numpy datetime64
import numpy as np
dt64 : datetime64
object
>>> dt64 = np.datetime64('2017-10-24 05:34:20.123456')
>>> unix_epoch = np.datetime64(0, 's')
>>> one_second = np.timedelta64(1, 's')
>>> seconds_since_epoch = (dt64 - unix_epoch) / one_second
>>> seconds_since_epoch
1508823260.123456
>>> datetime.datetime.utcfromtimestamp(seconds_since_epoch)
datetime.datetime(2017, 10, 24, 5, 34, 20, 123456)
datetime64 >>> dt64 = np.datetime64('2017-10-24 05:34:20.123456')
>>> unix_epoch = np.datetime64(0, 's')
>>> one_second = np.timedelta64(1, 's')
>>> seconds_since_epoch = (dt64 - unix_epoch) / one_second
>>> seconds_since_epoch
1508823260.123456
>>>
datetime.datetime.utcfromtimestamp(seconds_since_epoch)
datetime.datetime(2017, 10, 24, 5, 34, 20, 123456)
Pandas timestamp
import pandas as pd
ts : timestamp
object
In [64]: stamps = pd.date_range("2012-10-08 18:15:05",
periods=4, freq="D")
In [66]: (stamps - pd.Timestamp("1970-01-01")) //
pd.Timedelta("1s")
Int64Index([1349720105, 1349806505, 1349892905, 1349979305], dtype='int64')
timestamp >>> pd.to_datetime([1627756200.0, 1600000000], unit = 's’)
DatetimeIndex(['2021-07-31 18:30:00', '2020-09-13 12:26:40'],
dtype='datetime64[ns]', freq=None)
Link
matplotlib matplotlib.date matplotlib.dates.num2epoch(d)
[source]
matplotlib.date matplotlib.dates.epoch2num(unixtime)
[source]
From
Go to
Matplotlib.dates
To
https://stackoverflow.com/questions/466345/converting-string-into-datetime/470303#470303
https://www.seehuhn.de/pages/pdate.html#unix
Conversion Matrix between Unix time vs
datetime, Timestamp and datetime64
187
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
String to datetime object = strptime
datetime object to other formats = strftime
Jun 1 2021 1:33PM
is equals to
%b %d %Y %I:%M%p
• %b :Month as locale’s abbreviated name(Jun)
• %d :Day of the month as a zero-padded decimal
number(1)
• %Y :Year with century as a decimal number(2015)
• %I :Hour (12-hour clock) as a zero-padded decimal
number(01)
• %M :Minute as a zero-padded decimal number(33)
• %p :Locale’s equivalent of either AM or PM(PM)
strptime means string parser, this will convert a string
format to datetime.
strftime means string formatter, this will format a
datetime object to string format.
dateutil capable of parsing most human intelligible date
representations.
dateutil.parser.parse to format a date/time from known formats.
§ Functions :
parser.parse(parserinfo=None, **kwargs)
classmethod parser.isoparse(dt_str) # full ISO-8601 format
parser
§ Classes :
class dateutil.parser.parserinfo(dayfirst=False, yearfirst=False)
Handling Datetime objects and strings Using Pandas for String conversion to datetime object : link
Check answer by alexander, Also check dt accessor methods
>>>import pandas as pd
>>>dates = ['2015-12-25', '2015-12-26’]
# 1) Use a list comprehension.
>>> [d.date() for d in pd.to_datetime(dates)]
[datetime.date(2015, 12, 25), datetime.date(2015, 12, 26)]
# 2) Convert the dates to a DatetimeIndex and extract the
python dates.
>>> pd.DatetimeIndex(dates).date.tolist()
[datetime.date(2015, 12, 25), datetime.date(2015, 12, 26)]
https://stackoverflow.com/questions/466345/converting-
string-into-datetime/470303#470303
Code Meaning
%a Weekday as Sun, Mon
%A
Weekday as full name as Sunday,
Monday
%w Weekday as decimal no as 0,1,2...
%d Day of month as 01,02
%b Months as Jan, Feb
%B Months as January, February
%m Months as 01,02
%y Year without century as 11,12,13
%Y Year with century 2011,2012
%H 24 Hours clock from 00 to 23
%I 12 Hours clock from 01 to 12
%p AM, PM
%M Minutes from 00 to 59
%S Seconds from 00 to 59
%f Microseconds 6 decimal numbers
https://docs.python.org/3/library/datetime.html#strf
time-and-strptime-behavior
strftime() and strptime() Format Codes
Strings to datetime
ISO 8601 Date Format : YYYY-MM-DD
• YYYY is the year [all the digits, i.e. 2012]
• MM is the month [01 (January) to 12(December)]
• DD is the day [01 to 31]
https://www.iso.org/iso-8601-date-and-time-format.html
https://www.cl.cam.ac.uk/~mgk25/iso-time.html
RFC3339 Format
https://datatracker.ietf.org/doc/html/rfc3339
strptime
strftime
datetime.datetime
module
class
methods
SO Thread
Date Format
standards
Check
Series.dt.strftime(*args,
**kwargs)
188
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Concept Scalar Class Array Class pandas Data Type Primary Creation
Method
Date times Timestamp DatetimeIndex datetime64[ns] or
datetime64[ns, tz]
to_datetime or
date_range
Time deltas Timedelta TimedeltaIndex timedelta64[ns] to_timedelta or
timedelta_range
Time spans Period PeriodIndex period[freq] Period or
period_range
Date offsets DateOffset None None DateOffset
pandas captures 4 general
time related concepts:
Date times : A specific date and time with timezone
support.
Similar to datetime.datetime from the standard library.
Time deltas: An absolute time duration.
Similar to datetime.timedelta from the standard library.
Time spans: A span of time defined by a point in time
and its associated frequency.
Date offsets: A relative time duration that respects
calendar arithmetic.
Similar to dateutil.relativedelta.relativedelta from
dateutil package.
Under the hood, pandas represents timestamps using instances of Timestamp and sequences of timestamps
using instances of DatetimeIndex. For regular time spans, pandas uses Period objects for scalar values
and PeriodIndex for sequences of spans
What is epoch time?
The Unix epoch (or Unix time or POSIX time or Unix timestamp) is
the number of seconds that have elapsed since January 1, 1970
(midnight UTC/GMT), not counting leap seconds (in ISO 8601:
1970-01-01T00:00:00Z). Literally speaking the epoch is Unix time 0
(midnight 1/1/1970), but 'epoch' is often used as a synonym for
Unix time.
SO Thread
In [21]: pd.Series(pd.period_range("1/1/2011", freq="M", periods=3))
Out[21]:
0 2011-01
1 2011-02
2 2011-03 dtype: period[M]
import datetime
In [2]: dti = pd.to_datetime( ["1/1/2018", np.datetime64("2018-01-01"), datetime.datetime(2018, 1, 1)]
In [3]: dti
Out[3]: DatetimeIndex(['2018-01-01', '2018-01-01', '2018-01-01'], dtype='datetime64[ns]', freq=None)
Pandas Official Link
Comprehensive
Tutorial
189
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Concept Scalar Class Array Class pandas Data Type Primary Creation Method
Date times Timestamp DatetimeIndex datetime64[ns] or datetime64[ns, tz] to_datetime or date_range
Time deltas Timedelta TimedeltaIndex timedelta64[ns] to_timedelta or timedelta_range
Time spans Period PeriodIndex period[freq] Period or period_range
Date offsets DateOffset None None DateOffset
>>> df = pd.DataFrame({'year': [2015, 2016], 'month': [2, 3], 'day': [4, 5]})
>>> pd.to_datetime(df)
0 2015-02-04
1 2016-03-05
dtype: datetime64[ns]
>>> pd.to_datetime('13000101', format='%Y%m%d', errors='ignore’)
datetime.datetime(1300, 1, 1, 0, 0)
>>> pd.to_datetime('13000101', format='%Y%m%d', errors='coerce’)
NaT
>>> pd.date_range(start='1/1/2018', end='1/08/2018’)
DatetimeIndex(['2018-01-01', '2018-01-02', '2018-01-03’, '2018-01-04', '2018-01-05’,
'2018-01-06’, '2018-01-07', '2018-01-08’],dtype='datetime64[ns]’,
freq='D')
>>> pd.date_range(start='1/1/2018', periods=5, freq='M’)
DatetimeIndex(['2018-01-31', '2018-02-28', '2018-03-31’, '2018-04-30', '2018-05-31’],
dtype='datetime64[ns]’, freq='M')
>>> pd.date_range(start='1/1/2018', periods=5, freq=pd.offsets.MonthEnd(3))
DatetimeIndex(['2018-01-31', '2018-04-30', '2018-07-31’, '2018-10-31', '2019-01-31’],
dtype='datetime64[ns]’, freq='3M')
>>> pd.period_range(start='2017-01-01', end='2018-01-01', freq='M’)
PeriodIndex(['2017-01', '2017-02', '2017-03', '2017-04', '2017-05’, '2017-06’,
'2017-07', '2017-08', '2017-09', '2017-10’, '2017-11', '2017-12’,
'2018-01'], dtype='period[M]')
Frequency codes/ Offset Aliases
Alias Description
B business day frequency
C custom business day
frequency
D calendar day frequency
W weekly frequency
M month end frequency
SM semi-month end
frequency (15th and
end of month)
BM business month end
frequency
CBM custom business month
end frequency
MS month start frequency
SMS semi-month start
frequency (1st and 15th)
BMS business month start
frequency
CBMS custom business month
start frequency
Q quarter end frequency
BQ business quarter end
frequency
QS quarter start frequency
BQS business quarter start
frequency
A, Y year end frequency
BA, BY business year end
frequency
AS, YS year start frequency
BAS, BYS business year start
frequency
BH business hour
frequency
H hourly frequency
T, min minutely frequency
S secondly frequency
L, ms milliseconds
U, us microseconds
N nanoseconds
>>> pd.timedelta_range(start='1 day', periods=4)
TimedeltaIndex(['1 days', '2 days', '3 days', '4 days'], dtype='timedelta64[ns]’, freq='D’)
>>> pd.timedelta_range(start='1 day', end='2 days', freq='6H’)
TimedeltaIndex(['1 days 00:00:00', '1 days 06:00:00', '1 days 12:00:00’,
'1 days 18:00:00', '2 days 00:00:00'], dtype='timedelta64[ns]’, freq='6H')
>>> pd.timedelta_range(start='1 day', end='5 days', periods=4)
TimedeltaIndex(['1 days 00:00:00', '2 days 08:00:00', '3 days 16:00:00’, '5 days 00:00:00’],
dtype='timedelta64[ns]', freq=None)
>>> pd.to_timedelta(np.arange(5), unit='s’)
TimedeltaIndex(['0 days 00:00:00', '0 days 00:00:01', '0 days 00:00:02’,
'0 days 00:00:03', '0 days 00:00:04'], dtype='timedelta64[ns]', freq=None)
>>> pd.to_timedelta(np.arange(5), unit='d’)
TimedeltaIndex(['0 days', '1 days', '2 days', '3 days', '4 days'],
dtype='timedelta64[ns]', freq=None)
# This particular day contains a day light savings time transition
>>> ts = pd.Timestamp("2016-10-30 00:00:00", tz="Europe/Helsinki")
# Respects absolute time
>>> ts + pd.Timedelta(days=1)
Timestamp('2016-10-30 23:00:00+0200', tz='Europe/Helsinki’)
# Respects calendar time
>>> ts + pd.DateOffset(days=1)
Timestamp('2016-10-31 00:00:00+0200', tz='Europe/Helsinki')
Pandas Date and time handling classes : Creation Methods
Also, Check Slide for division of timedeltas to find number of years between two dates.
190
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Attributes
year The year of the datetime.
month The month as January=1, December=12.
day The day of the datetime.
hour The hours of the datetime.
minute The minutes of the datetime.
second The seconds of the datetime.
microsecond The microseconds of the datetime.
nanosecond The nanoseconds of the datetime.
date Returns numpy array of python datetime.date objects (namely, the date part of
Timestamps without timezone information).
time Returns numpy array of datetime.time.
timetz Returns numpy array of datetime.time also containing timezone information.
dayofyear The ordinal day of the year.
day_of_year The ordinal day of the year.
weekofyear (DEPRECATED) The week ordinal of the year.
week (DEPRECATED) The week ordinal of the year.
dayofweek The day of the week with Monday=0, Sunday=6.
day_of_week The day of the week with Monday=0, Sunday=6.
weekday The day of the week with Monday=0, Sunday=6.
quarter The quarter of the date.
tz Return timezone, if any.
freq Return the frequency object if it is set, otherwise None.
freqstr Return the frequency object as a string if its set, otherwise None.
is_month_start Indicates whether the date is the first day of the month.
is_month_end Indicates whether the date is the last day of the month.
is_quarter_startIndicator for whether the date is the first day of a quarter.
is_quarter_end Indicator for whether the date is the last day of a quarter.
is_year_start Indicate whether the date is the first day of a year.
is_year_end Indicate whether the date is the last day of the year.
is_leap_year Boolean indicator if the date belongs to a leap year.
inferred_freq Tries to return a string representing a frequency guess, generated by infer_freq.
REMEMBER
• Valid date strings can be converted to datetime objects
using to_datetimefunction or as part of read functions.
• Datetime objects in pandas support calculations, logical
operations and convenient date-related properties using
the dt accessor.
• A DatetimeIndex contains these date-related properties and
supports convenient slicing.
• Resample is a powerful method to change the frequency of a
time series.
The DatetimeIndex class contains many time series
related optimizations:
• A large range of dates for various offsets are pre-computed and
cached under the hood in order to make generating subsequent
date ranges very fast (just have to grab a slice).
• Fast shifting using the shift method on pandas objects.
• Unioning of overlapping DatetimeIndex objects with the same
frequency is very fast (important for fast data alignment).
• Quick access to date fields via properties such as year, month,
etc.
• Regularization functions like snap and very fast as of logic.
Datetime Index
191
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Methods
normalize(*args, **kwargs) Convert times to midnight.
strftime(*args, **kwargs) Convert to Index using specified date_format.
snap([freq]) Snap time stamps to nearest occurring frequency.
tz_convert(tz) Convert tz-aware Datetime Array/Index from one time zone to another.
tz_localize Localize tz-naive Datetime Array/Index to tz-aware Datetime
Array/Index.
round(*args, **kwargs) Perform round operation on the data to the specified freq.
floor(*args, **kwargs) Perform floor operation on the data to the specified freq.
ceil(*args, **kwargs) Perform ceil operation on the data to the specified freq.
to_period(*args, **kwargs) Cast to PeriodArray/Index at a particular frequency.
to_perioddelta(freq) Calculate TimedeltaArray of difference between index values and index
converted to PeriodArray at specified freq.
to_pydatetime Return Datetime Array/Index as object ndarray of datetime.datetime
objects.
to_series Create a Series with both index and values equal to the index keys
useful with map for returning an indexer based on an index.
to_frame([index, name]) Create a DataFrame with a column containing the Index.
month_name Return the month names of the DateTimeIndex with specified locale.
day_name(*args, **kwargs) Return the day names of the DateTimeIndex with specified locale.
mean(*args, **kwargs) Return the mean value of the Array.
to_perioddelta(freq) Calculate TimedeltaArray of difference between index values and index
converted to PeriodArray at specified freq.
to_pydatetime Return Datetime Array/Index as object ndarray of datetime.datetime
objects.
to_series Create a Series with both index and values equal to the index keys
useful with map for returning an indexer based on an index.
Check if two datetime indexes are equal
>>> import pandas as pd
>>> idx1 = pd.date_range('2020-01-01','2020-12-31',freq='D’)
>>> idx2 = pd.date_range('2020-01-01','2020-11-01',freq='D’)
>>> idx3 = pd.date_range('2020-01-01','2020-12-31',freq='D’)
>>> help(idx1.equals)
Help on method equals in module
pandas.core.indexes.datetimelike: equals(other: object) -> bool
method of pandas.core.indexes.datetimes.DatetimeIndex instance
Determines if two Index objects contain the same elements.
>>> print(idx1.equals(idx2))
False
>>> print(idx1.equals(idx3))
True
Datetime Index
192
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
APPENDIX
Package Name pandas numpy scipy
Version 1.2.1 1.19.5 1.6.0
Summary Powerful data structures for
data analysis, time series, and
statistics
NumPy is the fundamental
package for array computing
with Python.
SciPy: Scientific Library for
Python
All Code examples have been checked with Python Version 3.8.3 and IDE JupyterLab
The package details are as below :
* Use pip show <package_name> to get details of a package
193
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
#https://stackoverflow.com/questions/24547047/how-to-
make-matplotlib-graphs-look-professionally-done-like-this
# Check out the answer by DrV
# Necessary imports
import numpy as np
import matplotlib.pyplot as plt
plt.rcdefaults()
# create some fictive access data by hour
xdata = np.arange(25)
ydata = np.random.randint(10, 20, 25)
ydata[24] = ydata[0]
# let us make a simple graph
fig = plt.figure(figsize=[7,5])
ax = plt.subplot(111)
l = ax.fill_between(xdata, ydata)
# set the basic properties
ax.set_xlabel('Time of posting (US EST)')
ax.set_ylabel('Percentage of Frontpaged Submissions')
ax.set_title('Likelihood of Reaching the Frontpage')
# set the limits
ax.set_xlim(0, 24)
ax.set_ylim(6, 24)
# set the grid on
ax.grid('on’)
# change the fill into a blueish color with opacity .3
l.set_facecolors([[.5,.5,.8,.3]])
# change the edge color (bluish and transparentish) and
thickness
l.set_edgecolors([[0.3, 0.6, .9, 1]])
l.set_linewidths([2])
# add more ticks
ax.set_xticks(np.arange(25))
# remove tick marks
ax.xaxis.set_tick_params(size=0)
ax.yaxis.set_tick_params(size=0)
# change the color of the top and right spines to opaque
gray
ax.spines['right'].set_color((.8,.8,.8))
ax.spines['top'].set_color((.8,.8,.8))
# tweak the axis labels
xlab = ax.xaxis.get_label()
ylab = ax.yaxis.get_label()
xlab.set_style('italic')
xlab.set_size(10)
ylab.set_style('italic')
ylab.set_size(10)
# tweak the title
ttl = ax.title
ttl.set_weight('bold')
fig.savefig('Filline plot.png',dpi=300, format='png',
bbox_inches='tight')
Back
194
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
#https://stackoverflow.com/questions/66011487/highlighti
ng-weekends-in-small-multiples
# Check the answer by Patrick Fitzgerald
CONTEXT
• Raw data is time Series with Daily frequency
• Figure with multiple subplots with each one for a
particular month (iteration used)
• Weekends need to be highlighted
Code
195
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
#https://stackoverflow.com/questions/66011487/highlighti
ng-weekends-in-small-multiples
# Check the answer by Patrick Fitzgerald
CONTEXT
• Raw data is time Series with Daily frequency
• Figure with multiple subplots with each one for a
particular month (iteration used)
• Weekends need to be highlighted
#Create a date time index
dti = pd.date_range('2020-01-01 00:00', '2020-12-31 
23:59', freq='6H’)
consumption = rng.integers(1000, 2000, size=dti.size)
# Create the data frame
df = pd.DataFrame(dict(consumption=consumption),
index=dti)
# Draw and format subplots by looping through months
and flattened array of axes
fig, axs = plt.subplots(4, 3, figsize=(13, 9), sharey=True)
for month, ax in zip(df.index.month.unique(), axs.flat):
# Select monthly data and plot it
df_month = df[df.index.month == month]
ax.plot(df_month.index, df_month['consumption’])
# set limit similar to plot shown in question
ax.set_ylim(0, 2500)
# Draw vertical spans for weekends: computing the time
delta and adding it to the date solves the problem of
exceeding the df_month.index
timedelta = pd.to_timedelta(df_month.index.freq)
weekends =df_month.index[df_month.index.weekday
>=5].to_series()
for date in weekends:
ax.axvspan(date, date+timedelta, facecolor='k’,
edgecolor=None, alpha=.1)
# Format tick labels
ax.set_xticks(ax.get_xticks())
tk_labels = [pd.to_datetime(tk, unit='D').strftime('%d’)
for tk in ax.get_xticks()]
ax.set_xticklabels(tk_labels, rotation=0, ha='center’)
# Add x labels for months
ax.set_xlabel(df_month.index[0].month_name().upper(),
labelpad=5)
ax.xaxis.set_label_position('top’)
# Add title and edit spaces between subplots
year = df.index[0].year
freq = df_month.index.freqstr
title = f'{year} consumption displayed for each month with
a {freq} frequency’
fig.suptitle(title.upper(), y=0.95, fontsize=12)
fig.subplots_adjust(wspace=0.1, hspace=0.5)
fig.text(0.5, 0.99, 'Weekends are highlighted by using 
the DatetimeIndex’,
ha='center', fontsize=14, weight='semibold’)
plt.savefig('Daily consumption with weekends 
highlighted.png’, bbox_inches = "tight",pad_inches =
0.5, dpi=300)
plt.show()
# Also, Check below excellent links on using spans and fill
areas
• https://stackoverflow.com/questions/8270981/in-a-
matplotlib-plot-can-i-highlight-specific-x-value-
ranges?noredirect=1&lq=1
• https://stackoverflow.com/questions/48973471/how-to-
highlight-weekends-for-time-series-line-plot-in-
python/66052245#66052245
• https://stackoverflow.com/questions/64356412/highlig
ht-time-interval-in-multivariate-time-series-plot-using-
matplotlib-and-se?rq=1
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates # used only for
method 2
plt.rcdefaults()
# Create sample dataset
# random number generator
rng = np.random.default_rng(seed=1)
196
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
#https://stackoverflow.com/questions/48973471/how-to-
highlight-weekends-for-time-series-line-plot-in-
python/66052245#66052245
# Check the answer by Patrick Fitzgerald
Highlighting Weekends in a Time
Series
Code
197
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
#https://stackoverflow.com/questions/48973471/how-to-
highlight-weekends-for-time-series-line-plot-in-
python/66052245#66052245
# Check the answer by Patrick Fitzgerald
#Highlighting Weekends on a time series plot
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
# Create sample dataset
# random number generator
rng = np.random.default_rng(seed=1234)
dti = pd.date_range('2017-01-01', '2017-05-15', freq='D’)
counts = 5000 + np.cumsum(rng.integers(-1000, 1000,
size=dti.size))
df = pd.DataFrame(dict(Counts=counts), index=dti)
# Draw pandas plot: x_compat=True converts the pandas x-axis units to matplotlib
# date units (not strictly necessary when using a daily frequency like here)
ax = df.plot(x_compat=True, figsize=(10, 7), legend = None, ylabel='Counts’)
# reset y limits to display highlights without gaps
ax.set_ylim(*ax.get_ylim())
# Highlight weekends based on the x-axis units
xmin, xmax = ax.get_xlim()
days = np.arange(np.floor(xmin), np.ceil(xmax)+2)
weekends = [(dt.weekday()>=5)|(dt.weekday()==0) for dt in mdates.num2date(days)]
ax.fill_between(days, *ax.get_ylim(), where=weekends, facecolor='k', alpha=.1)
ax.set_xlim(xmin, xmax) # set limits back to default values
# Create appropriate ticks using matplotlib date tick locators and formatters
ax.xaxis.set_major_locator(mdates.MonthLocator())
ax.xaxis.set_minor_locator(mdates.MonthLocator(bymonthday=np.arange(5, 31, step=7)))
ax.xaxis.set_major_formatter(mdates.DateFormatter('n%b'))
ax.xaxis.set_minor_formatter(mdates.DateFormatter('%d'))
# Additional formatting
ax.figure.autofmt_xdate(rotation=0, ha='center')
title = 'Daily count of trips with weekends highlighted from SAT 00:00 to MON 00:00'
ax.set_title(title, pad=20, fontsize=14)
plt.savefig('Daily count of trips with weekends highlighted.png',bbox_inches = "tight",pad_inches = 0.5, dpi=300)
plt.show()
Highlighting Weekends in a
Time Series
198
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
#Matplotlib Release, Page no.120-121
#plt.figure creates a matplotlib.figure.Figure instance
fig = plt.figure(figsize = (10,5))
rect = fig.patch # a rectangle instance
rect.set_facecolor('lightgoldenrodyellow')
ax1 = fig.add_axes([0.1, 0.3, 0.4, 0.4])
rect = ax1.patch
rect.set_facecolor('lightslategray’)
for label in ax1.xaxis.get_ticklabels():
# label is a Text instance
label.set_color('red')
label.set_rotation(45)
label.set_fontsize(16)
for line in ax1.yaxis.get_ticklines():
# line is a Line2D instance
line.set_color('green')
line.set_markersize(25)
line.set_markeredgewidth(3)
fig.savefig(f'tick_labels and lines.png',dpi=300,
format='png', bbox_inches='tight')
plt.show()
import matplotlib.pyplot as plt
import numpy as np
fig, (ax1, ax2) = plt.subplots(1,2, sharey = True)
x = np.random.randint(1,20,20)
#Set the face color of figure background
rect = fig.patch
rect.set(facecolor = 'aliceblue’)
#Set the face color of the axes backgrounds
ax1.patch.set(facecolor = 'salmon')
ax2.patch.set(facecolor = 'aquamarine')
ax2.plot(x)
plt.show()
#Matplotlib Release, Page no.120-121
import numpy as np
import matplotlib.pyplot as plt
# Fixing random state for reproducibility
np.random.seed(19680801)
fig, ax = plt.subplots()
ax.plot(100*np.random.rand(20))
ax.patch.set(facecolor="yellow",alpha =0.5)
ax.patch.set_facecolor("yellow")
ax.patch.set_alpha(0.5)
fig.set(facecolor = "salmon")
# Use automatic StrMethodFormatter
ax.yaxis.set_major_formatter('${x:1.2f}')
ax.yaxis.set_tick_params(which='major’, labelcolor='green’,
labelleft=False, labelright=True)
#ax.set_xticks(ax.get_xticks())
#ax.set_xticklabels(ax.get_xticklabels(), rotation = 30)
plt.setp(ax.get_xticklabels(), rotation=30, ha='right')
plt.show()
Appearance of Plot
Fig, Axes background
Tick labels, Tick lines
199
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
#https://stackoverflow.com/questions/3584805/in-
matplotlib-what-does-the-argument-mean-in-fig-add-
subplot111?rq=1
Check the answer by compuphys
The link lucidly discusses the multiple call signatures of
fig.add_subplot method.
The add_subplot() method has several call signatures:
• add_subplot(nrows, ncols, index, **kwargs)
• add_subplot(pos, **kwargs)
• add_subplot(ax)
• add_subplot()
The following code demonstrates the difference between
the first two call signatures.
• add_subplot(nrows, ncols, index, **kwargs)
• add_subplot(pos, **kwargs)
The first call signature can accommodate any number of
rows, columns. Whereas the second call signature is
limited to total 9 subplots
pos is a three digit integer, where the first digit is the
number of rows, the second the number of columns, and
the third the index of the subplot. i.e.
fig.add_subplot(235) is the same as fig.add_subplot(2, 3,
5).
Also, Check the documentation :
https://matplotlib.org/stable/api/_as_gen/matplotlib.pypl
ot.subplot.html
import matplotlib.pyplot as plt
def plot_and_text(axis, text):
'''Simple function to add a straight line
and text to an axis object'''
axis.plot([0,1],[0,1])
axis.text(0.02, 0.9, text)
f = plt.figure()
f2 = plt.figure()
_max = 12
for i in range(_max):
axis = f.add_subplot(3,4,i+1, fc=(0,0,0,i/(_max*2)), xticks=[],
yticks=[])
plot_and_text(axis,chr(i+97) + ') ' + '3,4,' +str(i+1))
# If this check isn't in place, a
# ValueError: num must be 1 <= num <= 15, not 0 is raised
if i < 9:
axis = f2.add_subplot(341+i, fc=(0,0,0,i/(_max*2)), 
xticks=[], yticks=[])
plot_and_text(axis,chr(i+97) + ') ' + str(341+i))
f.tight_layout()
f2.tight_layout()
f._label = 'fig1'
f2._label = 'fig2'
for fig in [f, f2]:
print(f'{fig._label}')
fig.savefig(f'Adding Subplots_{fig._label}.png’,
dpi = 300,format='png', bbox_inches='tight')
plt.show()
Making multiple subplots
fig.add_subplot() Method
200
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
#Legend Call Signature : ax.legend()
#All the lines have been assigned valid text labels
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
lgh1, = ax.plot([0, 0.5], [0, 1], '-g', label="a")
lgh2, = ax.plot([0, 1], [0, 1], '-r', label = 'b')
lgh3, = ax.plot([0, 1], [0, 0.5], '-b', label="c")
#plt.legend() #This is equivalent to ax.legend()
ax.legend()
plt.show()
#Legend Call Signature : ax.legend()
#Except the red line, the other lines have been assigned
text labels
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
lgh1, = ax.plot([0, 0.5], [0, 1], '-g', label="a")
lgh2, = ax.plot([0, 1], [0, 1], '-r')
lgh3, = ax.plot([0, 1], [0, 0.5], '-b', label="c")
#plt.legend() #This is equivalent to ax.legend()
ax.legend()
plt.show()
#https://stackoverflow.com/questions/54390421/matplotli
b-legend-not-working-correctly-with-handles
#Legend Call Signature : ax.legend(handles, labels)
#Explicitly Passing the handles and corresponding labels
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
lgh1, = ax.plot([0, 0.5], [0, 1], '-g', label="a")
lgh2, = ax.plot([0, 1], [0, 1], '-r')
lgh3, = ax.plot([0, 1], [0, 0.5], '-b', label="c")
handles = [lgh1, lgh2, lgh3]
labels = [‘A’, ‘B’, ‘C’]
#plt.legend(handles = handles, labels = labels)
ax.legend(handles = handles, labels =labels)
plt.show()
Automatic Detection Legend Automatic Detection Legend Explicitly Define Legend Elements
201
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
#Legend Call Signature : ax.legend(handles)
#The patches are not in the axes but specifically added
for legend
#Secondly, we have used a dictionary of labels and colors
#https://stackoverflow.com/questions/39500265/manually
-add-legend-items-python-matplotlib
import matplotlib.patches as mpatches
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
legend_dict = { 'data1' : 'green', 'data2' : 'red’,
'data3' : 'blue' }
patchList = []
for key in legend_dict:
data_key = mpatches.Patch(color=legend_dict[key],
label=key)
patchList.append(data_key)
ax.legend(handles=patchList)
fig.savefig('legend_from dict.png',bbox_inches='tight')
#Legend Call Signature : ax.legend()
#Except the red line, the other lines have been assigned
text labels
#https://stackoverflow.com/questions/39500265/manually
-add-legend-items-python-matplotlib
import matplotlib.pyplot as plt
from matplotlib.lines import Line2D
fig, ax = plt.subplots(figsize=(5,3))
colors = ['black', 'red', 'green’]
#Instantiating proxy line artists
lines = [Line2D([0], [0], color=c, linewidth=3, linestyle='--’)
for c in colors]
labels = ['black data', 'red data', 'green data']
ax.legend(lines, labels)
fig.savefig('proxy artists_line 2D.png',dpi = 150,
bbox_inches='tight')
#https://stackoverflow.com/questions/24867363/matplotli
b-put-legend-symbols-on-the-right-of-the-labels
fig, ax = plt.subplots(figsize = (5,3))
ax.plot([1,2],[3,4], label='labeltext')
ax.legend(markerfirst=False)
plt.gcf().savefig('markerfirst_legend.png',dpi = 150,
bbox_inches='tight')
plt.show()
Proxy Artists : Manual Patch Addition Proxy Artists : Manual Lines Addition Labels Appearing before Legend key
202
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
#Legend Call Signature : ax.legend(handles)
#The patches are not in the axes but specifically added
for legend
#https://stackoverflow.com/questions/27826064/matplotli
b-make-legend-keys-square
import matplotlib.lines as mlines
plt.rcdefaults()
fig, ax = plt.subplots(figsize = (5,3))
rect1 = mlines.Line2D([], [], marker="s", markersize=10,
linewidth=0, color="red")
rect2 = mlines.Line2D([], [], marker="s", markersize=10,
linewidth=0, color="yellow")
ax.legend((rect1, rect2), ('Men', 'Women'))
fig.savefig('Making square markers.png',dpi = 150,
bbox_inches='tight')
plt.show()
https://matplotlib.org/stable/gallery/text_labels_and_ann
otations/legend_demo.html#sphx-glr-gallery-text-labels-
and-annotations-legend-demo-py
Check the link above. There are various other use cases of
legend covered.
#https://stackoverflow.com/questions/44603480/remove-
legend-key-in-matplotlib
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(100)
x = np.random.normal(size=5000)
fig, ax = plt.subplots()
ax.hist(x, label = 'something')
ax.legend()
leg = plt.gca().get_legend()
leg.legendHandles[0].set_visible(False)
plt.show()
Making Legend Keys square Legend Addition in Complex Plots Remove Legend key
203
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
#https://stackoverflow.com/questions/44413020/how-to-specify-legend-position-in-matplotlib-in-graph-coordinates?
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = 12, 4
fig, axes = plt.subplots(ncols=3)
locs = ["upper left", "lower left", "center right"]
for l, ax in zip(locs, axes.flatten()): #Iterating over the subplots to create line plots
ax.set_title(l)
ax.plot([1,2,3],[2,3,1], "b-", label="blue")
ax.plot([1,2,3],[1,2,1], "r-", label="red")
ax.legend(loc=l, bbox_to_anchor=(0.6,0.5)) #Note the green dot in the axes is the bbox_to_anchor
ax.scatter((0.6),(0.5), s=81, c="limegreen", transform=ax.transAxes)
plt.tight_layout()
fig.savefig('Legend loc.png', dpi = 300)
plt.show()
Legend
‘loc’ argument
upper left upper center upper right
center left center center right
lower left lower center lower right
204
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
import matplotlib.pyplot as plt
fig = plt.figure(dpi=100, figsize=(11, 8), tight_layout=True)
available = ['default'] + plt.style.available
#Creating sample data
x = ['Product A','Product B', 'Product C']
y = [100,130,220]
n, p = 6, 1
for i, style in enumerate(available[n*p:n*(p+1)]):
with plt.style.context(style):
ax = fig.add_subplot(2,3, i + 1)
ax.bar(x,y)
ax.set_title(style)
plt.savefig('Styles_second_2.png',bbox_inches = "tight",pad_inches = 0.05, dpi=300)
plt.show()
import matplotlib.pyplot as plt
fig = plt.figure(dpi=100, figsize=(11, 8), tight_layout=True)
available = ['default'] + plt.style.available
#Creating sample data
x = ['Product A','Product B', 'Product C']
y = [100,130,220]
n,p = 6, 0
for i, style in enumerate(available[n*p:n*(p+1]):
with plt.style.context(style):
ax = fig.add_subplot(2,3, i + 1)
ax.bar(x,y)
ax.set_title(style)
plt.savefig('Styles_second_2.png',bbox_inches = "tight",pad_inches = 0.05, dpi=300)
plt.show()
Style Sheets
in Matplotlib
Back
205
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.ticker as ticker
#Instantiating figure and axes
fig, ax = plt.subplots(2,2,figsize= (10,5), sharey = True)
x = np.random.randint(3,20,20)
#Setting background color of figure
rect = fig.patch
rect.set(facecolor = 'lightgrey', alpha = 0.5)
ax1,ax2,ax3,ax4 = ax.flatten()
#ax1.patch.set(facecolor = 'salmon')
#ax2.patch.set(facecolor = 'aquamarine’)
# Setting different colors for the axes using colormap
#colors = [ plt.cm.Pastel1(x) for x in np.linspace(0, 1,4)]
#colors = [ plt.cm.tab20c(x) for x in np.linspace(0, 1,4)]
#Accessing colors from the colormap
colors = [ plt.cm.Accent(x) for x in np.linspace(0,0.4,4)]
[ax.patch.set(facecolor = colors[i]) for i,ax in enumerate(fig.axes)]
#Line Plot in axes ax2
ax2.plot(x)
# Setting x and y limits in all the axes
[ax.set_xlim(0,10) for ax in fig.axes]
[ax.set_ylim(0,20) for ax in fig.axes]
#Setting the tick locators using locator class
ax1.xaxis.set_major_locator(ticker.NullLocator())
ax2.xaxis.set_major_locator(ticker.MultipleLocator(2))
ax3.xaxis.set_major_locator(ticker.FixedLocator([0,1,2,8,9]))
ax4.xaxis.set_major_locator(ticker.LinearLocator(5))
#Making list of Locator labels
Locator_labels = ['NullLocator()', 'MultipleLocator(2)', 'FixedLocator([0,1,2,8,9])', 'LinearLocatorn(numticks = 5)’]
#Adding text iteratively to the figure axes
for ax, label in zip(fig.axes,Locator_labels):
ax.text(x = 5,y = 2, s = label, rotation_mode = 'anchor', ha = 'center', size = 18)
fig.savefig('Tick Locators_2.png', dpi=300, format='png', bbox_inches='tight’)
plt.show()
Tick Locators
Back
206
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
#https://matplotlib.org/stable/gallery/ticks_and_spines/ti
ck-formatters.html?highlight=formatter
import matplotlib.pyplot as plt
from matplotlib import ticker
def setup(ax, title):
"""Set up common parameters for the Axes in the
example."""
# only show the bottom spine
ax.yaxis.set_major_locator(ticker.NullLocator())
ax.spines.right.set_color('none’)
ax.spines.left.set_color('none’)
ax.spines.top.set_color('none’)
# define tick positions
ax.xaxis.set_major_locator(ticker.MultipleLocator(1.00))
ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.25))
ax.xaxis.set_ticks_position('bottom’)
ax.tick_params(which='major', width=1.00, length=5)
ax.tick_params(which='minor', width=0.75, length=2.5,
labelsize=10)
ax.set_xlim(0, 5)
ax.set_ylim(0, 1)
ax.text(0.0, 0.2, title, transform=ax.transAxes,
fontsize=14, fontname='Monospace’,
color='tab:blue’)
TICK FORMATTERS (1 of 3) - CODE Back
Code continues to Next slide
Code Start
207
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
#https://matplotlib.org/stable/gallery/ticks_and_spines/tick-formatters.html?highlight=formatter
# Tick formatters can be set in one of two ways, either by passing a ``str`` # or function to
`~.Axis.set_major_formatter` or `~.Axis.set_minor_formatter`,
# or by creating an instance of one of the various `~.ticker.Formatter` classes # and providing that to
`~.Axis.set_major_formatter` or`~.Axis.set_minor_formatter`.
# The first two examples directly pass a ``str`` or function.
fig0, axs0 = plt.subplots(2, 1, figsize=(8, 2)) fig0.suptitle('Simple Formatting’)
# A ``str``, using format string function syntax, can be used directly as a # formatter. The variable ``x`` is the tick
value and the variable ``pos`` is # tick position. This creates a StrMethodFormatter automatically.
setup(axs0[0], title="'{x} km’”)
axs0[0].xaxis.set_major_formatter('{x} km’)
# A function can also be used directly as a formatter. The function must take # two arguments: ``x`` for the tick
value and ``pos`` for the tick position, # and must return a ``str`` This creates a FuncFormatter automatically.
setup(axs0[1], title="lambda x, pos: str(x-5)") axs0[1].xaxis.set_major_formatter(lambda x, pos: str(x-5))
fig0.tight_layout()
TICK FORMATTERS(2 of 3) – CODE CONTINUED
Code continues to Next slide
Code continues from Prev slide
208
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
# StrMethod formatter
setup(axs1[1], title="StrMethodFormatter('{x:.3f}’)”)
axs1[1].xaxis.set_major_formatter(ticker.StrMethodFormatter("{x:.3f}"))
# FuncFormatter can be used as a decorator
@ticker.FuncFormatter
def major_formatter(x, pos):
return f'[{x:.2f}]’
setup(axs1[2], title='FuncFormatter("[{:.2f}]".format)’)
axs1[2].xaxis.set_major_formatter(major_formatter)
# Fixed formatter
setup(axs1[3], title="FixedFormatter(['A', 'B', 'C', ...])")
# FixedFormatter should only be used together with FixedLocator.
# Otherwise, one cannot be sure where the labels will end up.
positions = [0, 1, 2, 3, 4, 5]
labels = ['A', 'B', 'C', 'D', 'E', 'F’]
axs1[3].xaxis.set_major_locator(ticker.FixedLocator(positions))
axs1[3].xaxis.set_major_formatter(ticker.FixedFormatter(labels))
# Scalar formatter
setup(axs1[4], title="ScalarFormatter()")
axs1[4].xaxis.set_major_formatter(ticker.ScalarFormatter(useMathText=True))
# FormatStr formatter
setup(axs1[5], title="FormatStrFormatter('#%d’)”)
axs1[5].xaxis.set_major_formatter(ticker.FormatStrFormatter("#%d"))
# Percent formatter
setup(axs1[6], title="PercentFormatter(xmax=5)")
axs1[6].xaxis.set_major_formatter(ticker.PercentFormatter(xmax=5))
fig1.tight_layout()
plt.show()
#https://matplotlib.org/stable/gallery/ticks_and_spines/tick-
formatters.html?highlight=formatter
# The remaining examples use Formatter objects.
fig1, axs1 = plt.subplots(7, 1, figsize=(8, 6))
fig1.suptitle('Formatter Object Formatting’)
# Null formatter
setup(axs1[0], title="NullFormatter()")
axs1[0].xaxis.set_major_formatter(ticker.NullFormatter())
TICK FORMATTERS(3 of 3) – CODE CONTINUED
Code End
Code continues from Prev slide
209
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
import matplotlib.pyplot as plt
from matplotlib.patches import Ellipse
import numpy as np
from matplotlib.text import OffsetFrom
plt.rcdefaults()
#Instantiating Figure and Axes
fig, (ax1, ax2) = plt.subplots(1, 2, figsize = (12,5))
fig.patch.set(edgecolor = ‘0.8’, lw = 4)
#Configuring bbox and arrow arguments for axes, data
and fig
bbox_args_axes = dict(boxstyle ="round", fc ="salmon",
alpha = 0.6)
bbox_args_data = dict(boxstyle ="round", fc ="cyan",
alpha = 0.6)
bbox_args_fig = dict(boxstyle ="round",
fc ="lightgreen", alpha = 0.6)
arrow_args = dict(arrowstyle ="->")
ax1.annotate(text = '(1,0)nax1 axesncoordinate system’, xy =(1,0), xycoords = ax1.transAxes,
xytext=(2,5 ), textcoords=ax1.transData,fontsize = 15,
bbox = bbox_args_axes, arrowprops = arrow_args, ha = 'left')
ax1.annotate(text = '(1,1)nax1 axesncoordinate system', xy =(1,1),xycoords = ax1.transAxes,
xytext=(2,17.5), textcoords=ax1.transData,fontsize = 15,
bbox = bbox_args_axes, arrowprops = arrow_args, va = 'top')
ax2.annotate(text = '(0,1)nax2 axesncoordinate system’, xy =(0.0,1),xycoords = ax2.transAxes,
xytext=(0.5, 22.5), textcoords=ax2.transData,fontsize = 15,
bbox = bbox_args_axes, arrowprops = arrow_args)
ax2.annotate(text = '(0,20)nax2 datancoordinate system', xy =(0,20),xycoords = ax2.transData,
xytext=(-5, 22.5), textcoords=ax2.transData,fontsize = 15,
bbox = bbox_args_data, arrowprops = arrow_args)
ax1.annotate(text = '(0,0.5)nfigncoordinate system', xy =(0,0.5),xycoords = fig.transFigure,
xytext=(0.03, 0.95), textcoords=fig.transFigure, fontsize = 15,
bbox = bbox_args_fig, arrowprops = arrow_args)
ax2.annotate(text = '(1,1)nfigncoordinate system’, xy =(1,1.1),xycoords = fig.transFigure,
xytext=(7, 25), textcoords=ax2.transData,fontsize = 15, va = 'top’,
bbox = bbox_args_fig, arrowprops = arrow_args )
ax2.annotate(text = '(1,1)nax2 axesncoordinate system’, xy =(1,1),xycoords = ax2.transAxes,
xytext=(2,17.5), textcoords=ax2.transData,fontsize = 15, va = 'top',
bbox = bbox_args_axes, arrowprops = arrow_args)
[ax.set_xlim(0,10) for ax in fig.axes] #Setting x limits in all the axes
[ax.set_ylim(0,20) for ax in fig.axes] #Setting y limits in all the axes
ax2.text(x = 0.5, y = 0.5, s = “Axes instance : ax2”, ha = ‘center’,transform = ax2.transAxes, fontsize = 20)
ax1.text(x = 0.5, y = 0.5, s = “Axes instance : ax1”, ha = ‘center’,transform = ax1.transAxes, fontsize = 20)
fig.text(x = 0.02, y = 0.02, s = “Figure instance”, fontsize = 20)
plt.subplots_adjust(bottom =0.2) #Adjusting padding
#plt.tight_layout(rect = [0,0,0.8,0.8])
fig.savefig(‘coordinate systems.png’, dpi = 150, format=‘png’, bbox_inches='tight’ )
Coordinate
System
Transform
Objects
Back
Code End
Code start
210
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
import matplotlib.gridspec as gridspec
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(13.5, 7))
#Creating Gridspec object
spec = gridspec.GridSpec(nrows = 5,ncols = 11,
wspace = 0.2, hspace=0.1)
#Create Axes for Jupyter
ax1 = fig.add_subplot(spec[:, 0])
#Create Axes IPython
ax2 = fig.add_subplot(spec[:, 1])
# Create Axes for scikit
ax3 = fig.add_subplot(spec[0, 2::])
# Create Axes for Statsmodels
ax4 = fig.add_subplot(spec[1, 2:4])
# Create Axes for Seaborn
ax5 = fig.add_subplot(spec[1, 4:8])
# Create Axes for Scipy
ax6 = fig.add_subplot(spec[1:3, 8:11])
# Create Axes for Pandas
ax7 = fig.add_subplot(spec[2, 2:5])
# Create Axes for Matplotlib
ax8 = fig.add_subplot(spec[2, 5:8])
# Create Axes for Numpy
ax9 = fig.add_subplot(spec[3, 2:11])
# Create Axes for Python
ax10 = fig.add_subplot(spec[4, 2:11])
for ax in fig.axes:
plt.sca(ax)
plt.xticks([]) #Removing x ticks
plt.yticks([]) #Removing y ticks
#Create labels list containing axes names
labels = ['Jupyter', 'Ipython', 'scikit', 'statsmodels’,
'Seaborn', 'Scipy’, 'Pandas', 'Matplotlib’,
'Numpy', 'Python’]
ax_list = [ax1, ax2, ax3, ax4, ax5, ax6, ax7, ax8,ax9, ax10]
#Adding text to axes
for i,(ax,label) in enumerate(zip(ax_list,labels)):
if i <=1 :
ax.text(0.5, 0.5, label, transform = ax.transAxes,
fontsize = 20, ha = 'center’,
rotation = 90, va = 'center')
else :
ax.text(0.5, 0.5, label, transform = ax.transAxes,
fontsize = 20, ha = 'center')
fig.savefig('Libraries_Python.png', dpi = 300,
format='png’, bbox_inches='tight')
GridSpec Use
Display structure of important packages
Back
211
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
fig, ax = plt.subplots(1,3, figsize=(18,6))
# Setting both xlim and ylim as (0,20)
[ax_obj.set(ylim = (0,20), xlim = (0,20)) for ax_obj in
fig.axes]
# Plotting a scatter point at (10,10)
[ax_obj.scatter([10],[10], color = 'r') for ax_obj in fig.axes]
# Setting number of ticks in both x axis and y axis to 5 in
all the axes
[ax_obj.xaxis.set_major_locator(ticker.LinearLocator(5)) for
ax_obj in fig.axes]
[ax_obj.yaxis.set_major_locator(ticker.LinearLocator(5)) for
ax_obj in fig.axes]
# Creating a list of horizontal alignment values
ha_values = ['left', 'center', 'right’]
# Setting the horizontal alignment by iterating over axes
for i, ax_obj in enumerate(fig.axes):
if i < len(ha_values):
ax_obj.text(10,10, f'ha : {ha_values[i]}', rotation = 20,
rotation_mode = 'anchor’,
ha = ha_values[i],fontsize = 22)
#Setting the figure background color to white
fig.patch.set(color = 'white')
# Adding a title
fig.suptitle("Horizontal Alignment Demo - 'left', 'center’, 'right'nrotation = 20,
rotation_mode = 'anchor' nposition = (10,10)", x = 0.05, y = 0.95, fontsize = 22, ha = 'left' )
plt.tight_layout(rect = [0,0,1,0.95])
fig.savefig("Horizontal Alignment_vertical plots.png”,
dpi=300, format='png', bbox_inches='tight')
Horizontal
Alignment
of
Text Instances
Back
Code End
Code Start
212
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
# UDF for Adding Annotations to place data labels
def Add_data_labels(rect): #rect is ax.patches object
for p in rect:
#Retrieving the Axes container of patch object
ax = rect[0].axes
#Adding text iteratively on top of each bar
#Using get and set on x,y parameters of rectangle
patch
ax.text( x = p.get_x()+p.get_width()/2,
y = ax.get_ylim()[1]*0.01+ p.get_height(),
s = p.get_height(),
ha = 'center', size = 12)
#User Defined Function to stylize Axes
def stylize_axes(ax):
#Making the axes background light gray
ax.set_facecolor('.85’)
# Setting the y limits
ax.set_ylim(0,ax.get_ylim()[1]*1.1)
#Making the tick lines disppear
ax.tick_params(length=0)
#Making the xtick labels rotated
[labels.set(size = 13,rotation = 15,
rotation_mode = 'anchor',ha = 'right’)
for labels in ax.get_xticklabels()]
#Setting grid lines to white color
ax.grid(True, axis='x', color='white’)
#Setting grid lines below all the artists
ax.set_axisbelow(True)
#Making all the spines invisible
[spine.set_visible(False) for spine in ax.spines.values()]
#Code snippet that uses User defined functions –
stylize_axes and Add_data_labels
# Doing the necessary imports
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.spines as spines
plt.rcdefaults()
%matplotlib inline
#create a list of rgba colors from a matplotlib.cm
colormap
colors = [ plt.cm.Dark2(x) for x in np.linspace(0, 1, len(x))]
#Instantiating figure, axes
fig, ax = plt.subplots(figsize = (10,6), frameon = True)
#Creating a bar plot by ax.bar method
# Note the colors passed as the color argument
bars = ax.bar(x,y, color = colors)
#Setting the title of plot
StrTitle = 'Consumption of Packed beverages in billion
liters,n by Beverage Type, Global, 2019'
ax.set_title(StrTitle, size = 16)
#Using the user defined functions – to add data labels
and stylize the axes
Add_data_labels(ax.patches)
stylize_axes(ax)
fig.savefig('Patches.png', dpi=300, format='png',
bbox_inches='tight')
data = {'Water':465,
'Alchoholic Beverages':271,
'Carbonated soft drinks': 224,
'Milk and Dairy products': 260,
'New Drinks': 75,
'Fruits and Vegetable Juices': 86}
x = list(data.keys())
y = list(data.values())
Also check : https://www.dunderdata.com/blog/create-a-
bar-chart-race-animation-in-python-with-matplotlib
Note : The data figures in the graph are fictitious.
UDF stands for User Defined Function.
Bar Charts
Back
Code End
Code Start
213
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.spines as spines
from matplotlib import cm
colors = [ cm.Dark2(x) for x in np.linspace(0, 1, len(x))]
%matplotlib inline
#Instantiating figure and axes
fig, ax = plt.subplots(figsize = (9,7), frameon = True)
t = ax.bar(x,y, color = colors)
#Setting xticklines visibility to false
[ticks.set(visible= False)for ticks in ax.get_xticklines()]
#Setting the y limits to leave ample room above bar
ax.set_ylim(0,ax.get_ylim()[1]*1.1)
#Rotating and aligning the xtick labels
for labels in ax.get_xticklabels():
labels.set(rotation = 15, size = 12,
rotation_mode = 'anchor',
ha = 'right'
)
#Rotating and aligning the ytick labels
for labels in ax.yaxis.get_ticklabels():
plt.setp(labels, size = 14, rotation_mode = 'anchor’,
ha = 'right’)
#Setting the figure and axes background colors
ax.patch.set(facecolor = 'white')
plt.setp(fig.patch, facecolor = 'lightgoldenrodyellow’,
edgecolor = 'grey', lw = 4)
#Making the y gridlines lighter by setting opacity to 0.5
for lines in ax.get_ygridlines():
lines.set(alpha = 0.5)
#Setting off the xgridlines to False
ax.grid(axis = 'x', b = False)
# Adding Annotations to place data labels
for p in ax.patches:
ax.text(p.get_x()+p.get_width()/2,
ax.get_ylim()[1]*0.01+ p.get_height(),
p.get_height(),
ha = 'center', size = 14
)
# Adding Legend labels and handles
[p.set_label(x[i]) for i,p in zip(range(len(x)), ax.patches)]
ax.legend(bbox_to_anchor = (1.04,.5),
loc="upper left", borderaxespad=0,
fontsize = 'large’)
#Setting title of the axes
StrTitle = 'Consumption of Packed beverages in billion
liters,n by Beverage Type, Global, 2019'
ax.set_title(StrTitle, size = 16)
fig.savefig('Consumption_4.png', dpi=300, format='png',
bbox_inches='tight')
data = {'Water':465,
'Alchoholic Beverages':271,
'Carbonated soft drinks': 224,
'Milk and Dairy products': 260,
'New Drinks': 75,
'Fruits and Vegetable Juices': 86}
x = list(data.keys())
y = list(data.values())
#Basic Bar Chart
import matplotlib.pyplot as plt
#data
x = [1, 2, 3, 4, 5]
h = [10, 8, 12, 5, 7]
c = ['red', 'yellow', 'black', 'blue', 'orange']
#bar plot
plt.bar(x, height = h, color = c)
fig = plt.gcf()
plt.show()
Bar Chart
With
Legend
Back
214
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
#Using CN color specification
color = ’C9’
from matplotlib import cm
colors = cm.Set1.colors[1:]
ax.barh(x,y, color = colors)
#Alternatively Accessing colors from Palettable external library
# https://jiffyclub.github.io/palettable/#matplotlib-discrete-colormap
colors =palettable.colorbrewer.qualitative.Dark2_7.mpl_colors
color = ’C9’ #Using CN color specification
ax.set_axisbelow(False) #To place grid lines (integral part of axes) above bars
ax.grid(axis = 'y', which = 'major',color = '0.75',b = True,ls = '-')
https://stackoverflow.com/questions/1726391/matplotlib-draw-grid-lines-behind-other-
graph-elements
#Using Named color in top
right for the facecolor or fc of
bars
color = ’salmon’
ax.bar(x,y, color = 'salmon')
Bar Chart Styling
215
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
#https://datascience.stackexchange.com/questions/57853
/group-small-values-in-a-pie-chart
#Changes vis-à-vis the code in the link :
• Using colors from external library
• Dictionary key value pairings
• Placement of the legend and the corresponding
transform object used
import matplotlib.pyplot as plt
import numpy as np
plt.rcdefaults()
dic = {'Class a': 26.9,
'Class b': 18,
'Class c': 16.8,
'Class d': 13,
'Class e': 9.83,
'Class f': 9,
'Class g': 3.59,
'Class h': 0.08,
'Class i': 1.42,
'Class j': 1.09,
'Class k': 0.903,
'Class l': 0.873,
'Class m': 0.28,
'Class n': 0.24,
'Class o': 0.112}
# group together all elements in the dictionary whose
value is less than 2
# name this group 'All the rest'
import itertools
newdic={}
for key, group in itertools.groupby(dic, lambda k: 'All the
rest' if (dic[k]<2) else k):
newdic[key] = sum([dic[k] for k in list(group)])
#Extracting labels and sizes from the dictionary
labels = newdic.keys()
sizes = newdic.values()
#Accessing colors from external library Palettable
from palettable.cartocolors.qualitative import Bold_10
colors = Bold_10.mpl_colors
#Instantiating figure and axes
fig, ax = plt.subplots()
#Creating pie chart and assigning to variable
pie_list = ax.pie(sizes, labels=labels, autopct='%1.1f%%',
explode=(0,0,0,0,0,0,0,.2), startangle=0,
colors = colors)
#Note pie_list is a tuple containing list of wedges and text
#Set equal scaling of the axes (make circles circular)
ax.axis('equal’)
#Setting the background color of figure to white
fig.patch.set(facecolor = "white")
#Setting the font size and colors for the value and text
labels
[txt.set(color = 'white', fontsize = 15) for txt in pie_list[2]]
[txt.set(color = 'black', fontsize = 12) for txt in pie_list[1]]
#Adding a Legend. Note the transform object used
plt.legend(pie_list[0],labels, bbox_to_anchor=(1,0.9),
loc="upper left", fontsize=10,
bbox_transform=plt.gca().transAxes)
#Alternate Legend
#plt.legend(pie_list[0],labels, bbox_to_anchor=(1,1),
loc="upper right", fontsize=10,
bbox_transform=plt.gcf().transFigure)
plt.tight_layout()
#The color code can be replaced by any of the other
colormaps as below
#Accessed colors count same as number of values
num = len(newdic.values()) #num for number of values
colors = [ plt.cm.tab20c(x) for x in np.linspace(0, 1,num)]
colors = plt.cm.Dark2(range(15))
colors = plt.cm.tab20(range(15))
colors = [ plt.cm.viridis(x) for x in np.linspace(0, 1, num)]
colors = [ plt.cm.Set3(x) for x in np.linspace(0, 1, num)]
colors = plt.cm.tab10.colors
colors = plt.cm.Paired.colors
colors = plt.cm.nipy_spectral(np.linspace(0.1,1,num))
colors = plt.cm.CMRmap(np.linspace(0.2,1,num))
Pie Charts
Back
Code End
Code Start
fig.savefig('PieChart.png',dpi=300, format='png’,
bbox_inches='tight')
plt.show() #Also check link
216
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
import palettable
import matplotlib.pyplot as plt
import numpy as np
#Creating random data for the pie chart
data = np.random.randint(50,200, size=30)
#Instantiating figure
fig = plt.figure(figsize = (10,7))
#Axes for nipy_spectral colormap
color = plt.cm.nipy_spectral(np.linspace(0,.9,len(data)))
cc = plt.cycler("color", color)
with plt.style.context({"axes.prop_cycle" : cc}):
ax1 = fig.add_subplot(231, aspect="equal")
ax1.pie(data)
ax1.set_title('nipy_spectral', fontsize = 20,
fontweight = 'medium', y = 1, x= 0.5,
transform = ax.transAxes)
#Axes for CMRmap
color = plt.cm.CMRmap(np.linspace(0,0.9,len(data)))
cc = plt.cycler("color", color)
with plt.style.context({"axes.prop_cycle" : cc}):
ax2 = fig.add_subplot(232, aspect="equal")
ax2.pie(data)
ax2.set_title('CMRmap', fontsize = 20,
fontweight = 'medium’, y = 1, x= 0.5,
transform = ax2.transAxes)
#Axes for jet colormap
color = plt.cm.jet(np.linspace(0,0.9,len(data)))
cc = plt.cycler("color", color)
with plt.style.context({"axes.prop_cycle" : cc}):
ax3 = fig.add_subplot(233, aspect="equal")
ax3.pie(data)
ax3.set_title('jet', fontsize = 20, fontweight = 'medium’,
y = 1, x= 0.5, transform = ax3.transAxes)
#Axes for tab20 colormap
color = plt.cm.tab20(np.linspace(0,1,len(data))))
cc = plt.cycler("color",
with plt.style.context({"axes.prop_cycle" : cc}):
ax4 = fig.add_subplot(234, aspect="equal")
ax4.pie(data)
ax4.set_title('tab20', fontsize = 20,
fontweight = 'medium', y = 1, x= 0.5,
transform = ax4.transAxes)
#Axes for tab10 colormap
color = plt.cm.tab10(np.linspace(0,1,len(data)))
cc = plt.cycler("color", color)
with plt.style.context({"axes.prop_cycle" : cc}):
ax5 = fig.add_subplot(235, aspect="equal")
ax5.pie(data)
ax5.set_title('tab10', fontsize = 20,
fontweight 'medium', y = 1, x= 0.5,
transform = ax5.transAxes)
#Axes for Bold_10 colormap from palettable
from palettable.cartocolors.qualitative import Bold_10
color = Bold_10.mpl_colors
cc = plt.cycler("color",Bold_10.mpl_colors)
with plt.style.context({"axes.prop_cycle" : cc}):
ax6 = fig.add_subplot(236, aspect="equal")
ax6.pie(data)
ax6.set_title('Bold_10', fontsize = 20,
fontweight = 'medium', y = 1, x= 0.5,
transform = ax6.transAxes)
fig.savefig("Piecharts 3.png",dpi=300, format='png’,
bbox_inches='tight')
plt.show()
Check the link :
https://stackoverflow.com/questions/52134364/how-
could-i-get-a-different-pie-chart-color
Check answer by ImportanceOfBeingErnest
The code has been tweaked for six pie charts in this slide.
There is scope to optimize the code with iteration through
a list of colors from six colormaps to draw the
corresponding pie charts.
Further, check the discussion in the link. Currently
maximum 20 colors available from the listed colormaps
under the qualitative colormaps list. Namely the
colormaps tab20, tab20b, tab20c.
However, more colors can be obtained from Linear
Segmented Colormaps by using the methods in section
ACCESSING COLORS FROM A MATPLOTLIB
COLORMAP. Note depending on the colormap
accessed, the colors extracted may look similar but having
different luminosity levels.
Pie Chart
colors
217
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
#https://stackoverflow.com/questions/20551477/changing-
line-properties-in-matplotlib-pie-chart
Three use case scenarios :
ü Setting the width of the lines separating the pie wedges
ü Setting the facecolor or edgecolor of the wedges
ü Retrieving the wedges from ax.patches if one has
an axes object but no direct access to the pie's wedges
Also check :
• Axes.pie() method Matplotlib, Release 3.4.2, Pg No. 1286
• wedge properties in Matplotlib, Release 3.4.2, Pg No.
2421-23
#Setting the linewidth, edge color of the wedges
#Necessary imports
import matplotlib.pyplot as plt
import numpy as np
# Fixing random state for reproducibility
np.random.seed(19680765)
ax = plt.subplot(111)
wedges, texts = ax.pie(np.abs(np.random.randn(6)))
for w in wedges:
w.set_linewidth(2)
w.set_edgecolor('cyan’)
fig = ax.figure
fig.savefig('PieChart_linewidths.png',dpi=300,
format='png')
# Retrieving wedges from axes object when no direct
access
wedges = [patch for patch in ax.patches if 
isinstance(patch, matplotlib.patches.Wedge)]
Pie Chart
Wedge
Properties
218
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
cell_width = 212
cell_height = 22
swatch_width = 48
margin = 12
topmargin = 40
p = colcount*rowcount
# Sort colors by hue, saturation, value and name
if sort_colors is True:
by_hsv =
sorted((tuple(mcolors.rgb_to_hsv(mcolors.to_rgb(color))),
name)
for name, color in colors.items())
names = [name for hsv, name in by_hsv]
else:
names = list(colors)
n = len(names[x*p:(x+1)*p])
nrows = rowcount - emptyrows
ncols = n // nrows + int(n % nrows > 0)
width = cell_width * ncols + 2 * margin
height = cell_height * nrows + margin + topmargin
dpi = 72
fig, ax = plt.subplots(figsize=(width / dpi, height / dpi),
dpi=dpi)
fig.subplots_adjust(margin/width, margin/height,
(width-margin)/width,
(height-topmargin)/height)
XKCD Colors
ax.set_xlim(0, cell_width * ncols)
ax.set_ylim(cell_height * (nrows-0.5), -cell_height/2.)
ax.yaxis.set_visible(False)
ax.xaxis.set_visible(False)
ax.set_axis_off()
ax.set_title(title, fontsize=20, loc="left", pad=10)
for i, name in enumerate(names[x*p:(x+1)*p]):
row = i % nrows
col = i // nrows
y = row * cell_height
swatch_start_x = cell_width * col
text_pos_x = cell_width * col + swatch_width + 7
ax.text(text_pos_x, y, name, fontsize=14,
horizontalalignment='left',
verticalalignment='center')
ax.add_patch(Rectangle(xy=(swatch_start_x, y-9),
width=swatch_width,
height=18, facecolor=colors[name],
edgecolor='0.7'))
filename = f'XKCD_Colors_{x+2}.png'
return fig, filename
#Creating the figure using the user defined function
xkcd_fig, filename =
plot_colortable(mcolors.XKCD_COLORS,
"XKCD Colors",
x = 7, rowcount = 30, colcount = 4)
xkcd_fig.patch.set(facecolor = 'white')
#Creating user defined function to return a tuple – first
element being a figure containing color patches and
second element being the figure name
from matplotlib.patches import Rectangle
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
def plot_colortable(colors, title, sort_colors=True,
emptyrows=0, x=0, colcount = 4 , rowcount = 30 ):
"""colors : any of the color tables from mcolors module
title : title to be displayed in the figure
sort_colors : Whether the colors are to be sorted
x : the iteration count
colcount : number of columns
rowcount : number of rows
"""
Back
219
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
#https://matplotlib.org/stable/gallery/statistics/hist.html#
sphx-glr-gallery-statistics-hist-py
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import colors
from matplotlib.ticker import PercentFormatter
plt.rcdefaults()
# Fixing random state for reproducibility
np.random.seed(19680801)
N_points = 100000
n_bins = 20
# Generate a normal distribution, center at x=0 and y=5
x = np.random.randn(N_points)
y = .4 * x + np.random.randn(100000) + 5
#Instantiating figure and axes
fig, axs = plt.subplots(1, 2, tight_layout=True)
# N is the count in each bin, bins is the lower-limit of the
bin
N, bins, patches = axs[0].hist(x, bins=n_bins)
# We'll color code by height, but you could use any scalar
fracs = N / N.max()
# we need to normalize the data to 0..1 for the full range
of the colormap
norm = colors.Normalize(fracs.min(), fracs.max())
# Now, we'll loop through our objects and set the color of
each accordingly
for thisfrac, thispatch in zip(fracs, patches):
color = plt.cm.viridis(norm(thisfrac))
thispatch.set_facecolor(color)
# We can also normalize our inputs by the total number of
counts
axs[1].hist(x, bins=n_bins, density=True)
# Now we format the y-axis to display percentage
axs[1].yaxis.set_major_formatter(PercentFormatter(xmax=
1))
fig.savefig(f'Histograms_colored.png',dpi=300,
format='png', bbox_inches='tight’)
plt.show()
Histogram
Color Coded
Back
220
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
#https://jakevdp.github.io/PythonDataScienceHandbook/04.08-multiple-
subplots.html
import matplotlib.pyplot as plt
import numpy as np
plt.rcdefaults()
# Create some normally distributed data
mean = [0, 0]
cov = [[1, 1], [1, 2]]
prng = np.random.default_rng(123456)
x, y = prng.multivariate_normal(mean, cov, 3000).T
# Set up the axes with gridspec
fig = plt.figure(figsize=(6, 6))
grid = plt.GridSpec(4, 4, hspace=0.2, wspace=0.2)
main_ax = fig.add_subplot(grid[:-1, 1:])
y_hist = fig.add_subplot(grid[:-1, 0], xticklabels=[], sharey=main_ax)
x_hist = fig.add_subplot(grid[-1, 1:], yticklabels=[], sharex=main_ax)
# scatter points on the main axes
main_ax.plot(x, y, 'ok', markersize=3, alpha=0.2)
# histogram on the attached axes
x_hist.hist(x, 40, histtype='stepfilled’, orientation='vertical', color='gray')
x_hist.invert_yaxis()
y_hist.hist(y, 40, histtype='stepfilled', orientation='horizontal', color='gray')
y_hist.invert_xaxis()
fig.savefig('Marginal histograms.png', dpi = 300, transparent = True)
Marginal Histogram
Back
221
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
#https://stackoverflow.com/questions/43962735/creating-barplots-using-for-loop-
using-pandas-matplotlib
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# Create some dummy data
data = pd.DataFrame()
zones = ["east","west","south","central"]
data['zone']=np.hstack([[zone]*np.random.randint(10, 20) for zone in zones])
data['OS Usage']=np.random.random(data.shape[0])
# Now create a figure
fig, axes = plt.subplots(1,4, figsize=(12,3))
# Now plot each zone on a particular axis
for i, zone in enumerate(zones):
data.loc[data.zone==zone].hist(column='OS Usage',
bins=np.linspace(0,1,10),
ax=axes[i],
sharey=True)
axes[i].set_title('OS Usage in {0}'.format(zone))
axes[i].set_xlabel('Value')
axes[i].set_ylabel('Count')
fig.tight_layout()
fig.savefig(f'Barplots_using_for_loops_Pandas.png',dpi=300, format='png’,
bbox_inches='tight')
Histograms
• Using for loop to create
histograms in multiple subplots
222
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
#https://stackoverflow.com/questions/37106828/how-to-get-data-labels-on-a-seaborn-pointplot
Soldier_years = [1850, 1851, 1852, 1853, 1854, 1855, 1856, 1857, 1858, 1859, 1860, 1861, 1862, 1863, 1864, 1865, 1866,
1867, 1868, 1869, 1870]
num_records_yob = [7, 5, 8, 9, 15, 17, 23, 19, 52, 55, 73, 73, 107, 137, 65, 182, 228, 257, 477, 853, 2303]
%matplotlib inline
import seaborn as sns
import matplotlib.pyplot as plt
sns.set(style="darkgrid")
f, (ax) = plt.subplots(figsize=(12, 6), sharex=True)
sns.set_style("darkgrid")
ax = sns.pointplot(x=Soldier_years, y=num_records_yob)
[ax.text(p[0], p[1]+50, p[1], color='g') for p in zip(ax.get_xticks(), num_records_yob)]
ax.figure.savefig(f'Datalabels_Seaborn_Pointplot.png',dpi=150, format='png', bbox_inches='tight')
Seaborn point plot
Adding data labels
223
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
#https://stackoverflow.com/questions/35788626/how-to-plot-a-heatmap-from-pandas-dataframe
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
# create some random data; replace that by your actual dataset
data = pd.DataFrame(np.random.rand(11, 5), columns=['A', 'B', 'C', 'D', 'E'], index = range(2000, 2011, 1))
# plot heatmap
ax = sns.heatmap(data.T)
# turn the axis label
for item in ax.get_yticklabels():
item.set_rotation(0)
for item in ax.get_xticklabels():
item.set_rotation(90)
# save figure
ax.figure.savefig(f'Heatmap_Pandas_Dataframe.png',dpi=150, format='png', bbox_inches='tight')
plt.show()
Plotting Heat Map from Pandas
Pandas + Seaborn + Matplotlib
224
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
#https://stackoverflow.com/users/190597/unutbu
import matplotlib.pyplot as plt
import numpy as np
def make_image(data, outputname, size=(1, 1), dpi=80):
fig = plt.figure()
fig.set_size_inches(size)
ax = plt.Axes(fig, [0., 0., 1., 1.])
ax.set_axis_off()
fig.add_axes(ax)
plt.set_cmap('hot')
ax.imshow(data, aspect='equal')
plt.savefig(outputname, dpi=dpi)
# data = mpimg.imread(inputname)[:,:,0]
data = np.arange(1,10).reshape((3, 3))
make_image(data, 'test2.png', dpi = 300)
matplotlib.axes.Axes.imshow
#https://stackoverflow.com/users/249341/hooked
from numpy import random
import matplotlib.pyplot as plt
data = random.random((5,5))
img = plt.imshow(data, interpolation='nearest')
img.set_cmap('hot')
plt.axis('off')
plt.savefig("test3.png", bbox_inches='tight', dpi = 300)
#https://stackoverflow.com/questions/9295026/matplotlib
-plots-removing-axis-legends-and-white-spaces?rq=1
fig = plt.figure(figsize=(5, 5))
fig.patch.set_visible(False) # turn off the patch
plt.imshow([[0, 1], [0.5, 0.7]], interpolation="nearest",
cmap='hot')
plt.axis('off')
plt.savefig("test.png", dpi = 300)
225
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Plotting heat maps
From Pandas Data frame
and Correlation Matrix
#https://stackoverflow.com/questions/55663030/
#plotting-two-heat-maps-side-by-side-in-matplotlib/
def corr_heatmaps(data1, data2, method='pearson'):
# Basic Configuration
fig, axes = plt.subplots(ncols=2, figsize=(12, 12))
ax1, ax2 = axes
corr_matrix1 = data1.corr(method=method)
corr_matrix2 = data2.corr(method=method)
columns1 = corr_matrix1.columns
columns2 = corr_matrix2.columns
# Heat maps.
im1 = ax1.matshow(corr_matrix1, cmap='coolwarm')
im2 = ax2.matshow(corr_matrix2, cmap='coolwarm')
# Formatting for heat map 1.
ax1.set_xticks(range(len(columns1)))
ax1.set_yticks(range(len(columns1)))
ax1.set_xticklabels(columns1)
ax1.set_yticklabels(columns1)
ax1.set_title(data1.name, y=-0.1)
plt.setp(ax1.get_xticklabels(), rotation=45, ha='left', rotation_mode='anchor')
plt.colorbar(im1, fraction=0.045, pad=0.05, ax=ax1)
# Formatting for heat map 2.
ax2.set_xticks(range(len(columns2)))
ax2.set_yticks(range(len(columns2)))
ax2.set_xticklabels(columns2)
ax2.set_yticklabels(columns2)
ax2.set_title(data2.name, y=-0.1)
plt.setp(ax2.get_xticklabels(), rotation=45, ha='left', rotation_mode='anchor')
plt.colorbar(im2, fraction=0.045, pad=0.05, ax=ax2)
fig.tight_layout()
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame(np.random.rand(10, 10))
fig, axes = plt.subplots(ncols=2, figsize=(8, 4))
ax1, ax2 = axes
im1 = ax1.matshow(df.corr(), cmap = 'YlGnBu')
im2 = ax2.matshow(df.corr(), cmap = 'YlGn')
fig.colorbar(im1, ax=ax1)
fig.colorbar(im2, ax=ax2)
fig.savefig('plotting_two_adjacent_heatmaps.png’,
dpi = 300, bbox_inches='tight')
Back
Creating User
Defined Function
Code End
Code Start
226
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Seaborn horizontal
bar plot
boxstyle: round
roundingsize:.015
Seaborn horizontal
bar plot
boxstyle: round
roundingsize : 2
Seaborn horizontal
bar plot
• Create spacing
between bars
• Adding dummy
row in data
Seaborn horizontal
bar plot
• Create spacing
between bars
• Resetting position
of tick/ ticklabel/
Basic horizontal bar
plot
Basic
horizontal bar plot
Secondary Axis twinx
Gridlines above bars
horizontal bar plot
seaborn style
Only y gridlines
horizontal bar plot
seaborn style
Only xgridlines
Axes.set_belowaxis(True)
horizontal bar plot
default style
Tick labels inside direction
Padding for tick labels
horizontal bar plot
Secondary Axis twinx
Gridlines below bars
Connected dot plots
default style
Using Circle patches
Note on Practical Implementation of tick related methods
227
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
plt.subplots(figsize=(6, 3))
sns.set_color_codes("pastel")
ax = sns.barplot(x=df.Completion, y=df.index, orient='h', joinstyle='bevel')
new_patches = []
for patch in reversed(ax.patches):
bb = patch.get_bbox()
color = patch.get_facecolor()
p_bbox = FancyBboxPatch((bb.xmin, bb.ymin),
abs(bb.width), abs(bb.height),
boxstyle="round,pad=-0.0040,rounding_size=0.015",
ec="none", fc=color,
mutation_aspect=4
)
patch.remove()
new_patches.append(p_bbox)
for patch in new_patches:
ax.add_patch(patch)
sns.despine(left=True, bottom=True)
ax.tick_params(axis=u'both', which=u'both', length=0) #Make ticklines zero length
plt.tight_layout()
ax.figure.savefig(f'Seaborn bar plot.png',dpi=150, format='png', bbox_inches='tight')
plt.show()
#https://stackoverflow.com/questions/61568935/seaborn-barplot-with-rounded-
corners
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from matplotlib.patches import FancyBboxPatch
plt.style.use("seaborn")
mydict = {
'Event': ['Running', 'Swimming', 'Biking', 'Hiking'],
'Completed': [2, 4, 3, 7],
'Participants': [10, 20, 35, 10]}
df = pd.DataFrame(mydict).set_index('Event')
df = df.assign(Completion=(df.Completed / df.Participants) * 100)
print(df)
Back to
Bar Charts List
228
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
plt.subplots(figsize=(6, 3))
sns.set_color_codes("pastel")
ax = sns.barplot(x=df.Completion, y=df.index, joinstyle='bevel')
new_patches = []
for patch in reversed(ax.patches):
# print(bb.xmin, bb.ymin,abs(bb.width), abs(bb.height))
bb = patch.get_bbox()
color = patch.get_facecolor()
p_bbox = FancyBboxPatch((bb.xmin, bb.ymin),
abs(bb.width), abs(bb.height),
boxstyle="round, pad=-0.0040, rounding_size=2",
ec="none", fc=color,
mutation_aspect=0.2
)
patch.remove()
new_patches.append(p_bbox)
for patch in new_patches:
ax.add_patch(patch)
sns.despine(left=True, bottom=True) #Removing all spines from plot
ax.grid(b = True, axis = 'x')
ax.tick_params(axis=u'both', which=u'both', length=0) #Make ticklines zero length
plt.tight_layout()
ax.figure.savefig(f'Seaborn_bar_plot_rounded_corners.png',dpi=300, format='png’,
bbox_inches='tight')
plt.show()
#https://stackoverflow.com/questions/61568935/seaborn-barplot-with-rounded-
corners
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from matplotlib.patches import FancyBboxPatch
plt.style.use("seaborn")
mydict = {
'Event': ['Running', 'Swimming', 'Biking', 'Hiking'],
'Completed': [2, 4, 3, 7],
'Participants': [10, 20, 35, 10]}
df = pd.DataFrame(mydict).set_index('Event')
df = df.assign(Completion=(df.Completed / df.Participants) * 100)
print(df) Back to
Bar Charts List
229
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
# Hack of adding dummy row to create spacing between bars
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
mydict = {
'Event': ['Running', 'Swimming', 'Biking', '', 'Hiking', 'Jogging'],
'Completed': [2, 4, 3, 0, 7, 9],
'Participants': [10, 20, 35, 0, 10, 20]}
df = pd.DataFrame(mydict).set_index('Event')
df = df.assign(Completion=(df.Completed / df.Participants) * 100)
plt.subplots(figsize=(5, 4))
print(df.index)
ax = sns.barplot(x=df.Completion, y=df.index, color="orange", orient='h')
plt.xticks(rotation=60). #Rotating x ticks by 60
plt.tight_layout()
ax.figure.savefig('Matplotlib Spacing between bars_dummy data.png',dpi=150,
bbox_inches='tight')
plt.show()
#https://stackoverflow.com/questions/61705783/matplotlib-control-spacing-
between-bars
#Matplotlib Control Spacing Between Bars
Back to
Bar Charts List
230
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
plt.style.use('seaborn’)
#define a function to add space starting a specific label
def add_space_after(ax, label_shift='', extra_space=0):
bool_space = False
label_list = [t.get_text() for t in ax.get_yticklabels()]
# get postion of current ticks
ticks_position = np.array(ax.get_yticks()).astype(float)
# iterate over the boxes/ticks/tick label and change the y position
for i, (patch, label) in enumerate(zip(ax.patches, ax.get_yticklabels())):
# if the label to start the shift found
if label.get_text()==label_shift: bool_space = True
# reposition the boxes and the labels afterward
if bool_space:
patch.set_y(patch.get_y() + extra_space)
ticks_position[i] += extra_space
# in the case where the spacing is needed
if bool_space:
print(f'ytick positions after: {ax.get_yticks()} nytick labels after: {ax.get_yticklabels()}’)
#ax.yaxis.set_major_locator(ticker.FixedLocator(ticks_position))
ax.set_yticks(ticks_position)
ax.set_ylim([ax.get_ylim()[0]+extra_space, ax.get_ylim()[1]])
print(f'ytick positions after: {ax.get_yticks()} nytick labels after: {ax.get_yticklabels()}’)
ax.yaxis.set_major_formatter(ticker.FixedFormatter(label_list))
fig, ax = plt.subplots(figsize = (7,4))
sns.barplot(x=df.Completion, y=df.index, color="orange", ax = ax, orient='h' )
plt.xticks(rotation=60) #Rotating x ticks by 60
plt.tight_layout()
#use the function
add_space_after(ax, 'Hiking', 0.6)
ax.figure.savefig(f'Matplotlib Spacing between bars.png',dpi=150, bbox_inches='tight')
#https://stackoverflow.com/questions/61705783/matplotlib-control-spacing-
between-bars
#Matplotlib Control Spacing Between Bars
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import matplotlib.ticker as ticker
mydict = {
'Event': ['Running', 'Swimming', 'Biking', 'Hiking', 'Jogging'],
'Completed': [2, 4, 3, 7, 9],
'Participants': [10, 20, 35, 10, 20]}
df = pd.DataFrame(mydict).set_index('Event')
df = df.assign(Completion=(df.Completed / df.Participants) * 100)
#note: no more blank row
Back to Bar Charts List
231
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
#https://stackoverflow.com/questions/49335512/ticklabels-inside-a-plot-in-matplotlib
#https://stackoverflow.com/questions/14800973/matplotlib-tick-labels-position-relative-to-axes
#https://matplotlib.org/stable/gallery/lines_bars_and_markers/barh.html
plt.rcdefaults()
fig, ax = plt.subplots()
# Example data
people = ('Tom', 'Dick', 'Harry', 'Slim', 'Jim')
y_pos = np.arange(len(people))
performance = 3 + 10 * np.random.rand(len(people))
error = np.random.rand(len(people))
ax.barh(y_pos, performance, xerr=error, align='center’, color='green', ecolor='black')
ax.set_yticks(y_pos)
ax.set_yticklabels(people)
ax.invert_yaxis() # labels read top-to-bottom
ax.set_xlabel('Performance')
ax.set_title('How fast do you want to go today?')
fig.savefig('BasicHorizontalBarplot.png', dpi = 300, bbox_inches='tight')
plt.show()
Back to
Bar Charts List
232
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
#https://stackoverflow.com/questions/49335512/ticklabels-inside-a-plot-in-matplotlib
#https://stackoverflow.com/questions/14800973/matplotlib-tick-labels-position-relative-to-axes
#https://matplotlib.org/stable/gallery/lines_bars_and_markers/barh.html
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
np.random.seed(123456) # Fixing random state for reproducibility
plt.style.use('seaborn')
fig, ax = plt.subplots(figsize = (6,4))
# Example data
people = ('Tom', 'Dick', 'Harry', 'Slim', 'Jim')
y_pos = np.arange(len(people))
performance = 3 + 10 * np.random.rand(len(people))
error = np.random.rand(len(people))
ax.barh(y_pos, performance, xerr=error, align='center', color='green',alpha = 0.8, ecolor='black')
ax.set_yticks(y_pos)
ax.set_yticklabels(people, ha = 'right')
ax.invert_yaxis() # labels read top-to-bottom
ax.set_xlabel('Performance')
ax.set_title('How fast do you want to go today?’)
[tkl.set(fontweight = 'bold') for tkl in ax.get_yticklabels()] #Making tick labels bold
ax.tick_params(axis = 'both', labelcolor = 'black', labelsize = 10) #Setting ticklabel size and color
ax.grid(axis = 'y', b = True). #Setting on gridlines from x axis
ax.grid(axis = 'x', b = False) #Setting off gridlines from x axis
fig.savefig('SeabornHorizontalBarplot_textbold.png', dpi = 300, bbox_inches='tight')
plt.show()
Back to
Bar Charts List
Note on Practical Implementation of tick related methods
233
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
#https://stackoverflow.com/questions/49335512/ticklabels-inside-a-plot-in-matplotlib
#https://stackoverflow.com/questions/14800973/matplotlib-tick-labels-position-relative-to-axes
#https://matplotlib.org/stable/gallery/lines_bars_and_markers/barh.html
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
np.random.seed(123456) # Fixing random state for reproducibility
plt.style.use('seaborn')
fig, ax = plt.subplots(figsize = (6,4))
# Example data
people = ('Tom', 'Dick', 'Harry', 'Slim', 'Jim')
y_pos = np.arange(len(people))
performance = 3 + 10 * np.random.rand(len(people))
error = np.random.rand(len(people))
ax.barh(y_pos, performance, xerr=error, align='center’, color='green',alpha = 0.8, ecolor='black')
ax.set_yticks(y_pos)
ax.set_yticklabels(people, ha = 'right')
ax.invert_yaxis() # labels read top-to-bottom
ax.set_xlabel('Performance')
ax.set_title('How fast do you want to go today?')
[tkl.set(fontweight = 'semibold') for tkl in ax.get_yticklabels()] #Making ticklabels semi bold
ax.tick_params(axis = 'y', labelcolor = 'black’) #Setting ticklabel size and color
ax.grid(axis = 'y', b = False)
stylize_axes(ax). # Using the user defined function stylize_axes
fig.savefig('SeabornHorizontalBarplot_textbold.png', dpi = 300, bbox_inches='tight')
plt.show()
#Creating a User Defined function
def stylize_axes(ax):
ax.set_facecolor('.8')
ax.tick_params(labelsize=10, length=0)
ax.grid(True, axis='x', color='white')
ax.set_axisbelow(True) #Ticks and gridlines below other artists
[spine.set_visible(False) for spine in ax.spines.values()]
Back to Bar Charts List
234
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
#https://stackoverflow.com/questions/49335512/ticklabels-inside-a-plot-in-matplotlib
#https://stackoverflow.com/questions/14800973/matplotlib-tick-labels-position-relative-to-axes
#https://matplotlib.org/stable/gallery/lines_bars_and_markers/barh.html
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
np.random.seed(123456)
plt.rcdefaults()
fig, ax = plt.subplots(figsize = (6,4))
# Example data
people = ('Tom', 'Dick', 'Harry', 'Slim', 'Jim')
y_pos = np.arange(len(people))
performance = 3 + 10 * np.random.rand(len(people))
error = np.random.rand(len(people))
plt.rcParams["axes.axisbelow"] = 'line’ #tick and gridlines above other artists except lines
#Can use the below line as well instead of above
#ax.set_axisbelow(False)
ax.barh(y_pos, performance, xerr=error, align='center’, color='green', ecolor='black’)
ax.invert_yaxis() # labels read top-to-bottom
ax.set_xlabel('Performance')
ax.set_title('How fast do you want to go today?’)
ax.set_yticks(y_pos)
ax.set_yticklabels(people, horizontalalignment = 'left', fontweight = 'medium’) #ha is important
ax.tick_params(axis = 'y', pad = -20, labelcolor = 'white’) #pad as -20 for ticklabels inside
plt.show()
• https://matplotlib.org/stable/gallery/misc/zorder_demo.html?highli
ght=zorder
• https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.set
_axisbelow.html?highlight=axisbelow#matplotlib.axes.Axes.set_axi
sbelow
• https://stackoverflow.com/questions/48327099/is-it-possible-to-set-
higher-z-index-of-the-axis-labels
Axes.set_axisbelow(self, b) : Set whether axis ticks and gridlines
are above or below most artists. This controls the zorder of the
ticks and gridlines.
Back to Bar Charts List
235
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
#https://stackoverflow.com/questions/52171321/
# creating-alternative-y-axis-labels-for-a-horizontal-bar-plot-with-matplotlib/52171704
#Check answer by Sheldore
import matplotlib.pyplot as plt
plt.rcdefaults()
fig, ax = plt.subplots(figsize = (6,4))
y = ['Physics','Chemistry','Economics','Philosophy','Computer
Science','Sociology','Biology','History','Overall']
x = [0.0,33.333333333333336,50.0,50.0,50.0,54.54545454545455,58.333333333333336,100.0,
51.851851851851855]
alternativeYlabels = ['54', '1', '3', '12', '12', '2', '11', '12', '1']
plt.barh(y,x)
plt.title('Percent Completion Rate of Each Course')
ax = plt.gca()
ax.xaxis.grid(True) # adding vertical grid lines
ax.set_axisbelow(‘line’) #Setting Ticks and gridlines above all Artists except lines
# Creating the right hand side y axis
ax2 = ax.twinx()
ax_lim = ax.get_ylim()
ax2_lim = (ax_lim[0], ax_lim[1]) # Aligning the limits of both y axes
ax2.set_ylim(ax2_lim)
ax2.set_yticks(range(0, len(y)))
ax2.set_yticklabels(alternativeYlabels[::-1]) # Reverses the list
ax2.set_ylabel("# enrolled in each course",rotation = 270, labelpad=15)
fig.savefig('SecondaryAxisBarplot_2.png', dpi = 300, bbox_inches='tight')
plt.show()
Creating alternative y axis labels
for a horizontal bar plot with matplotlib
• Axes.twinx
• (grid lines above bars)
Back to Bar Charts List
236
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
#https://stackoverflow.com/questions/52171321/
#creating-alternative-y-axis-labels-for-a-horizontal-bar-plot-with-matplotlib/52171704
#Check answer by Sheldore
import matplotlib.pyplot as plt
plt.rcdefaults()
fig, ax = plt.subplots(figsize = (6,4))
y = ['Physics','Chemistry','Economics','Philosophy','Computer
Science','Sociology','Biology','History','Overall']
x = [0.0,33.333333333333336,50.0,50.0,50.0,54.54545454545455,58.333333333333336,100.0,
51.851851851851855]
alternativeYlabels = ['54', '1', '3', '12', '12', '2', '11', '12', '1']
plt.barh(y,x)
plt.title('Percent Completion Rate of Each Course')
ax = plt.gca()
ax.xaxis.grid(True) # adding vertical grid lines
ax.yaxis.grid(True, alpha = 0.5) # adding horizontal grid lines
ax.set_axisbelow(True) #Setting Ticks and gridlines below all Artists.
# Creating the right hand side y axis
ax2 = ax.twinx()
ax_lim = ax.get_ylim()
ax2_lim = (ax_lim[0], ax_lim[1]) # Aligning the limits of both y axes
ax2.set_ylim(ax2_lim)
ax2.set_yticks(range(0, len(y)))
ax2.set_yticklabels(alternativeYlabels[::-1]) # Reverses the list
ax2.set_ylabel("# enrolled in each course",rotation = 270, labelpad=15)
fig.savefig('SecondaryAxisBarplot_2.png', dpi = 300, bbox_inches='tight')
plt.show()
Creating alternative y axis labels
for a horizontal bar plot with matplotlib
• Axes.twinx
• (grid lines below bars)
Back to Bar Charts List
237
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
#https://www.reddit.com/r/matplotlib/comments/i3t5uv/any_example_code_or_tips_for_h
ow_to_make_this/
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.patches as mpatches
# Fixing random state for reproducibility
np.random.seed(19680801)
plt.rcdefaults()
fig, ax = plt.subplots()
# Example data
people = ('Tom', 'Dick', 'Harry', 'Slim', 'Jim','John')
y_pos = np.arange(len(people))
performance = 3 + 10 * np.random.rand(len(people))
error = np.random.rand(len(people))
start = 3+np.random.rand(len(people))
ax.barh(y_pos, performance, left=start, height=0.03, align='center')
ax.set_yticks(y_pos)
ax.set_yticklabels(people)
ax.invert_yaxis() # labels read top-to-bottom
ax.set_xlabel('Performance')
ax.set_title('How fast do you want to go today?')
ax.xaxis.grid(b=True, alpha = 0.6)
ax.set_axisbelow(True). #Tick and Gridlines below other artists
for x,y,x1 in zip(start, y_pos, performance):
circ = mpatches.Circle((x,y),0.2,fc='red’) #Adding red circles for start position
ax.add_patch(circ)
circ = mpatches.Circle((x+x1,y),0.2,fc='blue’) #Adding blue circles for start position
ax.add_patch(circ)
plt.axis('scaled')
fig.savefig('Different Styles_Dot_Plot.png', dpi = 300, format = 'png',bbox_inches='tight')
plt.show()
Using seaborn plot style, code line :
plt.style.use(‘seaborn’)
plt.rcdefaults()
Cleveland Dot plots Using Circle Patches
Back to Bar Charts List
238
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Cleveland Dot plots
Using Circle Patches
Multiple Plot Styles
239
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
# Example data
people = ('Tom', 'Dick', 'Harry', 'Slim', 'Jim','John')
y_pos = np.arange(len(people))
performance = 3 + 10 * np.random.rand(len(people))
error = np.random.rand(len(people))
start = 3+np.random.rand(len(people))
fig = plt.figure(figsize = (25,9))
for i,ax in enumerate(styles):
with plt.style.context(styles[i]):
print(styles[i])
ax = fig.add_subplot(2,3,i+1)
ax.barh(y_pos, performance, left=start, height=0.03, align='center')
ax.set_yticks(y_pos)
ax.set_yticklabels(people,**font_tick) #Setting yticklabel props using ** unpacking
[tkl.set(**font_tick) for tkl in ax.get_xticklabels()] #Setting xticklabel props using ** unpacking
ax.invert_yaxis() # labels read top-to-bottom
ax.set_xlabel('Performance', **font_text)
ax.set_title('How fast do you want to go today?', loc = 'left', fontdict = font_title)
#ax.grid(b=True)
for x,y,x1 in zip(start, y_pos, performance): #Adding Circle Patches
circ = Circle((x,y),0.2,fc='red', transform = ax.transData)
ax.add_patch(circ)
circ = Circle((x+x1,y),0.2,fc='blue',transform = ax.transData)
ax.add_patch(circ)
ax.axis('scaled')
#Adding name of the plot style for the subplot within fancy bbox
ax.text(0.99,1.05,s = f'{styles[i]}',ha = 'right',transform = ax.transAxes, **font_text,
bbox=dict(facecolor='lightyellow', edgecolor='green', boxstyle='round',pad =.3))
plt.subplots_adjust(wspace = .2, hspace = .1) #Adjust horizontal & vertical space btw subplots
fig.savefig('Different Styles_Dot_Plot_fancybox_text.png', dpi = 300, bbox_inches='tight')
#https://www.reddit.com/r/matplotlib/comments/i3t5uv/any_example_cod
e_or_tips_for_how_to_make_this/
import matplotlib.pyplot as plt
from matplotlib.patches import Circle
import numpy as np
# Fixing random state for reproducibility
np.random.seed(19680801)
#Listing the styles for plotting with for loop
styles = ['bmh', 'Solarize_Light2','seaborn', 'fast',
'seaborn-darkgrid','seaborn-whitegrid’]
#Creating font dicts for different use cases
font_text = {'family': 'EB Garamond',
'color': 'darkred', 'weight': 'normal’,'size': 16} #for label plot style
font_title = {'family': 'Dejavu Sans',
'color': 'darkred', 'weight': 'normal','size': 14} #for axes title
font_tick = {'family': 'Dejavu Sans',
'color': 'darkred’, 'weight': 'normal’, 'size': 12} #for tick labels
240
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
# libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
plt.rcdefaults()
# Create a dataframe
value1=np.random.uniform(size=20)
value2=value1+np.random.uniform(size=20)/4
df = pd.DataFrame({'group':list(map(chr, range(65, 85))), 'value1':value1 , 'value2':value2 })
# Reorder it following the values of the first value:
ordered_df = df.sort_values(by='value1')
my_range=range(1,len(df.index)+1)
fig, ax = plt.subplots(figsize = (8,6))
# The horizontal plot is made using the hline function
ax.hlines(y=my_range, xmin=ordered_df['value1'], xmax=ordered_df['value2'], color='grey’,
alpha=0.4)
ax.scatter(ordered_df['value1'], my_range, color='skyblue', alpha=1, label='value1', s = 90)
ax.scatter(ordered_df['value2'], my_range, color='green', alpha=0.4 , label='value2', s= 90)
ax.legend()
# Add title and axis names
ax.set_yticks(my_range)
ax.set_yticklabels(ordered_df['group'])
ax.set_title("Comparison of the value 1 and the value 2", loc='left')
ax.set_xlabel('Value of the variables')
ax.set_ylabel('Group')
# Configuring the grid lines
ax.grid(True, alpha = 0.6, ls = 'dashed')
ax.set_axisbelow(True)
fig.savefig('lollipop_charts.png', dpi = 300, format = 'png',bbox_inches='tight')
plt.show() #Show the graph
#https://www.python-graph-gallery.com/184-lollipop-plot-with-2-groups
Changes done in the code in link :
• Changed pyplot interface style to OO style
• Configured the grid lines to dashed line style, set_axisbelow to put
ticks/gridlines below other artists
• Changed the scatter marker size to 90
Cleveland
Dot plots
• Using Axes Methods
• Axes.Scatter and Axes.hlines
241
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Using matplotlib in Python
Cleveland Dot plots
242
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
# libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.transforms as transforms
import seaborn as sns
import io
sns.set(style="whitegrid") # set style
#plt.style.use('seaborn-whitegrid')
data = io.StringIO(""""Country" 1990 2015
"Russia" 71.5 101.4
"Canada" 74.4 102.9
"Other non-OECD Europe/Eurasia" 60.9 135.2
"South Korea" 127.0 136.2
"China" 58.5 137.1
"Middle East" 170.9 158.8
"United States" 106.8 169.0
"Australia/New Zealand" 123.6 170.9
"Brazil" 208.5 199.8
"Japan" 181.0 216.7
"Africa" 185.4 222.0
"Other non-OECD Asia" 202.7 236.0
"OECD Europe" 173.8 239.9
"Other non-OECD Americas" 193.1 242.3
"India" 173.8 260.6
"Mexico/Chile" 221.1 269.8""")
df = pd.read_csv(data, sep="s+", quotechar='"')
df = df.set_index("Country").sort_values("2015")
df["change"] = df["2015"] / df["1990"] - 1
font_suptitle = {'fontsize' : 16}
#https://stats.stackexchange.com/questions/423735/what-is-the-name-of-
this-plot-that-has-rows-with-two-connected-dots/423861
#http://thewhyaxis.info/gap-remake/ #Dot plots in Excel
#https://www.eia.gov/outlooks/ieo/pdf/0484%282016%29.pdf
Cleveland
Dot plots
• Using Axes Methods
• Axes.Scatter and Axes.hlines
Code snippet 1 of 2
Code continues to Next slide
Code Start
243
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
fig = plt.figure(figsize=(13,5))
ax = fig.add_subplot()
y_range = np.arange(1, len(df.index) + 1)
colors = np.where(df['2015'] > df['1990'], '#d9d9d9', '#d57883')
ax.hlines(y=y_range, xmin=df['1990'], xmax=df['2015'],
color=colors, lw=5)
ax.scatter(df['1990'], y_range, color='#0096d7', s=150, label='1990', zorder=3)
ax.scatter(df['2015'], y_range, color='#003953', s=150 , label='2015', zorder=3)
for (_, row), y in zip(df.iterrows(), y_range):
ax.annotate(f"{row['change']:+.0%}", (max(row["1990"], row["2015"]) + 6, y - 0.25))
ax.grid(True,alpha = 0.6) #Make grid lines lighter
ax.set_axisbelow(False)
# Make all spines visible
ax.spines['right'].set(visible = True)
ax.spines['top'].set(visible =True)
ax.spines['left'].set(visible =True)
ax.spines['right'].set(visible =True)
ax.set_yticks(y_range)
ax.set_yticklabels(df.index)
ax.legend(ncol=2, bbox_to_anchor=(1, 1), loc="lower right", frameon=True )
# the x coords of this transformation are data, and the y coord are figure
trans = transforms.blended_transform_factory(ax.transData, fig.transFigure)
fig.suptitle("Energy productivity in selected countries and regions,1990 and 2015", **font_suptitle,
x = 50,y = 1, transform = trans,
ha = 'left', va = 'top')
ax.set_title("Billion dollars GDP per quadrillion BTU",loc = 'left')
ax.set_xlim(50, 300)
fig.subplots_adjust(left=0.35, top = 0.87)
fig.savefig('connected dot plots_sample2.png', dpi = 300, format = 'png’, bbox_inches='tight')
plt.show()
Cleveland
Dot plots
• Using Axes Methods
• Axes.Scatter and Axes.hlines
Code snippet 2 of 2
Code End
#https://stats.stackexchange.com/questions/423735/what-is-the-name-of-
this-plot-that-has-rows-with-two-connected-dots/423861
#http://thewhyaxis.info/gap-remake/ #Dot plots in Excel
#https://www.eia.gov/outlooks/ieo/pdf/0484%282016%29.pdf
Code continues from Prev slide
244
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Using ggplot2 in R
Cleveland Dot plots
245
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
# create the data frame # (in wide format, as needed for the line segments):
dat_wide = tibble::tribble( ~Country, ~Y1990, ~Y2015, 'Russia', 71.5, 101.4,
'Canada', 74.4, 102.9, 'Other non-OECD Europe/Eurasia', 60.9, 135.2, 'South Korea',
127, 136.2, 'China', 58.5, 137.1, 'Middle East', 170.9, 158.8, 'United States', 106.8,
169, 'Australia/New Zealand', 123.6, 170.9, 'Brazil', 208.5, 199.8, 'Japan', 181, 216.7,
'Africa', 185.4, 222, 'Other non-OECD Asia', 202.7, 236, 'OECD Europe', 173.8,
239.9, 'Other non-OECD Americas', 193.1, 242.3, 'India', 173.8, 260.6,
'Mexico/Chile', 221.1, 269.8 )
# a version reshaped to long format (for the points):
dat_long = dat_wide %>%
gather(key = 'Year', value = 'Energy_productivity', Y1990:Y2015) %>%
mutate(Year = str_replace(Year, 'Y', ’’))
# create the graph:
ggplot() +
geom_segment(data = dat_wide, aes(x = Y1990, xend = Y2015, y = reorder(Country,
Y2015), yend = reorder(Country, Y2015)), size = 3, colour = '#D0D0D0') +
geom_point(data = dat_long, aes(x = Energy_productivity, y = Country, colour =
Year), size = 4) +
labs(title = 'Energy productivity in selected countries nand regions', subtitle =
'Billion dollars GDP per quadrillion BTU', caption = 'Source: EIA, 2016', x = NULL, y
= NULL) +
scale_colour_manual(values = c('#1082CD', '#042B41')) +
theme_bw() +
theme(legend.position = c(0.92, 0.20), legend.title = element_blank(),
legend.box.background = element_rect(colour = 'black'), panel.border =
element_blank(), axis.ticks = element_line(colour = '#E6E6E6’))
ggsave('energy.png', width = 20, height = 10, units = 'cm')
#https://stats.stackexchange.com/questions/423735/what-is-the-name-of-
this-plot-that-has-rows-with-two-connected-dots/423861
library(dplyr) # for data manipulation
library(tidyr) # for reshaping the data frame
library(stringr) # string manipulation
library(ggplot2) # graphing
Cleveland
Dot plots
• Using ggplot2 in R
246
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
#https://stackoverflow.com/questions/61527817/
#formatting-date-labels-using-seaborn-facetgrid
#Check answer by Diziet Asahi
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from datetime import datetime
import matplotlib.dates as mdates
import random
datelist = pd.date_range(start="march 1 2020", end="may 20 2020", freq="w").tolist()
varlist = ["x", "y", "z", "x", "y", "z", "x", "y", "z", "x", "y", "z"]
deptlist = ["a", "a", "b", "a", "a", "b", "a", "a", "b", "a", "a", "b"]
vallist = random.sample(range(10, 30), 12)
df = pd.DataFrame({'date': datelist, 'value': vallist, 'variable': varlist,
'department': deptlist})
g = sns.FacetGrid(df, row="department", col="variable", sharey='row')
g = g.map(plt.plot, "date", "value", marker='o', markersize=0.7)
xformatter = mdates.DateFormatter("%m/%d")
g.axes[0,0].xaxis.set_major_formatter(xformatter)
g.axes[0,0].figure.savefig(f’Date labels Seaborn plot.png',dpi=300, format='png’,
bbox_inches='tight')
plt.show()
Seaborn
Facet Grids
• pd.date_range() to generate
datetimeindex
• DateFormatter for formatting xtick
labels
Note on Practical Implementation of tick related methods
247
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
#https://stackoverflow.com/questions/12945971/
#pandas-timeseries-plot-setting-x-axis-major-and-minor-ticks-and-labels?
#Check answer by bmu
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
plt.style.use('seaborn')
idx = pd.date_range('2011-05-01', '2011-07-01')
prng = np.random.default_rng(123456)
s = pd.Series(prng.standard_normal(len(idx)), index=idx)
fig, ax = plt.subplots()
ax.plot_date(idx.to_pydatetime(), s, 'v-') #x values converted to python datetime objects
ax.xaxis.set_minor_locator(mdates.WeekdayLocator(byweekday=(1),interval=1))
ax.xaxis.set_minor_formatter(mdates.DateFormatter('%dn%a'))
ax.xaxis.grid(True, which="minor")
ax.yaxis.grid()
ax.xaxis.set_major_locator(mdates.MonthLocator())
ax.xaxis.set_major_formatter(mdates.DateFormatter('nnn%bn%Y'))
plt.tight_layout()
fig.savefig('Major and Minor date ticks in multiline.png',dpi=300, format='png’,
bbox_inches='tight')
dates
module
• pd.date_range() to generate datetime
index
• MonthLocator() for major ticks
• WeekdayLocator() for minor ticks
• Date Formatter
Note on Practical Implementation of tick related methods
248
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
import matplotlib.pyplot as plt
from matplotlib.dates import MonthLocator, YearLocator
import numpy as np
plt.rcdefaults()
fig, ax = plt.subplots(1, 1, figsize = (10,5))
#Using np.arange to create a range of datetime64 values
x = np.arange('2010-01', '2021-07', dtype='datetime64[D]')
# np.random.seed(12345)
#y = np.random.randn(x.size).cumsum()
# Using the generator np.random.default_rng to generate values from standard normal
rng = np.random.default_rng(12345)
y = rng.standard_normal(x.size).cumsum()
ax.plot(x, y)
yloc = YearLocator()
mloc = MonthLocator()
ax.xaxis.set_major_locator(yloc)
ax.xaxis.set_minor_locator(mloc)
ax.xaxis.set_tick_params(rotation = 30)
[tkl.set_ha('right') for tkl in ax.get_xticklabels()]
ax.grid(True)
savekargs = dict(dpi = 300, transparent = True, pad_inches = 0.5, bbox_inches = 'tight')
fig.savefig('Numpy_datetime64_plotting.png', **savekargs)
Numpy
datetime64
• Using np.arange to generate array of
datetime64[D] values
• Using generator np.random.default_range
• YearLocator and MonthLocator Classes
Note on Practical Implementation of tick related methods
249
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
#https://stackoverflow.com/questions/48790378/how-to-get-ticks-every-hour
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
style = ['fivethirtyeight', 'seaborn', 'ggplot', 'default']
plt.style.use(style[3])
idx = pd.date_range('2017-01-01 05:03', '2017-01-01 18:03', freq = 'min')
df = pd.Series(np.random.randn(len(idx)), index = idx)
fig, ax = plt.subplots(figsize = (10,5))
hours = mdates.HourLocator(interval = 1)
h_fmt = mdates.DateFormatter('%H:%M:%S')
ax.plot(df.index, df.values, color = 'black', linewidth = 0.4)
#or use
#df.plot(ax = ax, color = 'black', linewidth = 0.4, x_compat=True, figsize = (10,5))
#Then tick and format with matplotlib:
ax.xaxis.set_major_locator(hours)
ax.xaxis.set_major_formatter(h_fmt)
fig.autofmt_xdate()
savekargs = dict(dpi = 300, transparent = True, pad_inches = 0.5, bbox_inches = 'tight')
fig.savefig('datetimeindex_plotting.png', **savekargs)
plt.show()
Datetime
index
Pandas
Tseries
Plotting
• Suppressing Tick Resolution Adjustment by
Pandas plotting
• x_compat = True to apply matplotlib.dates
locator/formatter
• Ticks at 1 hour interval
Note on Practical Implementation of tick related methods
250
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
#https://stackoverflow.com/questions/38154489/
#force-pandas-xaxis-datetime-index-using-a-specific-format
import pandas as pd
from datetime import datetime
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
timeInd = pd.date_range(start = datetime(2016,4,17,23,0,0),
end = datetime(2016,4,20,1,0,0), freq = 'H')
d = {'data1': np.random.randn(len(timeInd)), 'data2': np.random.randn(len(timeInd)),
'data3': np.random.randn(len(timeInd))}
df = pd.DataFrame(data = d, index = timeInd)
plt.style.use('ggplot')
ax0 = df.plot(subplots=True, grid=True, x_compat=True, figsize = (10,6))
ax = plt.gca()
# set major ticks location every day
ax.xaxis.set_major_locator(mdates.DayLocator())
# set major ticks format
ax.xaxis.set_major_formatter(mdates.DateFormatter('nnn%d.%m.%Y'))
# set minor ticks location every two hours
ax.xaxis.set_minor_locator(mdates.HourLocator(interval=2))
# set minor ticks format
ax.xaxis.set_minor_formatter(mdates.DateFormatter('%H:%M:%S'))
for ax in ax0:
ax.legend(loc = 'upper right')
ax.figure.patch.set(visible = False)
# or just set together date and time for major ticks like
# ax.xaxis.set_major_formatter(mdates.DateFormatter('%d.%m.%Y %H:%M:%S'))
ax.figure.savefig('Pandasplotting_datetime_xcompat.png', dpi = 300, pad_inches = 0.02)
plt.show()
Pandas
Tseries
Plotting
• Suppressing Tick Resolution Adjustment by Pandas
plotting
• x_compat = True to apply matplotlib.dates
locator/formatter
• Ticks at 2 hour interval
Note on Practical Implementation of tick related methods
251
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
import matplotlib.dates as mdates
import matplotlib.pyplot as plt
import datetime as dt
fig,ax1 = plt.subplots()
ax1.plot(df.index,df.values)
ax1.xaxis.set_major_locator(mdates.MonthLocator())
monthyearFmt = mdates.DateFormatter('%Yn%b')
ax1.xaxis.set_major_formatter(monthyearFmt)
#Underscore and assignment so that tick locs and labels are not shown in output
_ = plt.xticks(ha = 'center’)
plt.grid(axis = 'x')
x1 = dt.datetime(2020,0o5,0o1)
x2 = dt.datetime(2020,0o7,0o1)
ax1.set_ylim(0, ax1.get_ylim()[1])
ax1.fill_betweenx(y = (ax1.get_ylim()[0],ax1.get_ylim()[1]), x1 = x1, x2=x2, alpha = 0.3,
color = 'steelblue' )
x3 = dt.datetime(2020,9,0o1)
x4 = dt.datetime(2020,0o10,0o1)
ax1.axvspan(xmin = x3, xmax = x4, color = 'salmon', alpha = 0.8)
fig.savefig('Simple TimeSeries plot.png', dpi = 300, transparent = True)
TimeSeries
Plotting
• pd.date_range()
• dt.datetime
• np.random.default_rng(12345)
• fill_betweenx()
• Axvspan()
• MonthLocator()
Note on Practical Implementation of tick related methods
rng = np.random.default_rng(12345)
dtindex=pd.date_range(start='2020-01-01',end='2020-12-31',freq='M')
df = pd.DataFrame({'values':rng.integers(0,1000, len(dtindex))}, index= dtindex)
252
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import numpy as np
# create reproducible dataframe
dates = pd.date_range(start='2014-04-01', end='2021-04-07', freq='10min').tolist()
rng = np.random.default_rng(123456)
utilization = [rng.integers(10, 50) for _ in range(len(dates))]
df = pd.DataFrame({'dates': dates, 'util': utilization})
df.set_index('dates', inplace=True)
# resample dataframe to daily
df_daily = pd.DataFrame(df['util'].resample('D').sum())
#plot the dataframe
plt.style.use('seaborn-whitegrid')
fig = plt.figure(figsize=(10, 6))
ax = fig.add_subplot(111)
ax.plot(df_daily.index, df_daily['util'])
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m'))
fig.savefig('Resampling.png', dpi = 300, transparent = True)
plt.show()
Datetime
index
Time series
Plotting
• Resampling the dataframe to Day frequency
Note on Practical Implementation of tick related methods
https://pandas.pydata.org/docs/user_guide/timeseries.html#resampling
253
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
import seaborn as sns
np.random.seed(123456)
perf = np.cumprod(1+np.random.normal(size=250)/100)
df = pd.DataFrame({'underwater': perf/np.maximum.accumulate(perf)-1})
sns.set_context('notebook')
sns.set_style('ticks', rc = {'axes.grid': True, 'axes.spines.left': True,
'axes.spines.right': False, 'axes.spines.bottom': True,
'axes.spines.top': False})
#Assigning current axes to ax
ax = plt.gca()
df.plot.area(ax=ax, label = 'underwater')
ax.xaxis.set_ticklabels([])
ax.set_ylim(ax.get_ylim()[0],0)
#Removing spines at bottom and right
sns.despine(left=False, bottom=True, right=True, top = False, ax=ax)
#Adding legend with frameon
ax.legend(loc = 'upper right', bbox_to_anchor = (0.95,0.95),
bbox_transform = ax.transAxes, frameon = True )
# Adding red ticks in x axis pointing outwards
ax.tick_params('x', length=8, width=2, color='red', which='major', direction='out')
ax.figure.savefig('Major_ticks_Outer_No_AxisLabel.png',dpi=300, format='png',
bbox_inches='tight')
• sns.despine
• Adding red ticks in out direction
#https://stackoverflow.com/questions/57097467/
#Check answer by Sheldore
#Matplotlib x-axis at the top, no x axis label, no major tick labels, but outer
major and minor tick wanted
Note on Practical Implementation of tick related methods
Pandas
Seaborn
Matplotlib
254
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
#https://stackoverflow.com/questions/41924963/formatting-only-selected-tick-labels
import matplotlib.pyplot as plt
plt.rcdefaults()
fig, ax = plt.subplots()
ax.plot([1, 2, 3, 4, 5], [50, 40, 60, 70, 50])
ax.get_xticklabels()[-2].set_color("white")
ax.get_xticklabels()[-2].set_fontsize(14)
ax.get_xticklabels()[-2].set_weight("bold")
ax.get_xticklabels()[-2].set_bbox(dict(facecolor="red", alpha=0.9))
fig.savefig('Highlight selected ticks.png', dpi = 300, format = 'png',bbox_inches='tight’)
plt.show()
Format select
few tick labels
• Indexing/slicing axis tick labels
• Setting the bbox for text artist
matplotlib.patches.FancyBboxPatch
FancyBboxPatch Demo
Class Name Attrs
Circle circle pad=0.3
DArrow darrow pad=0.3
LArrow larrow pad=0.3
RArrow rarrow pad=0.3
Round round pad=0.3, rounding_size=None
Round4 round4 pad=0.3, rounding_size=None
Roundtooth roundtooth pad=0.3, tooth_size=None
Sawtooth sawtooth pad=0.3, tooth_size=None
Square square pad=0.3
boxstyle types
Note on Practical Implementation of tick related methods
255
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
#https://stackoverflow.com/questions/41924963/formatting-only-selected-tick-labels
#The above link contains only one tick formatted. The example is extended here for different
possible use-cases
plt.rcdefaults()
fig, ax = plt.subplots()
ax.plot([1, 2, 3, 4, 5], [50, 40, 60, 70, 50])
bbox_dict = dict(facecolor='red', alpha=0.9, ec = None, lw = 0, boxstyle = 'circle')
fontargs = dict(fontsize = 12, color = 'white', fontweight = 'bold', va = 'top’)
for tkl in ax.get_xticklabels(): #Iterating over the xaxis tick labels
#Setting the bbox with dict and dict unpacking
tkl.set(bbox = bbox_dict, **fontargs)
ax.xaxis.set_tick_params(pad = 10) #Increasing the padding between tick and labels
fig.savefig('Highlight selected ticks_2.png', dpi = 300,bbox_inches='tight', pad_inches = 0.3)
plt.show()
Format all tick
labels
• Iterate over axis tick labels
• Setting the bbox for text artist
• Set font properties
matplotlib.patches.FancyBboxPatch
Class Name Attrs
Circle circle pad=0.3
DArrow darrow pad=0.3
LArrow larrow pad=0.3
RArrow rarrow pad=0.3
Round round pad=0.3, rounding_size=None
Round4 round4 pad=0.3, rounding_size=None
Roundtooth roundtooth pad=0.3, tooth_size=None
Sawtooth sawtooth pad=0.3, tooth_size=None
Square square pad=0.3
boxstyle types
Note on Practical Implementation of tick related methods
256
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.colors import ListedColormap
from matplotlib.colors import LinearSegmentedColormap
from itertools import cycle, islice #Using iterator to cycle colors and boxstyle
import matplotlib.patches as mpatch
plt.rcdefaults()
fig, ax = plt.subplots()
ax.plot([1, 2, 3, 4, 5], [50, 40, 60, 70, 50])
#If more number of ticks present, we want box style to infinitely loop over style list
boxstyle_list = islice(cycle(mpatch.BoxStyle.get_styles()),0,None) #cycle with islice
#boxstyle_list = islice(cycle(['circle','darrow', 'rarrow', 'larrow', 'round', 'round4’,
'roundtooth’, 'square', 'sawtooth’])
cmap = 'inferno’ #color map
listedcolormap = ListedColormap(cm.get_cmap(cmap,256)(np.linspace(0,0.6,10)))
#Using islice to always start from first position
color_list = islice(cycle(listedcolormap.colors), 0, None)
#create fontargs and bboxargs to pass as arguments in ticklabel properties set
fontargs = {'color' :"white",'fontsize' :12, 'fontweight': "bold", 'ha':'center', 'va':'top'}
bboxargs = dict( alpha=0.9, ec = None, lw = 0)
ax.xaxis.set_tick_params(pad = 10) #Ensuring tick labels have sufficient gap from axis
for tkl in ax.get_xticklabels():
tkl.set(bbox = {**bboxargs, 'boxstyle' : next(boxstyle_list), 'facecolor':next(color_list)},
**fontargs )
fig.savefig('Highlight selected ticks_3.png', dpi = 300, format ='png’,bbox_inches='tight’,
pad_inches = 0.3)
plt.show()
• matplotlib.patches.FancyBboxPatch.html
• Formatting only selected tick labels
• specify-where-to-start-in-an-itertools-cycle-function
• fancybox_demo
• add-new-keys-to-a-dictionary
Format all tick
labels
• Use cycle iterator object with islice
• Set the bbox style with cycle
• Create listedcolormap from colormap and
use that within cycle object to assign color
to ticklabels
Note on Practical Implementation of tick related methods
257
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
import matplotlib.pyplot as plt
import matplotlib.transforms as mtransforms
import matplotlib.patches as mpatch
from matplotlib.patches import FancyBboxPatch
styles = mpatch.BoxStyle.get_styles()
spacing = 1.2
figheight = (spacing * len(styles) + .5)
fig = plt.figure(figsize=(4 / 1.5, figheight / 1.5))
fontsize = 0.3 * 72
for i, stylename in enumerate(sorted(styles)):
fig.text(0.5, (spacing * (len(styles) - i) - 0.5) / figheight,
stylename, ha="center", size=fontsize,
bbox=dict(boxstyle=stylename, fc="w", ec="k"))
• matplotlib.patches.FancyBboxPatch.html
• fancybox_demo
Patches
Class Name Attrs
Circle circle pad=0.3
DArrow darrow pad=0.3
LArrow larrow pad=0.3
RArrow rarrow pad=0.3
Round round pad=0.3, rounding_size=None
Round4 round4 pad=0.3, rounding_size=None
Roundtooth roundtooth pad=0.3, tooth_size=None
Sawtooth sawtooth pad=0.3, tooth_size=None
Square square pad=0.3
boxstyle types
• FancyBboxPatch
258
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(123456)
a = np.random.randint(5,15, size=10)
b = np.random.randint(5,15, size=10)
df = pd.DataFrame({"a":a})
df2 = pd.DataFrame({"b":b})
fig, (ax, ax2) = plt.subplots(ncols=2, sharey=True, figsize = (10,5) )
ax.invert_xaxis()
ax.yaxis.tick_right()
df["a"].plot(kind='barh', x='LABEL', legend=False, ax=ax)
df2["b"].plot(kind='barh', x='LABEL',ax=ax2)
fig.patch.set_visible(False)
ax.patch.set_visible(False)
ax2.patch.set_visible(False)
fig.savefig('Two barh adjacent.png', dpi = 300)
plt.show()
Adjacent
Barh charts
• https://stackoverflow.com/questions/44049132/python-pandas-plotting-two-
barh-side-by-side
Context of the Plot
• Two adjacent barh plots side-by-side sharing the y-axis
• Remove the label in y-axis for the lefthand side chart
• Xaxis of the two plots should start from the middle of the two plots
• Pandas DataFrame + Matplotlib
259
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
#https://sharkcoder.com/data-visualization/mpl-
bidirectional
import requests
import pandas as pd
url =
'https://en.wikipedia.org/wiki/List_of_countries_by_gover
nment_budget'
response = requests.get(url)
tables = pd.read_html(response.text)
tables[0].to_csv('data.csv’)
df = pd.read_csv('data.csv’)
data = df[['Country','Revenues','Expenditures']].head(10)
data.set_index('Country', inplace=True)
data['Revenues'] = data['Revenues'].astype(float)
data['Revenues'] = data['Revenues'] / 1000
data['Expenditures'] = data['Expenditures'].astype(float)
data['Expenditures'] = data['Expenditures'] / 1000 * (-1)
font_color = '#525252'
hfont = {'fontname':'Calibri’} # dict for unpacking later
facecolor = '#eaeaf2'
color_red = '#fd625e'
color_blue = '#01b8aa'
index = data.index
column0 = data['Expenditures']
column1 = data['Revenues']
title0 = 'Expenditures, USD thousand'
title1 = 'Revenues, USD thousand’
import matplotlib.pyplot as plt
plt.rcdefaults()
fig, axes = plt.subplots(figsize=(10,5), facecolor=facecolor,
ncols=2, sharey=True)
fig.tight_layout()
axes[0].barh(index, column0, align='center’,
color=color_red, zorder=10)
axes[0].set_title(title0, fontsize=18, pad=15, color= 
color_red, fontweight = 'medium', **hfont)
axes[1].barh(index, column1, align='center’,
color=color_blue, zorder=10)
axes[1].set_title(title1, fontsize=18, pad=15, color= 
color_blue, fontweight = 'medium', **hfont)
# If you have positive numbers and want to invert the x-axis
of the left plot
#axes[0].invert_xaxis()
# To show data from highest to lowest
plt.gca().invert_yaxis()
axes[0].set(yticks=data.index, yticklabels=data.index)
axes[0].yaxis.tick_left()
axes[0].tick_params(axis='y', colors='white') # tick color
axes[1].set_xticks([1000, 2000, 3000, 4000, 5000, 6000,
7000, 8000])
axes[1].yaxis.set_tick_params(size=0)
axes[1].set_xticklabels([1000, 2000, 3000, 4000, 5000, 6000,
7000,8000])
for label in (axes[0].get_xticklabels() +axes[0].get_yticklabels()):
label.set(fontsize=13, color=font_color, **hfont)
for label in (axes[1].get_xticklabels() +axes[1].get_yticklabels()):
label.set(fontsize=13, color=font_color, **hfont)
fig.patch.set(facecolor = 'white’)
#wspace=0 for no gap between the two axes
plt.subplots_adjust(wspace=0, top=0.85, bottom=0.1,
left=0.18, right=0.95)
[ax.grid(True) for ax in fig.axes]
filename = 'mpl-bidirectional2'
plt.savefig(filename+'.png', dpi=300, format='png’,
bbox_inches='tight')
Country Revenues Expenditures
United States 5923.829 -9818.53
China 3622.313 -5388.81
Germany 1729.224 -2038.25
Japan 1666.454 -2362.68
France 1334.944 -1609.71
United Kingdom 966.407 -1400.78
Italy 863.785 -1103.72
India 620.739 -940.771
Canada 598.434 -917.271
Spain 481.945 -657.75
Bi Directional Charts
The final data frame used for the plot after data aggregation
and manipulation is as below (can be used directly) :
Back
260
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
#https://stackoverflow.com/questions/41296313/stacked-bar-chart-with-centered-labels?
import numpy as np
import matplotlib.pyplot as plt
A = [45, 17, 47]
B = [91, 70, 72]
#Creating dictionary that contains keyword argument property values for text instance
text_kwargs = {'ha':"center", 'va':"center", 'color':"white", 'fontsize':16, 'fontweight':"bold"}
fig = plt.figure(facecolor="white")
ax = fig.add_subplot(1, 1, 1)
bar_width = 0.5
bar_l = np.arange(1, 4)
tick_pos = [i + (bar_width / 2) for i in bar_l]
ax1 = ax.bar(bar_l, A, width=bar_width, label="A", color="green")
ax2 = ax.bar(bar_l, B, bottom=A, width=bar_width, label="B", color="blue")
ax.set_ylabel("Count", fontsize=18)
ax.set_xlabel("Class", fontsize=18)
ax.legend(loc="best") #Adding Legend to “best” location
plt.xticks(tick_pos, ["C1", "C2", "C3"], fontsize=16)
plt.yticks(fontsize=16)
for r1, r2 in zip(ax1, ax2): #Adding data labels at the center of each of the bar patches
h1 = r1.get_height()
h2 = r2.get_height()
ax.text(r1.get_x() + r1.get_width() / 2., h1 / 2., "%d" % h1, **text_kwargs) #unpacking dict
ax.text(r2.get_x() + r2.get_width() / 2., h1 + h2 / 2., "%d" % h2, **text_kwargs)
fig.savefig('labels_stacked_bar_charts.png', dpi = 300, format = 'png’, bbox_inches='tight')
plt.show()
Stacked
Bar Charts
• Adding Labels to the bars
261
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
plt.style.use('ggplot')
text_kwargs = {'ha':"center", 'va':"center", 'color':"white",
'fontsize':18, 'fontweight':"semibold"}
ax = df.plot(stacked=True, kind='bar', figsize=(10, 6), rot='horizontal')
fig = ax.figure
# .patches gives acess to all the patches (bars)
for rect in ax.patches:
# Find where everything is located
height = rect.get_height()
width = rect.get_width()
x = rect.get_x()
y = rect.get_y()
# The height of the bar is the data value and can be used as the label
label_text = f'{height}' # f'{height:.2f}' to format decimal values
# ax.text(x, y, text)
label_x = x + width / 2
label_y = y + height / 2
# plot only when height is greater than specified value
if height > 0:
ax.text(label_x, label_y, label_text, **text_kwargs) #Dictionary style unpacking
ax.legend(bbox_to_anchor=(1.05, 1), loc='upper left', borderaxespad=0) #Adding Legend
ax.set_ylabel("Count", fontsize=18, labelpad = 10) #labelpad and fontsize changed
ax.set_xlabel("Class", fontsize=18, labelpad = 10) #labelpad and fontsize changed
ax.tick_params(labelsize = 16)
ax.set_ylim(0, ax.get_ylim()[1]*1.1) #Increasing the upper ylim to create spacing
fig.savefig('labels_stacked_bar_charts_ggplot.png', dpi = 300,
format = 'png',bbox_inches='tight')
plt.show()
#https://stackoverflow.com/questions/41296313/
#Check answer by Trenton McKinney
Below changes vis-a-vis above link
• Dictionary style unpacking of text keyword arguments
• Tick labelsize, Axis labelsize and labelpad
• Resetting yaxis limits
import pandas as pd
import matplotlib.pyplot as plt
#Data
A = [45, 17, 47]
B = [91, 70, 72]
C = [68, 43, 13]
# pandas dataframe
df = pd.DataFrame(data={'A': A, 'B': B, 'C': C})
df.index = ['C1', 'C2', 'C3']
Stacked
Bar Charts
ggplot style
262
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
# Pandas Bar Plotting with datetime in x axis
#https://stackoverflow.com/questions/30133280/pandas-bar-plot-changes-date-
format
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
plt.style.use('default')
# generate sample data
start = pd.to_datetime("1-1-2012")
index = pd.date_range(start, periods= 365)
prng = np.random.default_rng(123456)
df = pd.DataFrame({'A' : prng.random(365), 'B' : prng.random(365)}, index=index)
# resample to any timeframe you need, e.g. months
df_months = df.resample("M").sum()
# plot
fig, ax = plt.subplots()
df_months.plot(kind="bar", figsize=(16,5), stacked=True, ax=ax)
# Decorations/Embellishments
ax.grid(True, alpha = 0.5, ls = 'dotted')
ax.set_axisbelow(True)
# format xtick-labels with list comprehension
ax.set_xticklabels([x.strftime("%Y-%m") for x in df_months.index],ha = 'right', rotation=45)
fig.savefig('ticklabels_using timestamp.strftime.png', dpi = 300, bbox_inches = 'tight',
transparent = True)
plt.show()
Pandas
Stacked Bar
plots
• Datetime Index
• Using for timestamp.strftime for tick labels
• Using Resample to ‘Months’
263
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
#https://stackoverflow.com/questions/55642270/matplotlib-sharex-on-data-with-different-x-values
#Check answer by gmds
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
fig, axes = plt.subplots(2, sharex=‘all’)
ax1, ax2 = axes
df = pd.DataFrame({‘x’: [‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’], ‘y1’: np.arrange(6)})
df2 = pd.DataFrame({‘x’: [‘B’, ‘D’, ‘E’], ‘y2’: np.random.rand(3)})
combined = df.merge(df2, how=‘left’, on=‘x’)
ax1.bar(combined[‘x’], combined[‘y1’])
ax2.bar(combined[‘x’], combined[‘y2’])
fig.savefig(f’Share_x_across_subplots.png’,dpi=150, format=‘png’, bbox_inches='tight’)
plt.show()
Share-x on Data with different x values
#Alternately, one can also use Seaborn
df = pd.DataFrame({'x':['A','B','C','D','E','F'],'y1':np.random.rand(6)})
df2 = pd.DataFrame({'x':['B','D','E'],'y2':np.random.rand(3)})
order = np.unique(list(df.x)+list(df2.x))
fig,axes = plt.subplots(2, sharex='all')
sns.barplot(x='x',y='y1',data=df,ax=axes[0], order=order)
sns.barplot(x='x',y='y2',data=df2,ax=axes[1], order=order)
plt.show()
Seaborn
Matplotlib
264
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
plt.style.use(‘ggplot’)
# Preparing Sample data
N = 100
industry = ['a','b','c']
city = ['x','y','z']
ind = np.random.choice(industry, N)
cty = np.random.choice(city, N)
jobs = np.random.randint(low=1,high=250,size=N)
df_city =pd.DataFrame({'industry':ind,'city':cty,'jobs':jobs})
# Extracting the number of panels(cities) from df_city
cols =df_city.city.value_counts().shape[0]
fig, axes = plt.subplots(1, cols, figsize=(8, 8), sharey = True )
for x, city in enumerate(df_city.city.value_counts().index.values): #for loop to create bar plots
data = df_city[(df_city['city'] == city)]
data = data.groupby(['industry']).jobs.sum()
print (data)
print (type(data.index))
left= [k[0] for k in enumerate(data)]
right= [k[1] for k in enumerate(data)]
color = plt.cm.get_cmap('Set1').colors[x] #Access colors from Listed colormaps
axes[x].bar(left,right,label="%s" % (city), color = color)
axes[x].set_xticks(left, minor=False)
axes[x].set_xticklabels(data.index.values)
axes[x].legend(loc='best')
axes[x].grid(True)
fig.suptitle('Employment By Industry By City’, fontsize=20)
plt.subplots_adjust(wspace = 0.2)
fig.savefig(f'Multiple Subplots_shared_y.png',dpi=300, format='png', bbox_inches='tight')
Also check :
§ https://stackoverflow.com/questions/19613486/pandas
-matplotlib-faceting-bar-plots
Faceted Bar Charts
Pandas + Matplotlib
Similar to facet_wrap in ggplot2,
Facet_grid in Seaborn
265
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
#https://stackoverflow.com/questions/53577630/how-to-make-pareto-chart-in-
python
A Pareto Chart is a graph that indicates the frequency of defects, as well as
their cumulative impact. Pareto Charts are useful to find the defects to prioritize
in order to observe the greatest overall improvement.
Pareto chart contains both bars and a line graph, where individual values are
represented in descending order by bars, and the cumulative total is
represented by the line. The chart is named for the Pareto principle, which, in
turn, derives its name from Vilfredo Pareto, a noted Italian economist.
Pareto Chart
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.ticker import PercentFormatter
df = pd.DataFrame({'country': [177.0, 7.0, 4.0, 2.0, 2.0, 1.0, 1.0, 1.0]})
df.index = ['USA', 'Canada', 'Russia', 'UK', 'Belgium', 'Mexico', 'Germany’,
'Denmark']
df = df.sort_values(by='country',ascending=False)
df["cumpercentage"] = df["country"].cumsum()/df["country"].sum()*100
fig, ax = plt.subplots(figsize = (10,5))
ax.tick_params(axis="y", colors="C0",labelsize = 13)
ax.tick_params(axis="x", colors="0", rotation = 15)
ax2.tick_params(axis="y", colors="C1", labelsize = 13)
for labels in ax.get_xticklabels():
labels.set(size = 13,rotation = 0, rotation_mode = 'anchor’,
ha = 'center')
ax.figure.savefig(f'Pareto Chart.png',dpi=150, format='png’,
bbox_inches='tight')
ax.bar(df.index, df["country"], color="C0")
ax2 = ax.twinx() #Creating twin axes with shared x -axis
ax2.plot(df.index, df["cumpercentage"], color="C1", marker="D",
ms=7)
ax2.yaxis.set_major_formatter(PercentFormatter())
266
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
#https://matplotlib.org/stable/gallery/lines_bars_and_mar
kers/fill_between_alpha.html#sphx-glr-gallery-lines-bars-
and-markers-fill-between-alpha-py
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.cbook as cbook
# Fixing random state for reproducibility
np.random.seed(19680801)
# load up some sample financial data
r = (cbook.get_sample_data('goog.npz’,
np_load=True)['price_data'].view(np.recarray))
# create two subplots with the shared x and y axes
fig, ax1 = plt.subplots()
pricemin = r.close.min() #For entering as argument later
#Optional line plotting with thick lines
#ax1.plot(r.date, r.close, lw=2)
p = ax1.fill_between(r.date, pricemin, r.close,
facecolor='blue', alpha=0.5)
ax1.grid(True) #Make Gridlines visible
ax1.set_ylabel('price’)
#Make y ticklabels invisible
for label in ax1.get_yticklabels():
label.set_visible(False)
fig.suptitle('Google (GOOG) daily closing price')
fig.autofmt_xdate()
fig.savefig("Fill Area.png",dpi=300, format='png’,
bbox_inches='tight’)
plt.show()
#Above plot by uncommenting the optional line plotting
ax1.plot(r.date, r.close, lw=2)
Fill Between and Alpha
Back
267
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
#https://www.machinelearningplus.com/plots/top-50-
matplotlib-visualizations-the-master-plots-python/ - 14.-
Area-Chart
import pandas as pd
# Prepare Data
df =
pd.read_csv("https://github.com/selva86/datasets/raw/m
aster/mtcars.csv")
x = df.loc[:, ['mpg']]
df['mpg_z'] = (x - x.mean())/x.std()
df['colors'] = 'black'
# color fiat differently
df.loc[df.cars == 'Fiat X1-9', 'colors'] = 'darkorange'
df.sort_values('mpg_z', inplace=True)
df.reset_index(inplace=True)
# Draw plot
import matplotlib.patches as patches
fig, ax = plt.subplots(figsize=(10,12), dpi= 300)
plt.hlines(y=df.index, xmin=0, xmax=df.mpg_z,
color=df.colors, alpha=0.4, linewidth=1)
plt.scatter(df.mpg_z, df.index, color=df.colors,
s=[600 if x == 'Fiat X1-9' else 300 for x in
df.cars],
alpha=0.6)
plt.yticks(df.index, df.cars)
plt.xticks(fontsize=12)
# Annotate
plt.annotate('Mercedes Models', xy=(0.0, 11.0),
xytext=(1.0, 11), xycoords='data',
fontsize=15, ha='center', va='center',
bbox=dict(boxstyle='square', fc='firebrick'),
arrowprops=dict(arrowstyle='-[, widthB=2.0,
lengthB=1.5', lw=2.0, color='steelblue’),
color='white')
# Add Patches
p1 = patches.Rectangle((-2.0, -1), width=.3, height=3,
alpha=.2, facecolor='red')
p2 = patches.Rectangle((1.5, 27), width=.8, height=5,
alpha=.2, facecolor='green’)
plt.gca().add_patch(p1)
plt.gca().add_patch(p2)
# Decorate
plt.title('Diverging Bars of Car Mileage’,
fontdict={'size':20})
plt.grid(linestyle='--', alpha=0.5)
fig.savefig("aesthetic plot.png",dpi=300, format='png’,
bbox_inches='tight')
plt.show()
Interesting Use Case
ü Horizontal lines
ü Scatter points
ü Annotations
ü Patches
ARTIST
Horizontal lines • Starts from the base z score of 0
and move in either direction
• Visually appealing and helps
mapping the car model label with
the scatter point
Scatter points The scatter points with size 300 for all
cars except Fiat X1-9. x position
indicates the z score in terms of
mileage.
Annotations To markout the Mercedes models
Patches To highlight the cars at each of the
extremes
Raw Data – Next Slide
Back
268
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
"mpg","cyl","disp","hp","drat","wt","qsec","vs","am","gear","carb","fast","cars","carname"
4.58257569495584,6,160,110,3.9,2.62,16.46,0,1,4,4,1,"Mazda RX4","Mazda RX4"
4.58257569495584,6,160,110,3.9,2.875,17.02,0,1,4,4,1,"Mazda RX4 Wag","Mazda RX4 Wag"
4.77493455452533,4,108,93,3.85,2.32,18.61,1,1,4,1,1,"Datsun 710","Datsun 710"
4.62601340248815,6,258,110,3.08,3.215,19.44,1,0,3,1,1,"Hornet 4 Drive","Hornet 4 Drive"
4.32434966208793,8,360,175,3.15,3.44,17.02,0,0,3,2,1,"Hornet Sportabout","Hornet Sportabout"
4.25440947723653,6,225,105,2.76,3.46,20.22,1,0,3,1,1,"Valiant","Valiant"
3.78153408023781,8,360,245,3.21,3.57,15.84,0,0,3,4,0,"Duster 360","Duster 360"
4.93963561409139,4,146.7,62,3.69,3.19,20,1,0,4,2,1,"Merc 240D","Merc 240D"
4.77493455452533,4,140.8,95,3.92,3.15,22.9,1,0,4,2,1,"Merc 230","Merc 230"
4.38178046004133,6,167.6,123,3.92,3.44,18.3,1,0,4,4,1,"Merc 280","Merc 280"
4.2190046219458,6,167.6,123,3.92,3.44,18.9,1,0,4,4,1,"Merc 280C","Merc 280C"
4.04969134626332,8,275.8,180,3.07,4.07,17.4,0,0,3,3,1,"Merc 450SE","Merc 450SE"
4.15932686861708,8,275.8,180,3.07,3.73,17.6,0,0,3,3,1,"Merc 450SL","Merc 450SL"
3.89871773792359,8,275.8,180,3.07,3.78,18,0,0,3,3,0,"Merc 450SLC","Merc 450SLC"
3.22490309931942,8,472,205,2.93,5.25,17.98,0,0,3,4,0,"Cadillac Fleetwood","Cadillac Fleetwood"
3.22490309931942,8,460,215,3,5.424,17.82,0,0,3,4,0,"Lincoln Continental","Lincoln Continental"
3.83405790253616,8,440,230,3.23,5.345,17.42,0,0,3,4,0,"Chrysler Imperial","Chrysler Imperial"
5.69209978830308,4,78.7,66,4.08,2.2,19.47,1,1,4,1,1,"Fiat 128","Fiat 128"
5.51361950083609,4,75.7,52,4.93,1.615,18.52,1,1,4,2,1,"Honda Civic","Honda Civic"
5.82237065120385,4,71.1,65,4.22,1.835,19.9,1,1,4,1,1,"Toyota Corolla","Toyota Corolla"
4.63680924774785,4,120.1,97,3.7,2.465,20.01,1,0,3,1,1,"Toyota Corona","Toyota Corona"
3.93700393700591,8,318,150,2.76,3.52,16.87,0,0,3,2,0,"Dodge Challenger","Dodge Challenger"
3.89871773792359,8,304,150,3.15,3.435,17.3,0,0,3,2,0,"AMC Javelin","AMC Javelin"
3.64691650576209,8,350,245,3.73,3.84,15.41,0,0,3,4,0,"Camaro Z28","Camaro Z28"
4.38178046004133,8,400,175,3.08,3.845,17.05,0,0,3,2,1,"Pontiac Firebird","Pontiac Firebird"
5.22494019104525,4,79,66,4.08,1.935,18.9,1,1,4,1,1,"Fiat X1-9","Fiat X1-9"
5.09901951359278,4,120.3,91,4.43,2.14,16.7,0,1,5,2,1,"Porsche 914-2","Porsche 914-2"
5.51361950083609,4,95.1,113,3.77,1.513,16.9,1,1,5,2,1,"Lotus Europa","Lotus Europa"
3.97492138287036,8,351,264,4.22,3.17,14.5,0,1,5,4,0,"Ford Pantera L","Ford Pantera L"
4.43846820423443,6,145,175,3.62,2.77,15.5,0,1,5,6,1,"Ferrari Dino","Ferrari Dino"
3.87298334620742,8,301,335,3.54,3.57,14.6,0,1,5,8,0,"Maserati Bora","Maserati Bora"
4.62601340248815,4,121,109,4.11,2.78,18.6,1,1,4,2,1,"Volvo 142E","Volvo 142E"
#https://www.machinelearningplus.com/plots/top-50-
matplotlib-visualizations-the-master-plots-python/ - 14.-Area-
Chart
Raw Data Source Link:
https://github.com/selva86/datasets/raw/master/mtcars.csv
Raw Data : Table on the right
• Copy onto excel sheet and save as csv. Use pd.read_csv in
jupyter notebook. `
• Alternately, copy onto excel sheet and Use Text to
columns in Data Tab. Proceed to R Studio for EDA in R. Raw Data
Back
269
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
from pandas import DataFrame
import matplotlib.pyplot as plt
import numpy as np
# setting font size to 30
#plt.style.use('ggplot')
plt.rcdefaults()
plt.rcParams.update({'font.size': 20})
#Preparing the sample data
a=np.array([[4,8,5,7,6],
[2,3,4,2,6],
[4,7,4,7,8],
[2,6,4,8,6],
[2,4,3,3,2]])
df=DataFrame(a, columns=['a','b','c','d','e’],
index=['Jan','Feb','March','April','May'])
r = df.plot(kind='bar’) #Using Pandas plotting function
#Turn off the minor ticks
plt.minorticks_off()
# Turn on the grid
#plt.grid(which='major', linestyle='-', linewidth='0.5’,
color='green')
#plt.grid(which='minor', linestyle=':', linewidth='0.5’,
color='black’)
#Display the y axis grid lines
plt.grid(True, axis = 'y', alpha = 0.95, ls = ':’)
#Instantiating axes object to use methods
ax = plt.gca()
#Adding Legend
ax.legend(bbox_to_anchor=(1.05, 0.5), loc='center left’,
fontsize = 15)
#Setting xtick label properties
[ticklabel.set(rotation =0, ha = ‘center’, fontsize = 15) for
ticklabel in ax.get_xticklabels()]
#Setting ytick parameters
ax.yaxis.set_tick_params(rotation =0, labelsize = 15 )
[ticks.set(alpha = 0.5) for ticks in ax.get_xticklabels()]
[ticks.set(alpha = 0.5) for ticks in ax.get_yticklabels()]
#Setting figure properties
fig = plt.gcf()
fig.set(figheight = 5, figwidth =8)
fig.savefig(“Barplot2.png”,dpi=300, format=‘png’,
bbox_inches=‘tight’)
plt.show()
Also check :
§ https://matplotlib.org/stable/gallery/lines_bars_and_m
arkers/barchart.html#sphx-glr-gallery-lines-bars-and-
markers-barchart-py
§ https://www.pythoncharts.com/matplotlib/grouped-
bar-charts-matplotlib/
§ To get a list of all containers in the axes, use
ax.containers
https://stackoverflow.com/questions/62189677/how-to-
get-a-barcontainer-object-from-an-axessubplot-object-
in-matplotllib
§ To get list of all artists in the axes, use ax.get_children()
§ To get list of the attributes and methods of axes
object, use print(dir(ax))
Back
Grouped Bar Chart
Pandas plotting
270
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
#https://stackoverflow.com/questions/23293011/how-to-plot-a-superimposed-bar-chart-using-
matplotlib-in-python
a = range(1,10)
b = range(4,13)
ind = np.arange(len(a))
fig = plt.figure()
ax = fig.add_subplot(111)
ax.bar(x=ind, height=a, width=0.35,align='center')
ax.bar(x=ind, height=b, width=0.35/3, align='center')
plt.xticks(ind, a)
plt.tight_layout()
fig.savefig('Bullet_chart.png', dpi = 300, transparent = True)
plt.show()
Matplotlib
Bar Plots
• Bars of different widths
271
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
from datetime import datetime
import matplotlib.pyplot as plt
from matplotlib.dates import (
DateFormatter, AutoDateLocator, AutoDateFormatter, datestr2num
)
days = [
'30/01/2019', '31/01/2019', '01/02/2019',
'02/02/2019', '03/02/2019', '04/02/2019'
]
adata = [1, 9, 10, 3, 7, 6]
bdata = [1, 2, 11, 3, 6, 2]
#converting/parsing string to python datetime object using strptime
#and then converting back to string using strftime
x = datestr2num([
datetime.strptime(day, '%d/%m/%Y').strftime('%m/%d/%Y')
for day in days
])
#Alternately instead of datestr2num, we can directly use date2num on the datettime object
#x = date2num([ datetime.strptime(day, '%d/%m/%Y') for day in days]
w = 0.10
fig = plt.figure(figsize=(8, 4))
ax = fig.add_subplot(111)
#align:'center' is important and width as 2*w in conjunction with x value ensures adjacent bars
ax.bar(x - w, adata, width=2 * w, color='g', align='center', tick_label=days, label = 'adata')
ax.bar(x + w, bdata, width=2 * w, color='r', align='center', tick_label=days, label = 'bdata')
ax.xaxis.set_major_locator(AutoDateLocator(minticks=3, interval_multiples=False))
ax.xaxis.set_major_formatter(DateFormatter("%d/%m/%y"))
ax.legend() #Adding legend - automatic detection of handles, labels
fig.savefig('Processing_dates_in_string_as_xaxis.png', dpi = 300)
plt.show()
#https://stackoverflow.com/questions/59738557/date-format-issues-in-
plot-ticks-with-matplotlib-dates-and-datestr2num
Context : datestr2num calls (wrapper) dateutil but currently does not
forward all keyword arguments. For instance, dayfirst is not forwarded
by datestr2num. Instead, month first is always considered. This creates
problems incase of ambiguous date formats. The date2strnum is not
robust to deal consistently with dates in string form. We therefore first
parse date string with Python datetime module functions, convert back
to date string with month first string format ('%m/%d/%Y') and then
pass it to datestr2num to convert to matplotlib dates.
Matplotlib
Parsing Date
strings
• To process strings, use datetime functions
• Convert string to datetime object using
strptime
• Convert datetime object back to string
using strftime
• Convert the string from previous step to
matplotlib.date using datestr2num
Code End
Code Start
272
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
import matplotlib.pyplot as plt
from matplotlib.dates import date2num
import datetime
import matplotlib.dates as mdates
x = [
datetime.datetime(2011, 1, 4, 0, 0),
datetime.datetime(2011, 1, 5, 0, 0),
datetime.datetime(2011, 1, 6, 0, 0)
]
x = date2num(x) #Converting datetime objects to matplotlib dates
y = [4, 9, 2]
z = [1, 2, 3]
k = [11, 12, 13]
w = 0.05
fig, ax = plt.subplots(figsize = (8,5))
bars1 = ax.bar(x-2*w, y, width=2*w, color='b', align='center', label = 'y')
bars2 = ax.bar(x, z, width=2*w, color='g', align='center', label = 'z')
bars3 = ax.bar(x+2*w, k, width=2*w, color='r', align='center', label = 'k')
ax.xaxis.set_major_locator(mdates.DayLocator())
formatter = mdates.DateFormatter('%d %bn%a')
ax.xaxis.set_major_formatter(formatter)
ax.legend()
def autolabel(rects):
for rect in rects:
h = rect.get_height()
ax.text(rect.get_x()+rect.get_width()/2., 1.01*h, '%d'%int(h),
ha='center', va='bottom')
autolabel(bars1)
autolabel(bars2)
autolabel(bars3)
plt.show()
#Check the above discussion link.
I have proposed a solution over here and extended the solution to
handle any number of groups. Follow the next few slides.
Context :
• Three Bar plots plotted for the same datetime index
• Each Bar plot corresponds to values for a a single group against
date values
• Ensure no overlap between the bars
• Bars should be symmetrically positioned in either side of date tick
value
Matplotlib
Multiple Group
Bar plots
• Date Formatting in x axis
• No overlapping between bars
Code End
Code Start
273
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm
from matplotlib.colors import ListedColormap
from matplotlib.colors import LinearSegmentedColormap
from itertools import cycle, islice #Using iterator to cycle colors and boxstyle
def get_colors(y_list, cmap = 'tab20c'):
"""
Args :
-----------
y_list : list containing multiple y series each representing a categorical variable to be
plotted
cmap = Use next(color_list) to get the next color in each iteration
Returns :
-----------
color_list : a cycler object using islice to infinitely loop over a list of colors but always start
from fixed position
"""
listedcolormap = ListedColormap(cm.get_cmap(cmap,256)(np.linspace(0,0.6,len(y_list))))
color_list = islice(cycle(listedcolormap.colors),0,None)
return color_list
#Check the discussion link
Context :
• Three Bar plots plotted for the same datetime index
• Each Bar plot corresponds to values for a a single group against date
values
• Ensure no overlap between the bars
• Bars should be symmetrically positioned in either side of date tick value
• Date time formatting in x axis
Below is the structure of the code script scalable to any number of groups.
Code start
Code continue Next slide
User defined function
1. Use cycler object to access colors in loop
2. Make bar plots for proper placement of bars
and date time formatting of x axis
User
Defined
Functions
3.make_barplots(
*args)
Call the
function
Matplotlib
Multiple Group
Bar plots
• Date Formatting in x axis
• No overlapping between bars
User Defined function for multiple scenarios
Slide 1 of 4
Scalable Solution for multiple scenarios
274
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
# Creating User defined Function for returning the desired plot
def make_barplots(x, y_list, y_labels = [], w = 0.05, cmap = 'tab20c', style = 'seaborn-
whitegrid'):
"""
Args :
-----------
x : list of x values of the data type matplotlib dates
y_list : list of groupwise values corresponding to the x values
y_labels : list of group labels in the format string
style : plot style to use
cmap : colormap name from the list of matplotlib colormaps
w : half of the width of bar (The relationship is Bar width = 2*w)
If the input data is a Dataframe df with index of type datetimeindex and we want the columns
to be plotted, then
below conversion methods helpful :
x = date2num(df.index)
y_list = df.transpose().to_numpy().tolist()
Returns :
-----------
fig : Figure containing the axes and barplot
ax : Axes contining the barplots
bars_list : List containing barcontainer objects
Raises :
-----------
Warning : Legend not shown if the y_labels argument is empty.
"””
Code continues
to Next slide
Code continues from Prev slide
cycler object to access colors in
in a loop
To Make bar plots
User Defined
Functions
make_barplots
(*args)
Call the
function
Slide 2 of 4
Scalable Solution for multiple scenarios
275
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
plt.style.use(style)
fig, ax = plt.subplots(figsize = (10,6))
bars_list = []
# Creating color_list as a cycler object for cycling through the colors
color_list = get_colors(y_list, cmap = cmap)
# Creating bar plots iteratively from the list y_list containing the list of values
for i,y in enumerate(y_list) :
n = len(y_list) # number of groups
p = n//2 # p to be used for finding the start location
x_offset = 2*w*n//2+w*n%2 # calculating offset from tick location to locate start location
# Plotting the bar plot for a single group
# The start location will be further offset
# by bar widths multiplied by the index position (i) of the group within the y_list
bars = ax.bar(x-x_offset+i*2*w, y, width=2*w, color=next(color_list), align='edge’)
bars_list.append(bars)
# Creating a function to add data labels above bars
def autolabel(rects):
for rect in rects:
h = rect.get_height()
ax.text(rect.get_x()+rect.get_width()/2., 1.01*h, '%d'%int(h),
ha='center', va='bottom’)
# Adding data labels to bars by enumerating over bar containers and
for i, bars in enumerate(bars_list) :
autolabel(bars)
# Setting label property of bars
# for automatic detection of handles with labels for legend
if len(y_labels) == len(y_list): #Only if labels are provided while calling function
bars.set_label(y_labels[i])
Logic for consistent placement of the bars
Defining the problem :
• A bar belonging to a group should appear at fixed position versus its xtick
• Bars of different group should not overlap
What we want ?
Symmetrical distribution of bar groups across a tick location.
The group of bar at each of the x axis value should be symmetrically
distributed around the tick. There should be equal no. of bar groups on either
side of the x-axis tick location.
How will we achieve ? -- It’s a problem of placement of bars
We will focus specifically on the x value of a bar plot for a single group at a
time.
Let’s further narrow it down to a single tick location.
Three locations are important :
§ Tick Location
§ Start of the first bar patch corresponding to a tick location
§ Actual x value of a bar patch belonging to a group
1. Tick Location is matplotlib date( a floating point) and is fixed in our context.
2. Start Location will be offset from Tick location depending on number of
bars (number of groups) for a single tick
3. For even number of groups lets consider 4, the start location will be offset
by the width of 2 bars.
4. For odd number of groups lets consider 5, the start location will be offset
by the width of 2.5 bars.
5. Actual x value will be offset from the Start location depending on the
group sequence
Code continues to Next slide
Code continues from Prev slide
LOGIC
cycler object to access colors in
in a loop
Make bar plots
User
Defined
Functions
make_barplots
(*args)
Call the
function
Slide 3 of 4
Scalable Solution for multiple scenarios
276
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
locator = mdates.DayLocator() #Creating locator object
formatter = mdates.DateFormatter('%d %bn%a’) #Creating locator object
ax.xaxis.set_major_locator(locator)
ax.xaxis.set_major_formatter(formatter)
ax.xaxis.grid(False) #Making the xgrid lines disappear
ax.set_ylim(ax.get_ylim()[0]*1.2,ax.get_ylim()[1]*1.1) #Resetting y limits for ample margins
if len(y_labels) == len(y_list):
#handles, labels = ax.get_legend_handles_labels()
#print(handles)
#print(labels)
ax.legend(loc = 'best')
else :
print('labels for the bars not entered. Hence, Legend missing.')
fig.savefig('Multiple_barsplot_with_xaxis_datetickers.png', dpi = 300)
return fig, ax, bars_list #returns figure, axes, list containing bar container objects
# Preparing the data and making bar plot by using the user defined function make_barplots()
import matplotlib.pyplot as plt
from matplotlib.dates import date2num
import datetime
import matplotlib.dates as mdates
x = [ datetime.datetime(2011, 1, 4, 0, 0), datetime.datetime(2011, 1, 5, 0, 0),
datetime.datetime(2011, 1, 6, 0, 0)]
x = date2num(x)
y = [4, 9, 2]
z = [1, 2, 3]
k = [11, 12, 13]
fig, ax, bars_list = make_barplots(x, y_list, y_labels, w = 0.1, style = 'default')
Code continues form prev slide
Code End
cycler object to access colors in
in a loop
Make bar plots
User
Defined
Functions
make_barplots
(*args)
Call the
function
Slide 4 of 4
Scalable Solution for multiple scenarios
277
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
import pandas as pd
import numpy as np
#Fixing state for reproducibility
np.random.seed(123456)
#Creating sample data for plotting using numpy and pandas
index = pd.date_range("1/1/2000", periods=8)
s = pd.Series(np.random.randn(5), index=["a", "b", "c", "d", "e"])
df = pd.DataFrame(np.random.randint(20,100, (8,4)), index=index, columns=["A", "B", "C", "D"])
#Make bar plots using the userdefined function make_barplots
make_barplots(x = date2num(df.index), y_list = df.transpose().to_numpy().tolist() , 
y_labels = df.columns, w = 0.1, style = ‘default’)
Creating DataFrames https://pandas.pydata.org/docs/user_guide/dsintro.html#dataframe
Create sample data
https://github.com/pandas-dev/pandas/blob/v1.3.1/pandas/core/frame.py#L456-L10748 line no. 1595
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_numpy.html#pandas.DataFrame.to_numpy
Pandas DataFrame columns are Pandas Series when
you pull them out, which you can then call x.tolist() on
to turn them into a Python list.
( df
.filter(['column_name’])
.values
.reshape(1, -1)
.ravel()
.tolist()
)
A B C D
2000-01-01 88 82 69 87
2000-01-02 35 47 88 24
2000-01-03 47 93 66 31
2000-01-04 51 56 65 71
2000-01-05 31 41 94 25
2000-01-06 29 84 60 91
2000-01-07 48 66 81 99
2000-01-08 84 96 75 52
2000-01-01 2000-01-02 2000-01-03 2000-01-04 2000-01-05 2000-01-06 2000-01-07 2000-01-08
A 88 35 47 51 31 29 48 84
B 82 47 93 56 41 84 66 96
C 69 88 66 65 94 60 81 75
D 87 24 31 71 25 91 99 52
DataFrame DataFrame - Transpose
Alternate way from DataFrame to List
Matplotlib
Multiple Group
Bar plots
• Date Formatting in x axis
• No overlapping between bars
• Number of bar groups = 4
Demo using the User defined function make_barplots
Code End
Code Start
278
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.ticker as ticker
plt.rcdefaults()
fig = plt.figure()
# generate sample data for this example
count = 12
xs = list(range(1,count+1))
prng = np.random.default_rng(123456)
ys_bars = prng.normal(loc=3.0,size=len(xs))
ys_lines = prng.normal(loc=5.0,size=len(xs),scale=0.5)
# this is the axes on the top
ax1= plt.subplot(211)
# plot the same numbers but multiplied by 20
ax1.plot(xs,ys_lines*20,color='red')
# order is important when setting ticks.
# Ticks must be set after the plot has been drawn
#ax1.set_yticks(np.arange(0,101,10))
ax1.yaxis.set_major_locator(ticker.MultipleLocator(10))
# set ticks for the common x axis (bottom)
ax1.xaxis.set_ticks(xs)
ax1.grid(True, alpha = 0.5)
ax1.fill_between(xs,y1 = ys_lines*20, alpha = 0.5, color = 'xkcd:carolina blue')
ax1.vlines(x=xs, ymin = 0, ymax = ys_lines*20, alpha = 0.5, lw = 0.3 )
ax2= plt.subplot(212)
ax2.bar(xs,ys_bars,color='red')
ax2.yaxis.grid(True, alpha = 0.5)
ax2.set_axisbelow(True)
ax2.set_xticks(xs)
ax2.yaxis.set_major_locator(ticker.MultipleLocator(1))
plt.subplots_adjust(hspace = 0.4)
fig.savefig('Simple LinePlots_BarPlots_sharedxaxis.png', dpi = 300, transparent = True)
Multiple Subplots
Line plot + Bar plot
Shared Axis
• Categorical Bar and Line plot across
multiple subplots
• Ticker.MultipleLocator(10)
• Axes.fill_between()
• Axes.vlines() Code End
Code Start
279
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
ax1.bar(xs,ys_bars,color = 'xkcd:lichen', alpha = 0.8)
# order is important when setting ticks.
# Ticks must be set after the plot has been drawn
ax1.set_yticks(np.arange(0,11,1))
# define the number of ticks
NUM_TICKS=11
# change the tick locator for this axis and set the desired number of ticks
ax1.yaxis.set_major_locator(plt.LinearLocator(numticks=NUM_TICKS))
# create the 'twin’ axes with the y axis on right
ax2=ax1.twinx()
# plot the same numbers but multiplied by 20
ax2.plot(xs,ys_lines*20,color='red')
# set the limits for the twin axis
ymax = (ax2.get_ylim()[1]//100+0.5)*100
ax2.set_ylim(0, ymax)
# change the tick locator for this axis and set the desired number of ticks
ax2.yaxis.set_major_locator(plt.LinearLocator(numticks=NUM_TICKS))
# set ticks for the common x axis (bottom)
ax2.xaxis.set_ticks(xs)
#Show yaxis gridlines
ax1.yaxis.grid(True)
ax1.set_axisbelow(True)
fig.savefig('Barplot_Lineplot.png', dpi = 300, transparent = False)
import matplotlib.pyplot as plt
import numpy as np
fig, ax1 = plt.subplots()
# generate sample data for this example
xs = list(range(1,13))
prng = np.random.default_rng(123456)
ys_bars = prng.normal(loc=3.0,size=12)
ys_lines = prng.normal(loc=5.0,size=12,scale=0.5)
Overlaying Charts
• Categorical Bar and Line plot on same Axes
• X axis is numeric but non datetime
• Twin Axes with shared x axis
• Aligning yaxis of the twin axes
Check https://queirozf.com/entries/matplotlib-pyplot-by-example
Note : Changes have been done visavis the code in above link.
Code End
Code Start
280
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Check https://queirozf.com/entries/matplotlib-pyplot-by-example
Note : Changes have been done visavis the code in above link.
ax1.bar(xs,ys_bars,color = 'xkcd:lichen')
# order is important when setting ticks.
# Ticks must be set after the plot has been drawn
ax1.set_yticks(np.arange(0,11,1))
# define the number of ticks
NUM_TICKS=11
# change the tick locator for this axis and set the desired number of ticks
ax1.yaxis.set_major_locator(plt.LinearLocator(numticks=NUM_TICKS))
# plot the same numbers but multiplied by 20
ax2.plot(xs,ys_lines*20,color='red')
# set the ticks for the twin axis
#ax2.set_yticks(np.arange(0,101,10))
ymax = (ax2.get_ylim()[1]//100+0.5)*100
ax2.set_ylim(0, ymax)
# change the tick locator for this axis and set the desired number of ticks
ax2.yaxis.set_major_locator(plt.LinearLocator(numticks=NUM_TICKS))
# set ticks for the common x axis (bottom)
ax2.xaxis.set_ticks(xs)
ax1.yaxis.grid(True)
ax1.set_axisbelow(True)
ax2.vlines(x = xs,ymin = 0, ymax = ys_lines*20) #Axes.vlines from line plot
fig.savefig('Barplot_Lineplot_2.png', dpi = 300, transparent = False)
import matplotlib.pyplot as plt
import numpy as np
fig, ax1 = plt.subplots()
# generate sample data for this example
xs = list(range(1,13))
prng = np.random.default_rng(123456)
ys_bars = prng.normal(loc=3.0,size=12)
ys_lines = prng.normal(loc=5.0,size=12,scale=0.5)
Overlaying Charts
• Categorical Bar and Line plot on same Axes
• X axis is numeric but non datetime
• Twin Axes with a shared x axis
• Ax.vlines from the line plot
Code End
Code Start
# create the 'twin’ axes with the y axis on the right
ax2=ax1.twinx()
# Setting zorder of the twin axes below original axes. Check link1, link2 on zorder
ax2.set_zorder(ax2.get_zorder()-1)
ax1.patch.set(visible = False)
281
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
COMBO CHARTS
(OVERLAY CHARTS)
282
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
https://datasciencelab.wordpress.com/2013/12/21/
beautiful-plots-with-pandas-and-matplotlib/
283
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
The plot and data is inspired from the below link :
https://pythondata.com/visualizing-data-overlaying-charts/
Most of the code and comments of original link is as it is considering it is very informative.
However, below changes have been done :
• Locator/formatter pairs created for placement of x-axis tick labels
• For loop to create the bar plots iteratively
• Cycler object with islice from itertools to assign colors to bars
• Addition of legend by mix of automatic detection and proxy artists
For the revised code, go to the link :
https://github.com/Bhaskar-JR/Matplotlib_Overlaying_Charts/blob/main/Matplotlib_Overlaying_Charts.ipynb
284
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
plt.rcParams['figure.figsize']=(20,10) # set the figure size
plt.style.use('fivethirtyeight') # using the fivethirtyeight matplotlib theme
path1 = "https://raw.githubusercontent.com/Bhaskar-JR/Matplotlib_Overlaying_Charts/main/Sales_Data.csv"
#path = '/Users/bhaskarroy/Files/Data Science/PYTHON/Visualisation/Matplotlib/Overlaying charts/sales.csv'
sales = pd.read_csv(path1) # Read the data in
sales.Date = pd.to_datetime(sales.Date) #set the date column to datetime
sales.set_index('Date', inplace=True) #set the index to the date column
# now the hack for the multi-colored bar chart:
# create fiscal year dataframes covering the timeframes you are looking for. In this case,
# the fiscal year covered October - September.
# --------------------------------------------------------------------------------
# Note: This should be set up as a function, but for this small amount of data,
# I just manually built each fiscal year. This is not very pythonic and would
# suck to do if you have many years of data, but it isn't bad for a few years of data.
# --------------------------------------------------------------------------------
fy10_all = sales[(sales.index >= '2009-10-01') & (sales.index < '2010-10-01')]
fy11_all = sales[(sales.index >= '2010-10-01') & (sales.index < '2011-10-01')]
fy12_all = sales[(sales.index >= '2011-10-01') & (sales.index < '2012-10-01')]
fy13_all = sales[(sales.index >= '2012-10-01') & (sales.index < '2013-10-01')]
fy14_all = sales[(sales.index >= '2013-10-01') & (sales.index < '2014-10-01')]
fy15_all = sales[(sales.index >= '2014-10-01') & (sales.index < '2015-10-01')]
The plot and data is inspired from the link.
Below changes have been done in the code :
• Locator/formatter pairs created for placement of x-axis tick labels
• For loop to create the bar plots iteratively
• Cycler object with islice from itertools to assign colors to bars
• Addition of legend by mix of automatic detection and proxy artists
For the revised code, go to the link
Code continues to Next slide
Code Start
285
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
# Let's build our plot
fig, ax1 = plt.subplots()
ax2 = ax1.twinx() # set up the 2nd axis
df = sales.copy()
df.index = mdates.date2num(df.index)
ax1.plot(df.Sales_Dollars, label = 'Monthly Revenue') #plot the Revenue on axis #1
# Creating cycler object with islice to set bar plot colors to grey, orange alternately
from itertools import cycle, islice
from datetime import datetime
import matplotlib.patches as mpatches
color_list = islice(cycle(['orange','grey']), 0, None)
# Using for loop to plot the fiscal year data sequentially as bar plots
# Assign color from cycler object
kwargs = dict(width=20, alpha=0.2) #kwargs dictionary for bar plot arguments
for fy in [fy10_all, fy11_all, fy12_all, fy13_all, fy14_all, fy15_all]:
fyr = fy.copy()
fyr.index = mdates.date2num(fyr.index)
ax2.bar(fyr.index, fyr.Quantity, **kwargs, color = next(color_list), label = 'Monthly Quantity')
ax2.grid(b=False) # turn off grid #2
ax1.set_title('Monthly Sales Revenue vs Number of Items Sold Per Month', fontsize = 25, y = 1.05)
ax1.set_ylabel('Monthly Sales Revenue')
ax2.set_ylabel('Number of Items Sold’)
The plot and data is inspired from the link.
Below changes have been done in the code :
• Locator/formatter pairs created for placement of x-axis tick labels
• For loop to create the bar plots iteratively
• Cycler object with islice from itertools to assign colors to bars
• Addition of legend by mix of automatic detection and proxy artists
For the revised code, go to the link
Code continues to Next slide
Code continues from Prev slide
286
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
#Creating locator/formatter
from dateutil.rrule import rrule, MONTHLY,YEARLY
from datetime import datetime, timedelta #timedelta holds days, seconds, microseconds internally
import matplotlib.dates as mdates
import matplotlib.ticker as ticker
start_date = datetime(2009,10,1)
#Let's do ceiling division to get the relevant number of financial years
count = -(-(sales.index[-1]-start_date)//timedelta(days = 365.25)) #Division of timedelta by timedelta of 365.25days
date_list = list(rrule(freq=YEARLY, count=count, dtstart=start_date))
date_list = mdates.date2num(date_list)
#Creating locator and formatter
locator = ticker.FixedLocator(date_list)
date_labels = [datetime.strftime(x, "FY %Y") for x in mdates.num2date(date_list)]
formatter = ticker.FixedFormatter(date_labels)
# Set the x-axis labels to be more meaningful than just some random dates.
ax1.xaxis.set_major_locator(locator)
ax1.xaxis.set_major_formatter(formatter)
ax1.xaxis.grid(b = True, color = 'grey', lw = 1,alpha = 0.5)
ax1.xaxis.set_tick_params(pad = 15)
[tkl.set(ha = 'left',rotation = 0, rotation_mode = "anchor") for tkl in ax1.xaxis.get_ticklabels()]
# Creating Legend with a mix of automatic detection and adding proxy artists
handle, label = ax1.get_legend_handles_labels()
patch1 = mpatches.Patch(color='orange',alpha = 0.5, label='Monthly Qty',zorder = 100)
patch2 = mpatches.Patch(color='grey',alpha = 0.5, label='Monthly Qty', zorder = 100)
handle.extend([patch1, patch2])
ax1.legend(handles = handle,bbox_to_anchor = (1,1), loc = 'upper right', bbox_transform = fig.transFigure)
#ax2.set_ylim(0,ax2.get_ylim()[1]*10//3) # To restrict the bar plots to only 30% of the height of line plots
plt.show()
The plot and data is inspired from the link.
Below changes have been done in the code :
• Locator/formatter pairs created for placement of x-axis tick labels
• For loop to create the bar plots iteratively
• Cycler object with islice from itertools to assign colors to bars
• Addition of legend by mix of automatic detection and proxy artists
For the revised code, go to the link
Code End
Code continues from Prev slide
287
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
https://datasciencelab.wordpress.com/2013/12/21/
beautiful-plots-with-pandas-and-matplotlib/
Data Source : demographic data from countries
in the European Union obtained
from Wolfram|Alpha.
Data Set :
Contains information on population, extension
and life expectancy in 24 European countries.
Visualization :
• Our Main objective is exploration on usage of
hues, transparencies and simple layouts to
create informative, rich and appealing
visualisation.
• Specifically
• bar length (along horizontal direction)
is population indicator
• bar length (along vertical direction) is
country size indicator
• Color of the bar is mapped to Age
expectancy
Go to the above link for detailed run-through of
the context and the code for creating the plot.
288
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
https://datasciencelab.wordpress.com/2013/12/21/
beautiful-plots-with-pandas-and-matplotlib/
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import matplotlib as mpl
from matplotlib.colors import LinearSegmentedColormap
from matplotlib.lines import Line2D
from matplotlib import cm
# Preparing the Data
countries = ['France','Spain','Sweden','Germany','Finland','Poland','Italy',
'United Kingdom','Romania','Greece','Bulgaria','Hungary',
'Portugal','Austria','Czech Republic','Ireland','Lithuania','Latvia',
'Croatia','Slovakia','Estonia','Denmark','Netherlands','Belgium']
extensions = [547030,504782,450295,357022,338145,312685,301340,243610,238391,
131940,110879,93028,92090,83871,78867,70273,65300,64589,56594,
49035,45228,43094,41543,30528]
populations = [63.8,47,9.55,81.8,5.42,38.3,61.1,63.2,21.3,11.4,7.35,
9.93,10.7,8.44,10.6,4.63,3.28,2.23,4.38,5.49,1.34,5.61,
16.8,10.8]
life_expectancies = [81.8,82.1,81.8,80.7,80.5,76.4,82.4,80.5,73.8,80.8,73.5,
74.6,79.9,81.1,77.7,80.7,72.1,72.2,77,75.4,74.4,79.4,81,80.5]
data = {'extension' : pd.Series(extensions, index=countries),
'population' : pd.Series(populations, index=countries),
'life expectancy' : pd.Series(life_expectancies, index=countries)}
df = pd.DataFrame(data)
df = df.sort_values(by = 'life expectancy')
Code Start
Code continues to Next slide
Go to the above link for comprehensive run-
through of the context and the code for
creating the plot.
The code has been reproduced from the link
with all the comments intact. There is only a
minor change owing to API changes as the link
is dated. The change is : mpl.colors.Normalize
has been used for instantiation of norm as
required for Scalar Mappable.
See also
Colormap reference for a list of builtin colormaps.
Creating Colormaps in Matplotlib for examples of how to make colormaps.
Choosing Colormaps in Matplotlib an in-depth discussion of choosing
colormaps.
Colormap Normalization for more details about data normalization.
Customized Colorbars tutorials
289
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
# Create a figure of given size
fig = plt.figure(figsize=(16,12))
# Add a subplot
ax = fig.add_subplot(111)
# Set title
ttl = 'Population, size and age expectancy in the European Union'
# Set color transparency (0: transparent; 1: solid)
a = 0.7
# Create a colormap
customcmap = [(x/24.0, x/48.0, 0.05) for x in range(len(df))]
# Plot the 'population' column as horizontal bar plot
df['population'].plot(kind='barh', ax=ax, alpha=a, legend=False, color=customcmap,
edgecolor='w', xlim=(0,max(df['population'])), title=ttl)
# Remove grid lines (dotted lines inside plot)
ax.grid(False)
# Remove plot frame
ax.set_frame_on(False)
# Pandas trick: remove weird dotted line on axis
#ax.lines[0].set_visible(False)
# Customize title, set position, allow space on top of plot for title
ax.set_title(ax.get_title(), fontsize=26, alpha=a, ha='left')
plt.subplots_adjust(top=0.9)
ax.title.set_position((0,1.08))
# Set x axis label on top of plot, set label text
ax.xaxis.set_label_position('top')
xlab = 'Population (in millions)'
ax.set_xlabel(xlab, fontsize=20, alpha=a, ha='left')
ax.xaxis.set_label_coords(0, 1.04)
Code continues to Next slide
Code continues from Prev slide
https://datasciencelab.wordpress.com/2013/12/21/
beautiful-plots-with-pandas-and-matplotlib/
Go to the above link for comprehensive run-
through of the context and the code for
creating the plot.
The code has been reproduced from the link
with all the comments intact. There is only a
minor change owing to API changes as the link
is dated. The change is : mpl.colors.Normalize
has been used for instantiation of norm as
required for Scalar Mappable.
See also
Colormap reference for a list of builtin colormaps.
Creating Colormaps in Matplotlib for examples of how to make colormaps.
Choosing Colormaps in Matplotlib an in-depth discussion of choosing
colormaps.
Colormap Normalization for more details about data normalization.
Customized Colorbars tutorials
290
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
# Position x tick labels on top
ax.xaxis.tick_top()
# Remove tick lines in x and y axes
ax.yaxis.set_ticks_position('none')
ax.xaxis.set_ticks_position('none')
# Customize x tick labels
xticks = [5,10,20,50,80]
ax.xaxis.set_ticks(xticks)
ax.set_xticklabels(xticks, fontsize=16, alpha=a)
# Customize y tick labels
yticks = [item.get_text() for item in ax.get_yticklabels()]
ax.set_yticklabels(yticks, fontsize=16, alpha=a)
ax.yaxis.set_tick_params(pad=12)
# Set bar height dependent on country extension
# Set min and max bar thickness (from 0 to 1)
hmin, hmax = 0.3, 0.9
xmin, xmax = min(df['extension']), max(df['extension'])
# Function that interpolates linearly between hmin and hmax
f = lambda x: hmin + (hmax-hmin)*(x-xmin)/(xmax-xmin)
# Make array of heights
hs = [f(x) for x in df['extension']]
# Iterate over bars
for container in ax.containers:
# Each bar has a Rectangle element as child
for i,child in enumerate(container.get_children()):
# Reset the lower left point of each bar so that bar is centered
child.set_y(child.get_y()- 0.125 + 0.5-hs[i]/2)
# Attribute height to each Recatangle according to country's size
plt.setp(child, height=hs[i])
Code continues to Next slide
Code continues from Prev slide
https://datasciencelab.wordpress.com/2013/12/21/
beautiful-plots-with-pandas-and-matplotlib/
Go to the above link for comprehensive run-
through of the context and the code for
creating the plot.
The code has been reproduced from the link
with all the comments intact. There is only a
minor change owing to API changes as the link
is dated. The change is : mpl.colors.Normalize
has been used for instantiation of norm as
required for Scalar Mappable.
See also
Colormap reference for a list of builtin colormaps.
Creating Colormaps in Matplotlib for examples of how to make colormaps.
Choosing Colormaps in Matplotlib an in-depth discussion of choosing
colormaps.
Colormap Normalization for more details about data normalization.
Customized Colorbars tutorials
291
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
# Legend
# Create fake labels for legend
l1 = Line2D([], [], linewidth=6, color='k', alpha=a)
l2 = Line2D([], [], linewidth=12, color='k', alpha=a)
l3 = Line2D([], [], linewidth=22, color='k', alpha=a)
# Set three legend labels to be min, mean and max of countries extensions
# (rounded up to 10k km2)
rnd = 10000
rnd = 10000
labels = [str(int(round(l/rnd,1 )*rnd)) for l in [min(df['extension']), np.mean(df['extension']),
max(df['extension'])]]
# Position legend in lower right part
# Set ncol=3 for horizontally expanding legend
leg = ax.legend([l1, l2, l3], labels, ncol=3, frameon=False, fontsize=16,
bbox_to_anchor=[1.1, 0.11], handlelength=2,
handletextpad=1, columnspacing=2, title='Size (in km2)')
# Customize legend title
# Set position to increase space between legend and labels
plt.setp(leg.get_title(), fontsize=20, alpha=a)
leg.get_title().set_position((0, 10))
# Customize transparency for legend labels
[plt.setp(label, alpha=a) for label in leg.get_texts()]
Code continues to Next slide
Code continues from Prev slide
https://datasciencelab.wordpress.com/2013/12/21/
beautiful-plots-with-pandas-and-matplotlib/
Go to the above link for comprehensive run-
through of the context and the code for
creating the plot.
The code has been reproduced from the link
with all the comments intact. There is only a
minor change owing to API changes as the link
is dated. The change is : mpl.colors.Normalize
has been used for instantiation of norm as
required for Scalar Mappable.
See also
Colormap reference for a list of builtin colormaps.
Creating Colormaps in Matplotlib for examples of how to make colormaps.
Choosing Colormaps in Matplotlib an in-depth discussion of choosing
colormaps.
Colormap Normalization for more details about data normalization.
Customized Colorbars tutorials
292
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
# Create a fake colorbar
ctb = LinearSegmentedColormap.from_list('custombar', customcmap, N=2048)
# Trick from http://stackoverflow.com/questions/8342549/
# matplotlib-add-colorbar-to-a-sequence-of-line-plots
# Used mpl.colors.Normalize for Scalar Mappable
sm = plt.cm.ScalarMappable(cmap=ctb, norm=mpl.colors.Normalize(vmin=72, vmax=84))
# Fake up the array of the scalar mappable
sm._A = []
# Set colorbar, aspect ratio
cbar = plt.colorbar(sm, alpha=0.05, aspect=16, shrink=0.4)
cbar.solids.set_edgecolor("face")
# Remove colorbar container frame
cbar.outline.set_visible(False)
# Fontsize for colorbar ticklabels
cbar.ax.tick_params(labelsize=16)
# Customize colorbar tick labels
mytks = range(72,86,2)
cbar.set_ticks(mytks)
cbar.ax.set_yticklabels([str(a) for a in mytks], alpha=a)
# Colorbar label, customize fontsize and distance to colorbar
cbar.set_label('Age expectancy (in years)', alpha=a, rotation=270, fontsize=20,
labelpad=20)
# Remove color bar tick lines, while keeping the tick labels
cbarytks = plt.getp(cbar.ax.axes, 'yticklines')
plt.setp(cbarytks, visible=False)
plt.savefig("beautiful plot.png", bbox_inches='tight')
Code End
Code continues from Prev slide
https://datasciencelab.wordpress.com/2013/12/21/
beautiful-plots-with-pandas-and-matplotlib/
Go to the above link for comprehensive run-
through of the context and the code for
creating the plot.
The code has been reproduced from the link
with all the comments intact. There is only a
minor change owing to API changes as the link
is dated. The change is : mpl.colors.Normalize
has been used for instantiation of norm as
required for Scalar Mappable.
See also
Colormap reference for a list of builtin colormaps.
Creating Colormaps in Matplotlib for examples of how to make colormaps.
Choosing Colormaps in Matplotlib an in-depth discussion of choosing
colormaps.
Colormap Normalization for more details about data normalization.
Customized Colorbars tutorials
293
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Data Source : https://uidai.gov.in/images/state-wise-aadhaar-saturation.pdf
Check out the github repository for the shape files and notebook:
https://github.com/Bhaskar-JR/India-Statewise-Population-2020
GeoSpatial Data - Chloropeth Map
Using Geopandas, Matplotlib
294
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Data Source : https://uidai.gov.in/images/state-wise-aadhaar-saturation.pdf
Check out the github repository for the shape files and notebook:
https://github.com/Bhaskar-JR/India-Statewise-Population-2020
GeoSpatial Data - Chloropeth Map
Using Geopandas, Matplotlib
295
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
REFERENCES
296
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Category Description Link
Matplotlib overview,
tutorials
Succinct overview of matplotlib https://www.webpages.uidaho.edu/~stevel/504/Matplotlib%20Notes.pdf
What does it mean when they say 'stateful’? https://stackoverflow.com/questions/24764918/what-does-it-mean-when-they-say-stateful/24768434#24768434
Summary of Matplotlib for python developers https://radhakrishna.typepad.com/rks_musings/2012/04/matplotlib-for-python-developers-summary.html
Very Interesting Article ”Matplotlib and the Future of Visualization in
Python”by Jake VanderPlas
https://jakevdp.github.io/blog/2013/03/23/matplotlib-and-the-future-of-visualization-in-python/
Excellent resource with examples using mix of Pyplot and OO style -
“Python Data Cleaning Cookbook- Modern techniques and Python
tools to detect and remove dirty data and extract key insights” by
Michael Walker
[]
Advance plotting Tutorial https://python4astronomers.github.io/plotting/advanced.html
Nicola P Rougier https://github.com/rougier/matplotlib-tutorial
Beautiful tutorial using Pandas and Matplotlib in conjunction : http://jonathansoma.com/lede/algorithms-2017/classes/fuzziness-matplotlib/how-pandas-uses-matplotlib-plus-figures-
axes-and-subplots/
Blog Post - Python, Machine Learning, and Language Wars https://sebastianraschka.com/blog/2015/why-python.html
Python related Understanding df.plot in Pandas : http://jonathansoma.com/lede/algorithms-2017/classes/fuzziness-matplotlib/understand-df-plot-in-pandas/
Discussion to wrap all of the matplotlib getter and setter methods
with python properties, allowing them to be read and written like
class attributes
https://matplotlib.org/stable/devel/MEP/MEP13.html?highlight=artist%20attributes#id1
On *args and **kwargs https://stackoverflow.com/questions/36901/what-does-double-star-asterisk-and-star-asterisk-do-for-parameters?rq=1
On purpose of kwargs in https://stackoverflow.com/questions/1769403/what-is-the-purpose-and-use-of-kwargs?
Decorator basics https://stackoverflow.com/questions/739654/how-to-make-function-decorators-and-chain-them-together?rq=1
Sorting a dictionary by value https://stackoverflow.com/questions/613183/how-do-i-sort-a-dictionary-by-value/613218#613218
Using ax parameter https://stackoverflow.com/questions/43085427/using-dataframe-plot-to-make-a-chart-with-subplots-how-to-use-ax-
parameter?
Infinite Iterating always from a fixed position : https://stackoverflow.com/questions/42459582/how-to-specify-where-to-start-in-an-itertools-cycle-function
Difference between property and attribute : https://stackoverflow.com/questions/7374748/whats-the-difference-between-a-python-property-and-
attribute/7377013#7377013
Introduction to Probability Using Python : https://nbviewer.jupyter.org/url/norvig.com/ipython/Probability.ipynb
Discussion on Array Programming with Numpy : https://www.repository.cam.ac.uk/bitstream/handle/1810/315595/41586_2020_2649_MOESM1_ESM.pdf?sequence=2
On Random Number https://people.astro.umass.edu/~schloerb/ph281/Lectures/RandomNumbers/RandomNumbers.pdf
Fluent Python by Luciano Ramalho. Chapter - ‘Iterables, Iterators, and
Generators’ is relevant for many operations in matplotlib, pandas,
numpy
‘The NumPy array: a structure for efficient numerical computation’ by
Stefan van der Walt provides indepth understanding of Numpy Arrays
https://hal.inria.fr/inria-00564007/document
References Start
Also check https://scipy-lectures.org/advanced/advanced_python/index.html#id2
297
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Category Description Link
Figures, subplots,
gridspec
Relationship between DPI and figure size https://stackoverflow.com/questions/47633546/relationship-between-dpi-and-figure-size/47639545#47639545
Change the figure size https://stackoverflow.com/questions/332289/how-do-you-change-the-size-of-figures-drawn-with-
matplotlib/638443#638443
Using bbox_inches = ‘tight’ in plt.savefig() to ensure all artists are in
saved output | behaviour of jupyter "inline" backend (%matplotlib
inline)
https://stackoverflow.com/questions/44642082/text-or-legend-cut-from-matplotlib-figure-on-savefig
On Multiple Subplots https://actruce.com/en/object-oriented-way-of-using-matplotlib-4-multiple-subplots/
Combining different figures and putting in single subplot https://stackoverflow.com/questions/16748577/matplotlib-combine-different-figures-and-put-them-in-a-single-subplot-
sharing-a?noredirect=1&lq=1
Brilliant Link on adding subplots - https://stackoverflow.com/questions/3584805/in-matplotlib-what-does-the-argument-mean-in-fig-add-subplot111?rq=1
Excellent link on using GridSpec https://www.python-course.eu/matplotlib_gridspec.php
Usecase Scenarios of plt.cla, plt.clf, plt.close : https://stackoverflow.com/questions/8213522/when-to-use-cla-clf-or-close-for-clearing-a-plot-in-matplotlib?
Fig patch, axes patch,
zorder
Superb Example on setting fc of fig patch and axes patch https://stackoverflow.com/questions/4581504/how-to-set-opacity-of-background-colour-of-graph-with-matplotlib
Zorder demo : https://matplotlib.org/stable/gallery/misc/zorder_demo.html?highlight=zorder
Ticks, tick labels On removing ticks https://stackoverflow.com/questions/12998430/remove-xticks-in-a-matplotlib-plot
Rotating Axis Labels in Matplotlib https://www.pythoncharts.com/2019/05/17/rotating-axis-labels/
Tick Label Rotation and spacing https://stackoverflow.com/questions/43152502/how-can-i-rotate-xticklabels-in-matplotlib-so-that-the-spacing-between-
each-xtic?noredirect=1&lq=1
Making tick label font size smaller https://stackoverflow.com/questions/6390393/matplotlib-make-tick-labels-font-size-smaller/11386056#11386056
Formatter for large tick values - https://dfrieds.com/data-visualizations/how-format-large-tick-values.html
Discussion on tick labels zorder being above axes and patches
whereas the grid lines below the axes :
https://stackoverflow.com/questions/48327099/is-it-possible-to-set-higher-z-index-of-the-axis-labels
Tick locator and formatter for timeseries without using
matplotlib.dates
https://stackoverflow.com/questions/33743394/
Time Series AR, MA and ARIMA Models in Python https://www.ritchievink.com/blog/2018/09/26/algorithm-breakdown-ar-ma-and-arima-models/
Practical Time Series Analysis - Prediction with Statistics & Machine Learning by Aileen Nielson
Machine Learning for Time Series Forecasting with Python by Francesca Lazzeri
Advanced Data Science and Analytics with Python by Jesus Rogel-Salazar
Analysis of Financial Time Series, 3d Edition, by Ruey S. Tsay
Hands-on Time Series Analysis with Python by B V Vishwas and A. Patel
Data Science Revealed by Tshepo Chris Nokeri
Time Series Related Books
298
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Category Description Link
Legend Go to reference for understanding the purport of Legend arguments https://stackoverflow.com/questions/4700614/how-to-put-the-legend-out-of-the-plot/4701285#4701285
On adding text artists in Legend https://stackoverflow.com/questions/27174425/how-to-add-a-string-as-the-artist-in-matplotlib-legend
Drawing and Animating Shapes with Matplotlib https://nickcharlton.net/posts/drawing-animating-shapes-matplotlib.html
Tuple Arguments in bbox_to_anchor https://stackoverflow.com/questions/39803385/what-does-a-4-element-tuple-argument-for-bbox-to-anchor-mean-in-
matplotlib/39806180#39806180
Annotation resources -
from matplotlib v3.4.2
• Adding value labels on a matplotlib bar chart
• How to annotate each segment of a stacked bar chart
• Stacked Bar Chart with Centered Labels
• How to plot and annotate multiple data columns in a seaborn
barplot
• stack bar plot in matplotlib and add label to each section
• How to add multiple annotations to a barplot
• How to plot and annotate a grouped bar chart
Font properties Using font dictionary : https://matplotlib.org/stable/gallery/text_labels_and_annotations/text_fontdict.html?highlight=font%20dictionary
Fonts demo object oriented style : https://matplotlib.org/stable/gallery/text_labels_and_annotations/fonts_demo.html
List of font families or name : https://stackoverflow.com/questions/18821795/
set([f.name for f in
matplotlib.font_manager.fontManager.afmlist]) and set([f.name for f
in matplotlib.font_manager.fontManager.ttflist])
[]
Data Visualizations Ten Simple Rules for Better Figures https://journals.plos.org/ploscompbiol/article?id=10.1371/journal.pcbi.1003833
Excellent link on beautiful data viz http://www.randalolson.com/2014/06/28/how-to-make-beautiful-data-visualizations-in-python-with-matplotlib/
Superb data viz https://www.dunderdata.com/blog/create-a-bar-chart-race-animation-in-python-with-matplotlib
A Dramatic tour through Python’s Visualisation landscape including
ggplot2 and Altair
https://dsaber.com/2016/10/02/a-dramatic-tour-through-pythons-data-visualization-landscape-including-ggplot-and-
altair/
Superb link covering different kind of plots. Can serve as the base
code which can be modified as per requirement https://www.machinelearningplus.com/plots/top-50-matplotlib-visualizations-the-master-plots-python/#14.-Area-Chart
Superb blog on wide ranging topics related to data
wrangling/viz/programming with Pandas, matplotlib and python - https://dfrieds.com/#dataviz
Pie vs dot plots https://pierreh.eu/pie-vs-dots-powersystem/
Visualizing Data by William Cleveland []
Cleveland dot plots in excel https://mbounthavong.com/blog/2018/7/16/communicating-data-effectively-with-data-visualizations-part-9-cleveland-
plots
Styling the plots https://dfrieds.com/data-visualizations/style-plots-python-matplotlib.html
Extensive coverage of code for multiple aspects of plotting https://queirozf.com/entries/matplotlib-pyplot-by-example
299
Ch
Ch
Ch
Ch
Ch
Ch
Ch
DT
Category Description Link
Pandas Plotting,
Seaborn
Legend in plotting with Pandas https://stackoverflow.com/questions/21988196/legend-only-shows-one-label-when-plotting-with-pandas
Seaborn : https://michaelwaskom.medium.com/three-common-seaborn-difficulties-10fdd0cc2a8b
Superb Article on using Matplotlib, pandas and seaborn together : https://towardsdatascience.com/matplotlib-seaborn-pandas-an-ideal-amalgamation-for-statistical-data-visualisation-
f619c8e8baa3
Time Series in Seaborn : https://www.geeksforgeeks.org/creating-a-time-series-plot-with-seaborn-and-pandas/
On limitation of plotting time series data in Seaborn : https://stackoverflow.com/questions/22795348/plotting-time-series-data-with-seaborn
Plotting time in python: https://stackoverflow.com/questions/1574088/plotting-time-in-python-with-matplotlib?rq=1
Time Series in Matplotlib : https://www.earthdatascience.org/courses/use-data-open-source-python/use-time-series-data-in-python/date-time-
types-in-pandas-python/customize-dates-matplotlib-plots-python/
Comprehensive coverage of datetime in Pandas Time series : https://developers.arcgis.com/python/guide/part5-time-series-analysis-with-pandas/
Converting between datetime, timestamp and datetime64 https://stackoverflow.com/questions/13703720/converting-between-datetime-timestamp-and-datetime64?
R ggplot2 An Introduction to ggplot2 by Joey Stanley https://joeystanley.com/downloads/171012-ggplot2_handout.pdf
“ggplot2 Elegant Graphics for Data Analysis” by Hadley Wickham -
Highly recommended for ggplot2
https://ggplot2-book.org/introduction.html
“R Graphics E ssentials for Great Data Visualization” by Alboukadel
KASSAMBAR
Highly recommended for creating great graphics for the right data using either the ggplot2 package and extensions or
the traditional R graphics.
Books on Data Science with R Modern Data Science with R by Benjamin S. Baumer Daniel T. Kaplan Nicholas J. Horton
Machine Learning Essentials by Alboukadel KASSAMBAR
Application oriented Code Reference and R handbook for
experienced and new users
https://epirhandbook.com/ggplot-basics.html#labels
Bar charts in R ggplot : http://bradleyboehmke.github.io/tutorials/barchart
Manipulation of tabular data in R http://barryrowlingson.github.io/hadleyverse/
Comparison of viz tools in Python and R : https://shankarmsy.github.io/posts/exploringviz1.html
Covering baseline R vocabulary and knowledge for the primary data
wrangling processes.
“Data Wrangling with R” by Bradley C. Boehmke
“R for Data Science” by Hadley Wickham and Garrett Grolemund –
Highly recommended
https://r4ds.had.co.nz/introduction.html
“Building a plot layer by layer” by Hadley Wickham - https://rpubs.com/hadley/ggplot2-layers
geopandas Shapely documentation – Page No. 27 - https://geopandas.org/getting_started/introduction.html
Creating another layer to take care of Nan values https://buildmedia.readthedocs.org/media/pdf/shapely/latest/shapely.pdf
Labeling polygons in geopandas plotting https://stackoverflow.com/questions/38473257/how-can-i-set-a-special-colour-for-nans-in-my-plot
Placement of colorbars using cax https://stackoverflow.com/questions/38899190/geopandas-label-polygons
Fixing colorbars https://matplotlib.org/stable/gallery/subplots_axes_and_figures/colorbar_placement.html
Setting single common colorbar for two subplots https://joseph-long.com/writing/colorbars/
Delhi Map Geopandas discussion https://stackoverflow.com/questions/13784201/matplotlib-2-subplots-1-colorbar
Interesting Case study Airbnb Paris analysis https://stackoverflow.com/questions/63644131/how-to-use-geopandas-to-plot-latitude-and-longitude-on-a-more-
detailed-map-with/63646936#63646936
References End
300

More Related Content

PDF
Python Matplotlib Tutorial | Matplotlib Tutorial | Python Tutorial | Python T...
PDF
Text classification presentation
PPTX
Introduction to matplotlib
PDF
pandas - Python Data Analysis
PPTX
Data Analysis in Python-NumPy
PPTX
Scikit Learn intro
PDF
Python Sequence | Python Lists | Python Sets & Dictionary | Python Strings | ...
PPT
Query Processing in IR
Python Matplotlib Tutorial | Matplotlib Tutorial | Python Tutorial | Python T...
Text classification presentation
Introduction to matplotlib
pandas - Python Data Analysis
Data Analysis in Python-NumPy
Scikit Learn intro
Python Sequence | Python Lists | Python Sets & Dictionary | Python Strings | ...
Query Processing in IR

What's hot (20)

PPTX
Visualization and Matplotlib using Python.pptx
PPTX
Object oriented programming in python
PDF
DATA VISUALIZATION USING MATPLOTLIB (PYTHON)
PPT
Python Pandas
PDF
Machine learning life cycle
PPTX
Introduction to pandas
PDF
Python - object oriented
PPTX
Text Classification
PDF
Python Collections Tutorial | Edureka
PPTX
Python - An Introduction
PDF
Python Programming Language | Python Classes | Python Tutorial | Python Train...
PDF
Toxic Comment Classification
PPT
Python GUI Programming
PDF
PyTorch Introduction
PPTX
Beautiful soup
PDF
Data Wrangling and Visualization Using Python
PDF
Django Introduction & Tutorial
PPTX
Data Structures in Python
Visualization and Matplotlib using Python.pptx
Object oriented programming in python
DATA VISUALIZATION USING MATPLOTLIB (PYTHON)
Python Pandas
Machine learning life cycle
Introduction to pandas
Python - object oriented
Text Classification
Python Collections Tutorial | Edureka
Python - An Introduction
Python Programming Language | Python Classes | Python Tutorial | Python Train...
Toxic Comment Classification
Python GUI Programming
PyTorch Introduction
Beautiful soup
Data Wrangling and Visualization Using Python
Django Introduction & Tutorial
Data Structures in Python
Ad

Similar to Matplotlib Review 2021 (20)

PPTX
Unit III for data science engineering.pptx
PPTX
Matplotlib.pptx for data analysis and visualization
PDF
Introduction to plotting in Python
PPTX
UNIT-5-II IT-DATA VISUALIZATION TECHNIQUES
PPTX
Matplotlib - Python Plotting Library Description
PDF
Matplotlib 簡介與使用
PPTX
Introduction to Pylab and Matploitlib.
PPTX
UNIT_4_data visualization.pptx
PPTX
Python_Matplotlib_13. _Slides.pptx
PPTX
Unit3-v1-Plotting and Visualization.pptx
PDF
Python matplotlib cheat_sheet
PDF
Use the Matplotlib, Luke @ PyCon Taiwan 2012
PPTX
Python_Matplotlib_13_Slides_With_Diagrams.pptx
PDF
S4-Matplotlib_Computational_Physics_.pdf
PPTX
Matplotlib-Python-Plotting-Library(Edited).pptx
PPTX
Python Visualization API Primersubplots
PPTX
Matplotlib yayyyyyyyyyyyyyin Python.pptx
PPTX
Introduction to Matplotlib Library in Python.pptx
PPTX
Python Pyplot Class XII
PPTX
Matplotlib_Presentation jk jdjklskncncsjkk
Unit III for data science engineering.pptx
Matplotlib.pptx for data analysis and visualization
Introduction to plotting in Python
UNIT-5-II IT-DATA VISUALIZATION TECHNIQUES
Matplotlib - Python Plotting Library Description
Matplotlib 簡介與使用
Introduction to Pylab and Matploitlib.
UNIT_4_data visualization.pptx
Python_Matplotlib_13. _Slides.pptx
Unit3-v1-Plotting and Visualization.pptx
Python matplotlib cheat_sheet
Use the Matplotlib, Luke @ PyCon Taiwan 2012
Python_Matplotlib_13_Slides_With_Diagrams.pptx
S4-Matplotlib_Computational_Physics_.pdf
Matplotlib-Python-Plotting-Library(Edited).pptx
Python Visualization API Primersubplots
Matplotlib yayyyyyyyyyyyyyin Python.pptx
Introduction to Matplotlib Library in Python.pptx
Python Pyplot Class XII
Matplotlib_Presentation jk jdjklskncncsjkk
Ad

Recently uploaded (20)

PPT
ISS -ESG Data flows What is ESG and HowHow
PPTX
STUDY DESIGN details- Lt Col Maksud (21).pptx
PPTX
Microsoft-Fabric-Unifying-Analytics-for-the-Modern-Enterprise Solution.pptx
PPTX
iec ppt-1 pptx icmr ppt on rehabilitation.pptx
PPTX
Data_Analytics_and_PowerBI_Presentation.pptx
PDF
annual-report-2024-2025 original latest.
PPTX
Business Ppt On Nestle.pptx huunnnhhgfvu
PPTX
The THESIS FINAL-DEFENSE-PRESENTATION.pptx
PDF
Mega Projects Data Mega Projects Data
PPT
Reliability_Chapter_ presentation 1221.5784
PPTX
Introduction to machine learning and Linear Models
PDF
TRAFFIC-MANAGEMENT-AND-ACCIDENT-INVESTIGATION-WITH-DRIVING-PDF-FILE.pdf
PPTX
Business Acumen Training GuidePresentation.pptx
PPTX
climate analysis of Dhaka ,Banglades.pptx
PPT
Quality review (1)_presentation of this 21
PDF
“Getting Started with Data Analytics Using R – Concepts, Tools & Case Studies”
PPTX
MODULE 8 - DISASTER risk PREPAREDNESS.pptx
PPTX
mbdjdhjjodule 5-1 rhfhhfjtjjhafbrhfnfbbfnb
PDF
Galatica Smart Energy Infrastructure Startup Pitch Deck
ISS -ESG Data flows What is ESG and HowHow
STUDY DESIGN details- Lt Col Maksud (21).pptx
Microsoft-Fabric-Unifying-Analytics-for-the-Modern-Enterprise Solution.pptx
iec ppt-1 pptx icmr ppt on rehabilitation.pptx
Data_Analytics_and_PowerBI_Presentation.pptx
annual-report-2024-2025 original latest.
Business Ppt On Nestle.pptx huunnnhhgfvu
The THESIS FINAL-DEFENSE-PRESENTATION.pptx
Mega Projects Data Mega Projects Data
Reliability_Chapter_ presentation 1221.5784
Introduction to machine learning and Linear Models
TRAFFIC-MANAGEMENT-AND-ACCIDENT-INVESTIGATION-WITH-DRIVING-PDF-FILE.pdf
Business Acumen Training GuidePresentation.pptx
climate analysis of Dhaka ,Banglades.pptx
Quality review (1)_presentation of this 21
“Getting Started with Data Analytics Using R – Concepts, Tools & Case Studies”
MODULE 8 - DISASTER risk PREPAREDNESS.pptx
mbdjdhjjodule 5-1 rhfhhfjtjjhafbrhfnfbbfnb
Galatica Smart Energy Infrastructure Startup Pitch Deck

Matplotlib Review 2021

  • 1. Ch Ch Ch Ch Ch Ch Ch DT Ø MATPLOTLIB ARCHITECTURE Ø ANATOMY OF A MATPLOTLIB FIGURE Ø PYPLOT INTERFACE Ø OBJECT ORIENTED INTERFACE PREPARED BY : BHASKAR JYOTI ROY bhaskarjroy1605@gmail.com REFERENCES : • Matplotlib Release 3.4.2, May 08, 2021 • Mastering Matplotlib by Duncan M. McGreggor • StackOverflow and others 1 A COMPLETE REVIEW (Abridged Version)
  • 2. Ch Ch Ch Ch Ch Ch Ch DT • Matplotlib Introduction • Topics we will cover • Matplotlib Architecture – Three layers • Relationship of layers to Data and Figure • Deep Dive into Backend Layer • Deep Dive into Artist Layer • Anatomy of a Figure • Hierarchy of Artists • Scripting Layer • Pyplot Interface • Frequently Used Pyplot Functions • For the Figure, Axes, Axis • For Charting • For Multiple plots, Layout Customisation • Code Snippet for use of subplot function • For fine tuning plot appearance • rc Params , rcParams for global changes • rcParams dictionary variables to revert back settings • StyleSheets • StyleSheets – Showcasing All styles 2 INDEX – MATPLOTLIB API
  • 3. Ch Ch Ch Ch Ch Ch Ch DT INDEX – OO INTERFACE • OO interface • Create a Figure instance • Use Figures to create one or more axes or subplot • Demo of fig.subplots() and enumerate, flatten • Demo of fig.add_subplots() without and with GridSpec • Demo of fig.add_axes() • Use Axes helper methods to add artists to respective containers within the Axes object • Axes Helper Methods to create Primitives • Axes Methods to create Different Plots and text • Example Illustrations of various plots – Piecharts, Histograms, bar charts, Grouped bars charts • Example Illustrations of various plots – Line Chart, Bidirectional Chart, Marginal Histograms, Diverging Lollypop chart • Use Axes/Axis Helper methods to access and set x-axis and y-axis tick, tick labels and axis labels • Axes Helper Methods for Appearance, Axis Limits • Axes Helper Methods for Ticks and Tick Labels, GridLines • Axes Helper Methods for Axis Labels, Title and legend • Axis Helper Methods for Formatters and Locators, Axis Label • Axis Helper Methods for Ticks, Tick Labels • Axis Helper Methods – X Axis, Y Axis Specific • Tick Params Method – Bulk property Setter, A Note on Practical Implementation • Special Note on Tickers – Locator Class, Demo 1, Demo 2 3
  • 4. Ch Ch Ch Ch Ch Ch Ch DT • Special Note on Tickers – Formatter Class • Tickers – Using Simple Formatting, Formatter Class • Tickers for Time Series Plotting, Date Locator and Date Formatter • Concise date Formatter Example • Use Axes Methods (Accessors) to access artists through attributes • Accessing Artists by Assigning to a Variable when creating • Accessing Artists through Attributes - Approach • Useful Methods and Python Built in Methods • Accessing Artists through Attributes (Artist Hierarchy) • Set/modify the Artist properties after accessing the artists • Mapping the Journey – From Axes Instance to Artist Instance Properties • Properties Common to All Artists • Setting Artist Properties, Inspecting Artist Properties • Additional Properties specific to Line 2D , text • Special look into Patch Rectangle • Additional Properties Rectangle Specific • Usecases – Modifying the Rectangle patch properties • Methods of Rectangle Patch • Creating User Defined function to Stylize Axes • Creating User Defined function to Add data labels in bar plots • Demo – Using UDF to stylize Axes and add data labels INDEX – OO INTERFACE 4
  • 5. Ch Ch Ch Ch Ch Ch Ch DT • Transformations • Transformation Framework • Transformation Framework – The Coordinate systems • Transformation Framework – Transformation Objects • Blended Transformations Example • Colors • Specifying Colors – Acceptable Formats • Exploring the Color Charts – Use Case Scenario • Base Colors : ‘b’, ‘g’, ’r’, ‘c’, ‘m’, ‘y’, ‘k’, ‘w’ • Tableau Palette • CSS colors • XKCD Colors – xkcd:black – xkcd:dirty orange • XKCD Colors – xkcd:medium brown – xkcd:sunny yellow • XKCD Colors – xkcd:parchment – xkcd:leaf • XKCD Colors – xkcd:kermit green– xkcd:light seafoam • XKCD Colors – xkcd:dark mint– xkcd:ocean blue • XKCD Colors – xkcd:dirty blue– xkcd:ultra marine • XKCD Colors – xkcd:purpleish blue – xkcd:light plum • XKCD Colors – xkcd:pale magenta – xkcd:salmon pink INDEX – OO INTERFACE 5
  • 6. Ch Ch Ch Ch Ch Ch Ch DT • ColorMaps - Overview • Colormaps Classification • Range of Matplotlib Colormaps • Sequential colormaps • Diverging colormaps • Cyclic colormaps • Qualitative colormaps • Miscellaneous colormaps • Lightness of Matplotlib colormaps • Accessing Colors from a Matplotlib Colormap • Creating Listed Colormap from Existing Matplotlib Colormaps • Code snippet to create listed colormaps and accessing colors • Add legend and set title to the axes • Aspects of Legend • Adding Legend to Axes Call Signatures, Important terminologies • Automatic detection of elements to be shown in the legend • Explicitly defining the elements in the legend • With labels explicitly defined in call signature • With labels omitted in call signature • Creating Proxy Artists and Adding in the legend (Code demo) • Controlling Legend Location, Placement – Concept, Demo INDEX – OO INTERFACE 6
  • 7. Ch Ch Ch Ch Ch Ch Ch DT • Epilogue • Data Structures Overview • Pseudo Random Number Generation (in Python, Numpy) • Handling Date and Time • Statistics with Python • Appendix • References INDEX – END 7
  • 10. Ch Ch Ch Ch Ch Ch Ch DT MATPLOTLIB INTRODUCTION A comprehensive library for creating static, animated, and interactive visualizations in Python. Widely used plotting library for the Python programming language A core component of the scientific Python stack – a group of scientific computing tools, alongwith Numpy, Scipy and IPython Original Design Philosophy Matplotlib is designed with the philosophy that you should be able to create simple plots with just a few commands, or just one! If you want to see a histogram of your data, you shouldn't need to instantiate objects, call methods, set properties, and so on; it should just work. Initial Release in 2003 Created by neurobiologist John D. Hunter to plot data of electrical activity in the brains of epilepsy patients, but today is used in number of fields libraries like pandas and Seaborn are “wrappers” over matplotlib - providing high level API’s that allow access to a number of matplotlib's methods with less code. For instance, pandas’ .plot()combines multiple matplotlib methods into a single method, thus plotting in a few lines. Two ways to use Matplotlib : • Pyplot Interface inspired from MATLAB is fast and convenient • Pythonic OO way is flexible and being explicit gives finely grained control • ipython : : : For interactive work. • numpy : : : For working with vectors and arrays. • scipy : : : All the essential scientific algorithms, including those for basic statistics. • matplotlib : : : The de-facto standard module for plotting and visualization. • pandas : : : Adds DataFrames (imagine powerful spreadsheets) to Python. • statsmodels : : : For statistical modeling and advanced analysis. • seaborn : : : For visualization of statistical data. • Scikit extensions to scipy for specific fields like machine learning, , deep learning, x-ray, image processing and many more • Additional Libraries are Plotly, Bokeh, TensorFlow, Keras, XGBoost STRUCTURE OF THE IMPORTANT PYTHON PACKAGES FOR DATA SCIENCE Code 10
  • 11. Ch Ch Ch Ch Ch Ch Ch DT TOPICS WE WILL COVER Three Layers of matplotlib architecture Anatomy of a figure Artist Hierarchy Pyplot API Object oriented API - for control and Customisation •Backend layer •Artist layer •Scripting layer • Overview of Data Structures • Pseudo Random Number Generation • Handling Date and Time • Statistics with Python • Appendix – Matplotlib plot examples • References 11
  • 12. Ch Ch Ch Ch Ch Ch Ch DT *https://aosabook.org/en/matplotlib.html *Matplotlib Release 3.4.2, May 08, 2021, Page No. 460 *Mastering Matplotlib by Duncan M. McGreggor, Pg no.15 Matplotlib Hierarchy MATPLOTLIB ARCHITECTURE ü Enable the users to create, render, and update the figure objects. ü The Three layers can be visualised as stack from bottom to top ü The Three layers perform distinct role at different levels of awareness. Backend Backend Artist Backend Artist Scripting ü Backend layer is not aware of the Artist and the Scripting layer. It knows its own interfaces only. ü Artist Layer knows how to talk with the Backend Layer. ü Scripting Layer knows how to talk to both the Artist Layer and the Backend Layer. Logical Separation into Three Layers ISOLATION OF THE COMPLEXITIES IN EACH LAYER 12
  • 13. Ch Ch Ch Ch Ch Ch Ch DT *https://aosabook.org/en/matplotlib.html *Matplotlib Release 3.4.2, May 08, 2021, Page No. 460 *Mastering Matplotlib by Duncan M. McGreggor, Pg no.15 These objects are then rendered by the backend, which ultimately provides the configured toolkit with the raw data necessary to place an image on the toolkit's canvas The data is transformed into various objects in the artist layer; it is adjusted as scripted This data is either created or loaded in the scripting layer The user creates either the data that he/she wants to plot or the functions that generate this data Matplotlib Hierarchy RELATIONSHIP OF THE LAYERS TO DATA AND THE FIGURE OBJECT FOR A GIVEN PLOT 13
  • 14. Ch Ch Ch Ch Ch Ch Ch DT *https://aosabook.org/en/matplotlib.html *Matplotlib Release 3.4.2, May 08, 2021, Page No. 460 *Mastering Matplotlib by Duncan M. McGreggor, Pg no.15 DEEP DIVE INTO BACKEND LAYER Backend Layer refers to matplotlib capabilities to render plots for different use cases and output formats. Use Matplotlib interactively from the python shell and have plotting windows pop up when they type commands. Some people run Jupyter notebooks and draw inline plots for quick data analysis. Others embed Matplotlib into graphical user interfaces like PyQt or PyGObject to build rich applications. Some people use Matplotlib in batch scripts to generate postscript images from numerical simulations Others run web application servers to dynamically serve up graphs. Matplotlib Hierarchy DIFFERENT USE CASES 14
  • 15. Ch Ch Ch Ch Ch Ch Ch DT *https://aosabook.org/en/matplotlib.html *Matplotlib Release 3.4.2, May 08, 2021, Page No. 460 *Mastering Matplotlib by Duncan M. McGreggor, Pg no.15 DEEP DIVE INTO BACKEND LAYER Backend Layer refers to matplotlib capabilities to render plots for different use cases and output formats. Matplotlib Hierarchy DIFFERENT OUTPUT FORMATS VECTOR OR RASTER BASED Vector Graphics use clean lines and shapes that can be scaled to any size Raster Graphics use pixels arranged in grid formation to represent an image 15
  • 16. Ch Ch Ch Ch Ch Ch Ch DT *https://aosabook.org/en/matplotlib.html *Matplotlib Release 3.4.2, May 08, 2021, Page No. 460 *Mastering Matplotlib by Duncan M. McGreggor, Pg no.15 DEEP DIVE INTO BACKEND LAYER Backend Layer refers to matplotlib capabilities to render plots for different use cases and output formats. Renderer Output File Types Description ADD PNG raster graphics -- high quality images using the Anti-Grain Geometry engine PDF PDF vector graphics -- Portable Document Format PS PS, EPS vector graphics -- Postscript output SVG SVG vector graphics -- Scalable Vector Graphics PGF PGF, PDF vector graphics -- using the pgf package CAIRO PNG, PS, PDF, SVG raster or vector graphics -- using the Cairo library Matplotlib Hierarchy DIFFERENT OUTPUT FORMATS VECTOR OR RASTER BASED 16
  • 17. Ch Ch Ch Ch Ch Ch Ch DT *https://aosabook.org/en/matplotlib.html *Matplotlib Release 3.4.2, May 08, 2021, Page No. 460 *Mastering Matplotlib by Duncan M. McGreggor, Pg no.15 Hardcopy backends to make static image files (PNG, SVG, PDF, PS) DEEP DIVE INTO BACKEND LAYER User Interface Backends For use in • GTK 2.x and GTK 3.x • wxWidgets • Tk • Qt4 and Qt5 • Mac OS X Cocoa Backend Layer refers to matplotlib capabilities to render plots for different use cases and output formats. Matplotlib Hierarchy 17
  • 18. Ch Ch Ch Ch Ch Ch Ch DT *https://aosabook.org/en/matplotlib.html *Matplotlib Release 3.4.2, May 08, 2021, Page No. 460 *Mastering Matplotlib by Duncan M. McGreggor, Pg no.15 DEEP DIVE INTO ARTIST LAYER Matplotlib Hierarchy The Artist layer constitutes the bulk of what matplotlib actually does — the generation of the plots. Most work in the artist layer is performed by a number of classes, most of which are derived from the Artist base class. Let’s run through the sub classes in the Artist Layer… 18
  • 19. Ch Ch Ch Ch Ch Ch Ch DT *https://aosabook.org/en/matplotlib.html *Matplotlib Release 3.4.2, May 08, 2021, Page No. 460 *Mastering Matplotlib by Duncan M. McGreggor, Pg no.15 *Matplotlib Release 3.4.2, Chapter 18.36 matplotlib.patches, Pg No. 2326 Primitives Circle Rectangle Ellipse Polygon Fancybox Patch Path Patch Wedge Arrow text Line 2D DEEP DIVE INTO ARTIST LAYER The matplotlib artist primitives are classes of standard graphical objects that are supposed to be painted on a figure's canvas. Matplotlib Hierarchy 19
  • 20. Ch Ch Ch Ch Ch Ch Ch DT *https://aosabook.org/en/matplotlib.html *Matplotlib Release 3.4.2, May 08, 2021, Page No. 460 *Mastering Matplotlib by Duncan M. McGreggor, Pg no.15 *Matplotlib Release 3.4.2, Chapter 18.36 matplotlib.patches, Pg No. 2326 Primitives – Patches Classes Circle Rectangle Ellipse Polygon Fancybox Patch Path Patch Wedge Arrow DEEP DIVE INTO ARTIST LAYER The matplotlib artist primitives are classes of standard graphical objects that are supposed to be painted on a figure's canvas. Matplotlib Hierarchy 20
  • 21. Ch Ch Ch Ch Ch Ch Ch DT *https://aosabook.org/en/matplotlib.html *Matplotlib Release 3.4.2, May 08, 2021, Page No. 460 *Mastering Matplotlib by Duncan M. McGreggor, Pg no.15 DEEP DIVE INTO ARTIST LAYER The matplotlib artist primitives are classes of standard graphical objects that are supposed to be painted on a figure's canvas. Containers offer a useful abstraction to gather primitives. Matplotlib Hierarchy Figure - Top level Artist, which holds all plot elements 21
  • 22. Ch Ch Ch Ch Ch Ch Ch DT *https://aosabook.org/en/matplotlib.html *Matplotlib Release 3.4.2, May 08, 2021, Page No. 460 *Mastering Matplotlib by Duncan M. McGreggor, Pg no.15 DEEP DIVE INTO ARTIST LAYER The matplotlib artist primitives are classes of standard graphical objects that are supposed to be painted on a figure's canvas. Containers offer a useful abstraction to gather primitives. Collections are the classes that provide for the efficient drawing of large numbers of similar objects. Matplotlib Hierarchy Ellipse Collection 22
  • 23. Ch Ch Ch Ch Ch Ch Ch DT *https://aosabook.org/en/matplotlib.html *Matplotlib Release 3.4.2, May 08, 2021, Page No. 460 *Mastering Matplotlib by Duncan M. McGreggor, Pg no.15 DEEP DIVE INTO ARTIST LAYER The matplotlib artist primitives are classes of standard graphical objects that are supposed to be painted on a figure's canvas. Containers offer a useful abstraction to gather primitives. Collections are the classes that provide for the efficient drawing of large numbers of similar objects. Matplotlib Hierarchy 23
  • 24. Ch Ch Ch Ch Ch Ch Ch DT *https://aosabook.org/en/matplotlib.html *Matplotlib Release 3.4.2, May 08, 2021, Page No. 460 *Mastering Matplotlib by Duncan M. McGreggor, Pg no.15 DEEP DIVE INTO ARTIST LAYER Create a Figure instance. Use the Figure to create one or more Axes or Subplot instances Use the Axes instance helper methods to create the primitives The Standard Approach to Plotting using Artists Matplotlib Hierarchy 24
  • 25. Ch Ch Ch Ch Ch Ch Ch DT We will explore the approach to plotting in more detail… But prior to that, it is important to know the elements (artists) of a matplotlib figure. 25
  • 26. Ch Ch Ch Ch Ch Ch Ch DT • Matplotlib Release 3.4.2, May 08, 2021, Pg No. 7-11 • https://dev.to/skotaro/artist-in-matplotlib---something-i-wanted-to-know-before-spending-tremendous-hours-on-googling-how-tos--31oo ANATOMY OF A FIGURE Line (line plot) Y axis label Figure X axis label Spines Major tick label Y axis label X axis label Line (line plot) Markers (scatter plot) Axes Grid Major tick Minor tick Minor tick label Legend Figure is top level container for all plot elements. It means the whole window in user interface. The Axes contains most of the figure elements: Axis, Tick, Line2D, Text, Polygon, etc., and sets the coordinate system. Figure can contain axes or subplots. Title 26
  • 27. Ch Ch Ch Ch Ch Ch Ch DT • Matplotlib Release 3.4.2, May 08, 2021, Pg No. 7-11 • https://dev.to/skotaro/artist-in-matplotlib---something-i-wanted-to-know-before-spending-tremendous-hours-on-googling-how-tos--31oo ANATOMY OF A FIGURE Line (line plot) Figure Spines Major tick label Y axis label X axis label Line (line plot) Markers (scatter plot) Axes Grid Major tick Minor tick Minor tick label Legend Figure is top level container for all plot elements. It means the whole window in user interface. The Axes contains most of the figure elements: Axis, Tick, Line2D, Text, Polygon, etc., and sets the coordinate system. Figure can contain axes or subplots. Title 27
  • 28. Ch Ch Ch Ch Ch Ch Ch DT • Matplotlib Release 3.4.2, May 08, 2021, Pg No. 7-11 • https://dev.to/skotaro/artist-in-matplotlib---something-i-wanted-to-know-before-spending-tremendous-hours-on-googling-how-tos--31oo ANATOMY OF A FIGURE Line (line plot) Figure Spines Major tick label Y axis label X axis label Line (line plot) Markers (scatter plot) Axes Grid Major tick Minor tick Minor tick label Legend Figure is top level container for all plot elements. It means the whole window in user interface. The Axes contains most of the figure elements: Axis, Tick, Line2D, Text, Polygon, etc., and sets the coordinate system. Figure can contain axes or subplots. Title 28
  • 29. Ch Ch Ch Ch Ch Ch Ch DT • Matplotlib Release 3.4.2, May 08, 2021, Pg No. 7-11 • https://dev.to/skotaro/artist-in-matplotlib---something-i-wanted-to-know-before-spending-tremendous-hours-on-googling-how-tos--31oo ANATOMY OF A FIGURE Basically, everything you can see on the figure is an artist (even the Figure, Axes, and Axis objects). This includes Text objects, Line2D objects, collections objects, Patch objects.. When the figure is rendered, all of the artists are drawn to the canvas. Most Artists are tied to an Axes; such an Artist cannot be shared by multiple Axes, or moved from one to another. Line (line plot) Figure Spines Major tick label Y axis label X axis label Line (line plot) Markers (scatter plot) Axes Grid Major tick Minor tick Minor tick label Legend Title 29
  • 30. Ch Ch Ch Ch Ch Ch Ch DT • Matplotlib Release 3.4.2, May 08, 2021, Pg No. 7-11 • https://dev.to/skotaro/artist-in-matplotlib---something-i-wanted-to-know-before-spending-tremendous-hours-on-googling-how-tos--31oo Containers Primitives Let’s run down through the hierarchy of Artists… Representing Containers by yellow colored boxes. Representing Primitives by Salmon colored boxes. 30
  • 31. Ch Ch Ch Ch Ch Ch Ch DT • Matplotlib Release 3.4.2, May 08, 2021, Pg No. 7-11 • https://dev.to/skotaro/artist-in-matplotlib---something-i-wanted-to-know-before-spending-tremendous-hours-on-googling-how-tos--31oo Figure Containers Primitives Figure is the Top Level container Figure can contain other Containers and Primitives. Figure can contain Axes and subplots. Artist Type Color Code The figure keeps track of all the child Axes, a smattering of ‘special’ artists (titles, figure legends, etc), and the canvas 31
  • 32. Ch Ch Ch Ch Ch Ch Ch DT • Matplotlib Release 3.4.2, May 08, 2021, Pg No. 7-11 • https://dev.to/skotaro/artist-in-matplotlib---something-i-wanted-to-know-before-spending-tremendous-hours-on-googling-how-tos--31oo Figure Containers Primitives Legend Rectangle Line2D Text Image Axes Artist Type Color Code The figure keeps track of all the child Axes, a smattering of ‘special’ artists (titles, figure legends, etc), and the canvas 32
  • 33. Ch Ch Ch Ch Ch Ch Ch DT • Matplotlib Release 3.4.2, May 08, 2021, Pg No. 7-11 • https://dev.to/skotaro/artist-in-matplotlib---something-i-wanted-to-know-before-spending-tremendous-hours-on-googling-how-tos--31oo Figure Containers Primitives Legend Rectangle Line2D Text Image Axes Artist Type Color Code Let us focus on Axes. The Axes class is one of the most important. It is the primary mover and shaker in the artist layer. The reason for this is simple—the Axes instances are where most of the matplotlib objects go (both primitives and other containers) Note the term ‘Axes‘ is not related to term ‘axis’. 33
  • 34. Ch Ch Ch Ch Ch Ch Ch DT • Matplotlib Release 3.4.2, May 08, 2021, Pg No. 7-11 • https://dev.to/skotaro/artist-in-matplotlib---something-i-wanted-to-know-before-spending-tremendous-hours-on-googling-how-tos--31oo Figure Containers Primitives YAxis Line2D PolyCollection Legend Rectangle Text Axes XAxis Artist Type Color Code “the Axes instances are where most of the matplotlib objects go (both primitives and other containers)“ 34
  • 35. Ch Ch Ch Ch Ch Ch Ch DT • Matplotlib Release 3.4.2, May 08, 2021, Pg No. 7-11 • https://dev.to/skotaro/artist-in-matplotlib---something-i-wanted-to-know-before-spending-tremendous-hours-on-googling-how-tos--31oo Figure Containers Primitives YAxis Line2D PolyCollection Legend Rectangle Text Axes XAxis Artist Type Color Code In addition to the creation of primitives, the methods of axes class can prepare the supplied data to : § create primitives such as lines and shapes § add primitives to the appropriate containers, and § draw artists when called by some other objects. 35
  • 36. Ch Ch Ch Ch Ch Ch Ch DT • Matplotlib Release 3.4.2, May 08, 2021, Pg No. 7-11 • https://dev.to/skotaro/artist-in-matplotlib---something-i-wanted-to-know-before-spending-tremendous-hours-on-googling-how-tos--31oo Figure Containers Primitives YAxis Line2D PolyCollection Legend Rectangle Text Axes XAxis Artist Type Color Code In addition to the creation of primitives, the methods of axes class can prepare the supplied data to : § create primitives such as lines and shapes § add primitives to the appropriate containers, and § draw artists when called by some other objects. 36
  • 37. Ch Ch Ch Ch Ch Ch Ch DT • Matplotlib Release 3.4.2, May 08, 2021, Pg No. 7-11 • https://dev.to/skotaro/artist-in-matplotlib---something-i-wanted-to-know-before-spending-tremendous-hours-on-googling-how-tos--31oo XAxis Label XAxis Tick Tick Major Line Tick Minor Line Tick Major Label Tick Minor Label Grid Line Let’s focus on the two important containers inside Axes : the XAxis and YAxis The Axis instances handle the drawing of : • tick lines • grid lines • tick labels • axis label Figure Containers Primitives YAxis Axes XAxis Artist Type Color Code YAxis Tick Tick Major Line Tick Minor Line Tick Major Label Tick Minor Label Grid Line YAxis Label 37
  • 38. Ch Ch Ch Ch Ch Ch Ch DT • Matplotlib Release 3.4.2, May 08, 2021, Pg No. 7-11 • https://dev.to/skotaro/artist-in-matplotlib---something-i-wanted-to-know-before-spending-tremendous-hours-on-googling-how-tos--31oo XAxis Label XAxis Tick Tick Major Line Tick Minor Line Tick Major Label Tick Minor Label Grid Line The Axis instances also store Locator and Formatter instances which control : • where the ticks are placed and • how they are represented as strings. Figure Containers Primitives YAxis Axes XAxis Artist Type Color Code YAxis Tick Tick Major Line Tick Minor Line Tick Major Label Tick Minor Label Grid Line YAxis Label 38
  • 39. Ch Ch Ch Ch Ch Ch Ch DT Having reviewed the Artists Hierarchy… We shall move on to the “Scripting Layer” 39
  • 40. Ch Ch Ch Ch Ch Ch Ch DT Two ways to use Matplotlib: • Pyplot API to automatically create and manage the figures and axes, and use pyplot functions for plotting • Explicitly create figures and axes, and call methods on them (the "object-oriented (OO) style") Matplotlib Hierarchy SCRIPTING LAYER – DUAL INTERFACE TO MATPLOTLIB API Scripting Layer is the User Facing Interface from where User can make calls to the Artist layer and backend layer. 40
  • 41. Ch Ch Ch Ch Ch Ch Ch DT PYPLOT INTERFACE TO MATPLOTLIB API Pyplot Interface is a Stateful Interface styled on MATLAB matplotlib.pyplot is a collection of functions that make matplotlib work like MATLAB. • Each pyplot function makes some change to a figure. • Various states are preserved across function calls, so that pyplot keeps track of things like the current figure and plotting area. • All plotting functions apply to the current axes. import matplotlib.pyplot as plt plt.plot([1, 2, 3, 4]) plt.ylabel('some numbers’) plt.show() Plot y versus x as lines and/or markers Set the label for the y-axis Display all open figures 41
  • 42. Ch Ch Ch Ch Ch Ch Ch DT Functions Description plot(*args[, scalex, scaley, data]) Plot y versus x as lines and/or markers. axes([arg]) Add an axes to the current figure and make it the current axes. axis(*args[, emit]) Convenience method to get or set some axis properties. xlabel(xlabel[, fontdict, labelpad, loc]) Set the label for the x-axis. ylabel(ylabel[, fontdict, labelpad, loc]) Set the label for the y-axis. xticks([ticks, labels]) Get or set the current tick locations and labels of the x-axis. yticks([ticks, labels]) Get or set the current tick locations and labels of the y-axis. grid Configure the grid lines xlim(*args, **kwargs) Get or set the x limits of the current axes. ylim(*args, **kwargs) Get or set the y-limits of the current axes. legend(*args, **kwargs) Place a legend on the Axes. figlegend(*args, **kwargs) Place a legend on the figure. savefig(*args, **kwargs) Save the current figure. show(*[, block]) Display all open figures. FREQUENTLY USED MATPLOTLIB.PYPLOT FUNCTIONS For the Figure, Axes, Axis The plt.axis() method allows you to set the x and y limits with a single call, by passing a list that specifies [xmin, xmax, ymin, ymax]. https://matplotlib.org/stable/api/pyplot_summary.html?highlight=pyplot#pyplot-function-overview 42
  • 43. Ch Ch Ch Ch Ch Ch Ch DT FREQUENTLY USED MATPLOTLIB.PYPLOT FUNCTIONS For the Figure, Axes, Axis Functions Description suptitle(t, **kwargs) Add a centered suptitle to the figure. annotate(text, xy, *args, **kwargs) Annotate the point xy with text text. text(x, y, s[, fontdict]) Add text to the Axes. title(label[, fontdict, loc, pad, y]) Set a title for the Axes. figtext(x, y, s[, fontdict]) Add text to figure. Text https://matplotlib.org/stable/api/pyplot_summary.html?highlight=pyplot#pyplot-function-overview 43
  • 44. Ch Ch Ch Ch Ch Ch Ch DT FREQUENTLY USED MATPLOTLIB.PYPLOT FUNCTIONS For the Figure, Axes, Axis Functions Description axhline([y, xmin, xmax]) Add a horizontal line across the axis. axvline([x, ymin, ymax]) Add a vertical line across the Axes. axhspan(ymin, ymax[, xmin, xmax]) Add a horizontal span (rectangle) across the Axes. axvspan(xmin, xmax[, ymin, ymax]) Add a vertical span (rectangle) across the Axes. Line, Span https://matplotlib.org/stable/api/pyplot_summary.html?highlight=pyplot#pyplot-function-overview 44
  • 45. Ch Ch Ch Ch Ch Ch Ch DT Functions Description bar(x, height[, width, bottom, align, data]) Make a bar plot. barh(y, width[, height, left, align]) Make a horizontal bar plot. bar_label(container[, labels, fmt, ...]) Label a bar plot. scatter(x, y[, s, c, marker, cmap, norm, ...]) A scatter plot of y vs. x colorbar([mappable, cax, ax]) Add a colorbar to a plot hist(x[, bins, range, density, weights, ...]) Plot a histogram. hist2d(x, y[, bins, range, density, ...]) Make a 2D histogram plot. boxplot(x[, notch, sym, vert, whis, ...]) Make a box and whisker plot. violinplot(dataset[, positions, vert, ...]) Make a violin plot. stackplot(x, *args[, labels, colors, ...]) Draw a stacked area plot. pie(x[, explode, labels, colors, autopct, ...]) Plot a pie chart. stem(*args[, linefmt, markerfmt, basefmt, ...]) Create a stem plot. step(x, y, *args[, where, data]) Make a step plot. hexbin(x, y[, C, gridsize, bins, xscale, ...]) Make a 2D hexagonal binning plot of points x, y. table([cellText, cellColours, cellLoc, ...]) Add a table to an Axes. FREQUENTLY USED MATPLOTLIB.PYPLOT FUNCTIONS For Charting https://matplotlib.org/stable/api/pyplot_summary.html?highlight=pyplot#pyplot-function-overview 45
  • 46. Ch Ch Ch Ch Ch Ch Ch DT FREQUENTLY USED MATPLOTLIB.PYPLOT FUNCTIONS For Charting Functions Description hlines(y, xmin, xmax[, colors, linestyles, ...]) Plot horizontal lines at each y from xmin to xmax. vlines(x, ymin, ymax[, colors, linestyles, ...]) Plot vertical lines at each x from ymin to ymax. fill(*args[, data]) Plot filled polygons. fill_between(x, y1[, y2, where, ...]) Fill the area between two horizontal curves. fill_betweenx(y, x1[, x2, where, step, ...]) Fill the area between two vertical curves. Line, Area https://matplotlib.org/stable/api/pyplot_summary.html?highlight=pyplot#pyplot-function-overview 46
  • 47. Ch Ch Ch Ch Ch Ch Ch DT Functions Description gcf() Get the current figure. gca(**kwargs) Get the current Axes, creating one if necessary. gci() Get the current colorable artist. sca() Set the current Axes to ax and the current Figure to the parent of ax. cla() Clear the current axes. clf () Clear the current figure. delaxes([ax]) Remove an Axes (defaulting to the current axes) from its figure. findobj Find artist objects. getp(obj, *args, **kwargs) Return the value of an Artist's property, or print all of them. box([on]) Turn the axes box on or off on the current axes. get(obj, *args, **kwargs) Return the value of an Artist's property, or print all of them. setp(obj, *args, **kwargs) Set one or more properties on an Artist, or list allowed values. tick_params([axis]) Change the appearance of ticks, tick labels, and gridlines. locator_params([axis, tight]) Control behavior of major tick locators. minorticks_off() Remove minor ticks from the axes. minorticks_on() Display minor ticks on the axes FREQUENTLY USED MATPLOTLIB.PYPLOT FUNCTIONS Helper Methods Current Axes, Figure https://matplotlib.org/stable/api/pyplot_summary.html?highlight=pyplot#pyplot-function-overview Inspecting, Changing Properties 47
  • 48. Ch Ch Ch Ch Ch Ch Ch DT Functions Description autumn() Set the colormap to 'autumn'. bone() Set the colormap to 'bone'. cool() Set the colormap to 'cool'. copper() Set the colormap to 'copper'. flag() Set the colormap to 'flag'. gray() Set the colormap to 'gray'. hot() Set the colormap to 'hot'. hsv() Set the colormap to 'hsv'. inferno() Set the colormap to 'inferno'. jet() Set the colormap to 'jet'. magma() Set the colormap to 'magma'. nipy_spectral() Set the colormap to 'nipy_spectral'. pink() Set the colormap to 'pink'. FREQUENTLY USED MATPLOTLIB.PYPLOT FUNCTIONS For Colormaps, Pseudo Colorplots Functions Description pcolor(*args[, shading, alpha, norm, cmap, ...]) Create a pseudocolor plot with a non- regular rect- angular grid. pcolormesh(*args[, alpha, norm, cmap, ...]) Create a pseudocolor plot with a non- regular rect- angular grid. imread(fname[, format]) Read an image from a file into an array. imsave(fname, arr, **kwargs) Save an array as an image file. imshow(X[, cmap, norm, aspect, ...]) Display data as an image, i.e., on a 2D regular raster. https://matplotlib.org/stable/api/pyplot_summary.html?highlight=pyplot#pyplot-function-overview 48
  • 49. Ch Ch Ch Ch Ch Ch Ch DT Functions Description subplot(*args, **kwargs) Add an Axes to the current figure or retrieve an existing Axes. subplots([nrows, ncols, sharex, sharey, ...]) Create a figure and a set of subplots. subplot2grid(shape, loc[, rowspan, colspan, fig]) Create a subplot at a specific location inside a reg- ular grid. subplots_adjust([left, bottom, right, top, ...]) Adjust the subplot layout parameters. tight_layout(*[, pad, h_pad, w_pad, rect]) Adjust the padding between and around subplots. FREQUENTLY USED MATPLOTLIB.PYPLOT FUNCTIONS For Multiple plots, Layout Customization https://matplotlib.org/stable/api/pyplot_summary.html?highlight=pyplot#pyplot-function-overview 49
  • 50. Ch Ch Ch Ch Ch Ch Ch DT names = ['group_a', 'group_b', 'group_c'] values = [1, 10, 100] plt.figure(figsize=(9, 3)) plt.subplot(131) plt.bar(names, values) plt.subplot(132) plt.scatter(names, values) plt.subplot(133) plt.plot(names, values) plt.suptitle('Categorical Plotting’) plt.savefig('Categorical Plotting.png’, bbox_inches = "tight", pad_inches = 0.15, dpi=300) plt.show() Code Snippet plt.subplot adds axes to current figure. Everytime it is called, it resets the current axes. 131 means in a grid of one row by three columns, the subplot at index position 1 is set as the current axes. Subsequent pyplot functions will be applicable to current axes. 132 means in a grid of one row by three columns, the subplot at index position 2 is set as the current axes. A Bar chart is plotted on the current axes. A Scatter chart is plotted on the current axes. The current axes is reset. Current Axes is reset and a Line plot is drawn. Centered suptitle is set in the Figure. Figure is saved as png file. Figure is displayed in user screen Subplot function to add/change current axes Pyplot function on current axes 50
  • 51. Ch Ch Ch Ch Ch Ch Ch DT names = ['group_a', 'group_b', 'group_c'] values = [1, 10, 100] plt.figure(figsize=(9, 3)) plt.subplot(131) plt.bar(names, values) plt.subplot(132) plt.scatter(names, values) plt.subplot(133) plt.plot(names, values) plt.suptitle('Categorical Plotting’) plt.savefig('Categorical Plotting.png’, bbox_inches = "tight", pad_inches = 0.15, dpi=300) plt.show() Code Snippet Subplot function to add/change current axes Pyplot function on current axes 51
  • 52. Ch Ch Ch Ch Ch Ch Ch DT CONFIGURATIONS Functions Description rc(group, **kwargs) Set the current rcParams. group is the grouping for the rc, e.g., for lines.linewidth the group is lines, for axes.facecolor, the group is axes, and so on. Group may also be a list or tuple of group names, e.g., (xtick, ytick). kwargs is a dictionary attribute name/value pairs, rc_context([rc, fname]) Return a context manager for temporarily changing rcParams. rcdefaults() Restore the rcParams from Matplotlib's internal default style. FREQUENTLY USED MATPLOTLIB.PYPLOT FUNCTIONS For Fine Tuning your Plot Appearance Customizing Matplotlib Configurations Change rcParams settings *rc refers to Matplotlib runtime configurations import matplotlib as mpl mpl.rcParams['lines.linewidth'] import matplotlib as mpl mpl.rcParams['lines.linewidth’] = 2 Inspect Change *Matplotlib Release 3.4.2, May 08, 2021, Customizing Matplotlib with style sheets and rcParams, Pg No.84, 25 52
  • 53. Ch Ch Ch Ch Ch Ch Ch DT FREQUENTLY USED MATPLOTLIB.PYPLOT FUNCTIONS “rcParams” – dict like Global variable Stores settings Dictionary Like Variable that stores rc settings and Global to Matplotlib package Functions Description mpl.rcParamsDefaults Stores the default rcParams. mpl.rcParamsOrig Stores the rcParams in original matplotlibrc file mpl.rcParams Stores the current rcParams Settings Stored *Matplotlib Release 3.4.2, May 08, 2021, Customizing Matplotlib with style sheets and rcParams, Pg No.86 53
  • 54. Ch Ch Ch Ch Ch Ch Ch DT Import matplotlib.pyplot as plt from matplotlib import cycler colors = cycler('color’, ['#EE6666', '#3388BB’, '#9988DD', '#EECC55’, '#88BB44’, '#FFBBBB’]) plt.rc('axes', facecolor='#E6E6E6’, edgecolor='none', axisbelow=True, grid=True, prop_cycle=colors) plt.rc('grid', color='w', linestyle='solid’) plt.rc('xtick', direction='out', color='gray’) plt.rc('ytick', direction='out', color='gray’) plt.rc('patch', edgecolor='#E6E6E6’) plt.rc('lines', linewidth=2) Code Snippet for customizing configurations Before Customizing After Customizing rcParams function for global changes *Python Data Science Handbook by Jake VanderPlas, Page No.284 54
  • 55. Ch Ch Ch Ch Ch Ch Ch DT Code Snippet Before Reverting Back After restoring defaults mpl.rcParams.update(mpl.rcParamsOrig) mpl.rcParams.update(mpl.rcParamsDefaults) *https://stackoverflow.com/questions/26899310/python-seaborn-to-reset-back-to-the-matplotlib OR rcParams dictionary variables to revert back settings 55
  • 56. Ch Ch Ch Ch Ch Ch Ch DT import matplotlib as mpl my_params = mpl.rcParams # apply some change to the rcparams here mpl.rcParams.update(my_params) Code Snippet mpl.rcParams.update(mpl.rcParamsOrig) mpl.rcParams.update(mpl.rcParamsDefaults) *https://stackoverflow.com/questions/26899310/python-seaborn-to-reset-back-to-the-matplotlib Best Practices OR rcParams dictionary variables to revert back settings Store current rcParams as a variable prior to any change. Restore the rcParams with the earlier stored variable. Do some changes in rcParams, plot using new settings 56
  • 57. Ch Ch Ch Ch Ch Ch Ch DT FREQUENTLY USED MATPLOTLIB.PYPLOT FUNCTIONS STYLE SHEETS plt.style.use('stylename') stylename from any of the available style names : ['Solarize_Light2', '_classic_test_patch', 'bmh', 'classic', 'dark_background ', 'fast', 'fivethirtyeight', 'ggplot', 'grayscale', 'seaborn', 'seaborn- bright', 'seaborn-colorblind', 'seaborn-dark', 'seaborn-dark-palette', 'seaborn- darkgrid', 'seaborn-deep', 'seaborn-muted', 'seaborn-notebook', 'seaborn- paper', 'seaborn-pastel', 'seaborn-poster', 'seaborn-talk', 'seaborn-ticks', 'seaborn-white', 'seaborn-whitegrid', 'tableau-colorblind10 '] Use plt.style.available to get the available style names list *Matplotlib Release 3.4.2, May 08, 2021, Customizing Matplotlib with style sheets and rcParams, Pg No.84 For Fine Tuning your Plot Appearance Using StyleSheets Use plt.style.context to apply style temporarily. https://matplotlib.org/stable/gallery/style_sheets/style_sheets_reference.html 57
  • 58. Ch Ch Ch Ch Ch Ch Ch DT fig = plt.figure(dpi=100, figsize=(24, 20), tight_layout=True) available = ['default'] + plt.style.available x = ['Product A','Product B', 'Product C'] y = [100,130,200] for i, style in enumerate(available): with plt.style.context(style): ax = fig.add_subplot(6,5 , i + 1) ax.bar(x,y) ax.set_title(style) plt.savefig('All styles.png’, bbox_inches = "tight", pad_inches = 0.05, dpi=300) plt.show() Code Snippet for showcasing all styles All Available Stylesheets Code 58
  • 59. Ch Ch Ch Ch Ch Ch Ch DT Zooming in - First set of 6# Stylesheets Styles in this slide 'default’ 'Solarize_Light2' '_classic_test_patch' 'bmh' 'classic' 'dark_background ' 'fast' 'fivethirtyeight' 'ggplot' 'grayscale' 'seaborn' 'seaborn- bright' 'seaborn-colorblind' 'seaborn-dark' 'seaborn-dark-palette' 'seaborn-darkgrid' 'seaborn-deep' 'seaborn-muted' 'seaborn-notebook' 'seaborn-paper' 'seaborn-pastel' 'seaborn-poster' 'seaborn-talk' 'seaborn-ticks' 'seaborn-white' 'seaborn-whitegrid' 'tableau-colorblind10 ' Styles on Right 59
  • 60. Ch Ch Ch Ch Ch Ch Ch DT Styles in this slide 'default’ 'Solarize_Light2' '_classic_test_patch' 'bmh' 'classic' 'dark_background ' 'fast' 'fivethirtyeight' 'ggplot' 'grayscale' 'seaborn' 'seaborn- bright' 'seaborn-colorblind' 'seaborn-dark' 'seaborn-dark-palette' 'seaborn-darkgrid' 'seaborn-deep' 'seaborn-muted' 'seaborn-notebook' 'seaborn-paper' 'seaborn-pastel' 'seaborn-poster' 'seaborn-talk' 'seaborn-ticks' 'seaborn-white' 'seaborn-whitegrid' 'tableau-colorblind10 ' Styles on Right Zooming in - Second set of 6# Stylesheets 60
  • 61. Ch Ch Ch Ch Ch Ch Ch DT Styles in this slide 'default’ 'Solarize_Light2' '_classic_test_patch' 'bmh' 'classic' 'dark_background ' 'fast' 'fivethirtyeight' 'ggplot' 'grayscale' 'seaborn' 'seaborn- bright' 'seaborn-colorblind' 'seaborn-dark' 'seaborn-dark-palette' 'seaborn-darkgrid' 'seaborn-deep' 'seaborn-muted' 'seaborn-notebook' 'seaborn-paper' 'seaborn-pastel' 'seaborn-poster' 'seaborn-talk' 'seaborn-ticks' 'seaborn-white' 'seaborn-whitegrid' 'tableau-colorblind10 ' Styles on Right Zooming in - Third set of 6# Stylesheets 61
  • 62. Ch Ch Ch Ch Ch Ch Ch DT Styles in this slide 'default’ 'Solarize_Light2' '_classic_test_patch' 'bmh' 'classic' 'dark_background ' 'fast' 'fivethirtyeight' 'ggplot' 'grayscale' 'seaborn' 'seaborn- bright' 'seaborn-colorblind' 'seaborn-dark' 'seaborn-dark-palette' 'seaborn-darkgrid' 'seaborn-deep' 'seaborn-muted' 'seaborn-notebook' 'seaborn-paper' 'seaborn-pastel' 'seaborn-poster' 'seaborn-talk' 'seaborn-ticks' 'seaborn-white' 'seaborn-whitegrid' 'tableau-colorblind10 ' Styles on Right Zooming in - Fourth set of 6# Stylesheets 62
  • 63. Ch Ch Ch Ch Ch Ch Ch DT Styles in this slide 'default’ 'Solarize_Light2' '_classic_test_patch' 'bmh' 'classic' 'dark_background ' 'fast' 'fivethirtyeight' 'ggplot' 'grayscale' 'seaborn' 'seaborn- bright' 'seaborn-colorblind' 'seaborn-dark' 'seaborn-dark-palette' 'seaborn-darkgrid' 'seaborn-deep' 'seaborn-muted' 'seaborn-notebook' 'seaborn-paper' 'seaborn-pastel' 'seaborn-poster' 'seaborn-talk' 'seaborn-ticks' 'seaborn-white' 'seaborn-whitegrid' 'tableau-colorblind10 ' Zooming in - Fifth set of 3# Stylesheets Styles on Right 63
  • 64. Ch Ch Ch Ch Ch Ch Ch DT OO INTERFACE TO MATPLOTLIB ü A Stateless Interface ü Does not rely on the concept of current axes and current figure ü Explicitly object instances are named and methods invoked ü Anatomy of a figure and the Hierarchy of artists is respected ü Consider Pyplot Interface as a brief interlude ü It’s a good starting point ü Hands-on and convenient ü But to gain finely grained control over every aspect of plot ü OO is the way ü We will take it step by step ü Review matplotlib landscape ü Build on our learnings ü Partly incremental and partly undertake few leaps ü This is an attempt to bring a structure, build intuition to approach the plotting into breakable tasks ü The goal is also to be the go-to reference guide for the major part of business analytics related EDA ü Understand Matplotlib API ü Understand OO interface and settle on mix of OO and pyplot ü Eventually use OO interface seamlessly with Seaborn, Pandas, GeoPandas BROADER GOAL SPECIFIC OUTCOMES ENTRY POINT INFLECTION POINT ACTIONABLES 64
  • 65. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Helper Methods (Accessors) to access artists through attributes. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes Helper Methods to access x-axis and y-axis tick, tick labels and axis labels. 4 data = {'Water':465, 'Alchoholic Beverages':271, 'Carbonated soft drinks': 224, 'Milk and Dairy products': 260, 'New Drinks': 75, 'Fruits and Vegetable Juices': 86} x = list(data.keys()) y = list(data.values()) import matplotlib.pyplot as plt %matplotlib inline #Creating figure and axes instances fig, ax = plt.subplots(figsize = (10,5), frameon = True) #Creating barplot using axes.bar() method colors = [ plt.cm.Dark2(x) for x in np.linspace(0, 1, len(x))] ax.bar(x,y, color = colors) #Setting y limits yupperlimit = ax.get_ylim()[1]*1.1 ax.set_ylim(0,yupperlimit) #Making x-tick lines invisible [ ticks.set_visible(False] for ticks in ax.get_xticklines() ] #Rotating and aligning the xtick labels for labels in ax.get_xticklabels(): labels.set(size = 12, rotation = 15, rotation_mode = 'anchor’, ha = 'right') # Adding Annotations to place data labels for p in ax.patches: ax.text(x = p.get_x()+p.get_width()/2, y = ax.get_ylim()[1]*0.01+ p.get_height(), s = p.get_height(), ha = 'center', size = 12) #Setting the properties of rectangular patch in axes and figure ax.patch.set(facecolor = 'white') fig.patch.set(facecolor = 'white’, edgecolor = 'grey', lw = 4) ax.grid(True) #Setting axes title StrTitle = 'Consumption of Packed beverages in billion liters,n by Beverage Type, Global, 2019’ ax.set_title(StrTitle, size = 16) #Adding Legend labels and handles [p.set_label(x[i]) for i,p in zip(range(len(x)), ax.patches)] ax.legend(bbox_to_anchor = (1.04,.5), loc="upper left",borderaxespad=0) #Saving the figure locally fig.savefig('Consumption_2.png', dpi=300, format='png', bbox_inches='tight’) PREPARING THE DATA CODE SCRIPTING PLOTTING OUTPUT Add Legend and Set Title to the Axes 7 OO APPROACH – A COMPLETE RUNDOWN 65
  • 66. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Helper Methods (Accessors) to access artists through attributes. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes Helper Methods to access x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend and Set Title to the Axes 7 data = {'Water':465, 'Alchoholic Beverages':271, 'Carbonated soft drinks': 224, 'Milk and Dairy products': 260, 'New Drinks': 75, 'Fruits and Vegetable Juices': 86} x = list(data.keys()) y = list(data.values()) PREPARING THE DATA OO APPROACH – PREPARING THE DATA 66
  • 67. Ch Ch Ch Ch Ch Ch Ch DT Add Legend and Set Title to the Axes 7 Set/Modify the Artist properties after accessing the artists. 6 Use Axes Helper Methods (Accessors) to access artists through attributes. 5 Use Axes Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Use Figure to create one or more Axes or Subplot 2 Create a Figure instance 1 import matplotlib.pyplot as plt %matplotlib inline #Creating figure and axes instances fig, ax = plt.subplots(figsize = (10,5), frameon = True) #Creating barplot using axes.bar() method colors = [ plt.cm.Dark2(x) for x in np.linspace(0, 1, len(x))] #Accessing colors from colormap ax.bar(x,y, color = colors) #Setting y limits yupperlimit = ax.get_ylim()[1]*1.1 ax.set_ylim(0,yupperlimit) #Making x-tick lines invisible [ ticks.set_visible(False] for ticks in ax.get_xticklines() ] #Rotating and aligning the xtick labels for labels in ax.get_xticklabels(): labels.set(size = 12, rotation = 15, rotation_mode = 'anchor’, ha = 'right') # Adding Annotations to place data labels for p in ax.patches: ax.text(x = p.get_x()+p.get_width()/2, y = ax.get_ylim()[1]*0.01+ p.get_height(), s = p.get_height(), ha = 'center', size = 12) #Setting the properties of rectangular patch in axes and figure ax.patch.set(facecolor = 'white’) #OO style to set properties plt.setp(fig.patch, facecolor = 'white’, edgecolor = 'grey', lw = 4). #Pyplot style to set properties ax.grid(True) # Setting axes title StrTitle = 'Consumption of Packed beverages in billion liters,n by Beverage Type, Global, 2019’ ax.set_title(StrTitle, size = 16) # Adding Legend labels and handles [p.set_label(x[i]) for i,p in zip(range(len(x)), ax.patches)] ax.legend(bbox_to_anchor = (1.04,.5), loc="upper left",borderaxespad=0) fig.savefig('Consumption_2.png', dpi=300, format='png', bbox_inches='tight’) OO APPROACH – CODE SCRIPTING Check slide 67
  • 68. Ch Ch Ch Ch Ch Ch Ch DT Add Legend and Set Title to the Axes 7 Set/Modify the Artist properties after accessing the artists. 6 Use Axes Helper Methods (Accessors) to access artists through attributes. 5 Use Axes Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Use Figure to create one or more Axes or Subplot 2 Create a Figure instance 1 PLOTTING OUTPUT OO APPROACH – PLOTTING OUTPUT Check slide 68
  • 69. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 CREATING FIGURE AND AXES Method Description Page No. fig, ax = plt.subplots() Instantiate a figure and axes 2650 fig = plt.figure() Instantiate a figure 2528 METHODS TO CREATE NEW SUBPLOTS fig.subplots(nrows, ncols) Primary function to create and place all axes on the figure at once by dividing figure into grids 2117 fig.add_subplot( (nrows, ncols, index) Figure method to add axes by specifying the index in a grid layout pattern 2077, 2117 fig.add_gridspec( nrows, ncols) Figure method to return gridspec that specifies the geometry of the grid that a subplot will be placed. 2076, 2174 fig.add_subplot(SubplotSpec) Figure method to add axes by specifying the location of subplot in a GridSpec 2076 2174 fig.add_axes(rect) Figure method to add axes at any location within the Figure2074 fig.delaxes(axes object) Figure method to delete an axes ax.remove() Axes method to remove an axes CUSTOMIZE SUBPLOT fig.subplots_adjust(left, bottom, right, top, wspace, hspace ) Adjust the subplot layout parameters. 2119 tight_layout(pad, h_pad, w_pad, rect) Adjust the padding between and around subplots. 2124 ax.set_anchor(anchor) Define the anchor location. Anchor values can be ‘C’, N, NW,W, SW,S, SE,E,NE fig.align_labels() Align the labels of subplots in the same subplot column if label alignment is being done automatically 2080- 2082 fig.align_xlabels() fig.align_ylabels() OO APPROACH – A DEEP DIVE 69
  • 70. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 #Using fig.subplots to place multiple axes in the figure import matplotlib.pyplot as plt plt.style.use('seaborn’) %matplotlib inline fig = plt.figure() ax = fig.subplots(2,2) #Using fig.subplots import matplotlib.pyplot as plt plt.style.use('seaborn') %matplotlib inline rows, cols = 2,2 fig = plt.figure() ax = fig.subplots(rows,cols, sharex = True,sharey = True) #Adding text by looping through all the subplots/axes ax_iter = ax.flatten() for i,axes in enumerate(ax_iter): axes.text(0.5, 0.5, f'subplot{rows,cols,i}', ha='center', va='center', size=20, color="steelblue") axes.grid(False) DEMO OF FIG.SUBPLOTS() DEMO OF FIG.SUBPLOTS() AND ENUMERATE 70
  • 71. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 #Using add_subplots to add axes import matplotlib.pyplot as plt plt.style.use('seaborn’) %matplotlib inline fig = plt.figure() ax1 = fig.add_subplot(2,3,1) ax2 = fig.add_subplot(2, 3, (3,6)) #Using Gridspec with add_subplots import matplotlib.gridspec as gridspec import matplotlib.pyplot as plt fig = plt.figure(figsize=(12, 8)) spec = gridspec.GridSpec(nrows = 2, ncols = 2, wspace = 0.2, hspace=0.1) ax1 = fig.add_subplot(spec[0, 0]) ax2 = fig.add_subplot(spec[1, 0]) ax3 = fig.add_subplot(spec[:, 1]) ax1 ax2 ax1 ax2 ax3 Note : Indexing is 1 based. Note : Indexing is 0 based. 71
  • 72. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 #Using fig.add_axes() to add subplots import matplotlib.pyplot as plt plt.rcdefaults() fig = plt.figure() ax1 = fig.subplots() ax2 = fig.add_axes([0.3,0.7,0.4,0.1]) ax2.tick_params(labelsize=8, length=0) *Note : This method used to Add an axes at position rect[left, bottom,width, height] where all quantities are in fractions of figure width and height. 72
  • 73. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 AXES HELPER METHODs TO CREATE PRIMITIVES Axes helper method Artist Container annotate - text annotations Annotation ax.texts bar - bar charts Rectangle ax.patches and Barcontainer errorbar - error bar plots Line2D and Rectangle ax.lines and ax.patches fill - shared area Polygon ax.patches hist - histograms Rectangle ax.patches imshow - image data AxesImage ax.images legend - Axes legends Legend ax.legend() plot - xy plots Line2D ax.lines scatter - scatter charts PolyCollection ax.collections text - text Text ax.texts PRIMITIVES PRIMITIVES AXES HELPER METHOD CONTAINER CONTAINER ü To get a list of all containers in the axes, use ax.containers ü To get list of all artists in the axes, use ax.get_children() *Check Artist Tutorial 73
  • 74. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 Category Method Description Page No. Basic Axes.plot Plot y versus x as lines and/or markers. 1241 Axes.errorbar Plot y versus x as lines and/or markers with attached errorbars. 1241 Axes.scatter A scatter plot of y vs. 1241 Axes.plot_date Plot coercing the axis to treat floats as dates. 1241 Axes.step Make a step plot. 1241 Axes.loglog Make a plot with log scaling on both the x and y axis. 1241 Axes.semilogx Make a plot with log scaling on the x axis. 1241 Axes.semilogy Make a plot with log scaling on the y axis. 1241 Axes.fill_between Fill the area between two horizontal curves. 1241 Axes.fill_betweenx Fill the area between two vertical curves. 1241 Axes.bar Make a bar plot. 1241 Axes.barh Make a horizontal bar plot. 1241 Axes.bar_label Label a bar plot. 1241 Axes.stem Create a stem plot. 1242 Axes.eventplot Plot identical parallel lines at the given positions. 1242 Axes.pie Plot a pie chart. 1242 Axes.stackplot Draw a stacked area plot. 1242 Axes.broken_barh Plot a horizontal sequence of rectangles. 1242 Axes.vlines Plot vertical lines at each x from ymin to ymax. 1242 Axes.hlines Plot horizontal lines at each y from xmin to xmax. 1242 Axes.fill Plot filled polygons. 1242 AXES METHODS TO CREATE DIFFERENT PLOTS AND TEXT (1/3) 74
  • 75. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 Category Method Description Page No. Spans Axes.axhline Add a horizontal line across the axis. 1296 Axes.axhspan Add a horizontal span (rectangle) across the Axes. 1296 Axes.axvline Add a vertical line across the Axes. 1296 Axes.axvspan Add a vertical span (rectangle) across the Axes. 1296 Axes.axline Add an infinitely long straight line. 1296 Spectral Axes.xcorr Plot the cross correlation between x and y. 1307 Statistics Axes.boxplot Make a box and whisker plot. 1334 Axes.violinplot Make a violin plot. 1334 Axes.violin Drawing function for violin plots. 1334 Axes.bxp Drawing function for box and whisker plots. 1334 Binned Axes.hexbin Make a 2D hexagonal binning plot of points x, y. 1344 Axes.hist Plot a histogram. 1344 Axes.hist2d Make a 2D histogram plot. 1344 Axes.stairs A stepwise constant function as a line with bounding edges or a filled plot. 1344 Text and annotations Axes.annotate Axes.annotate(self, text, xy, *args, **kwargs) Annotate the point xy with text text. 1399 Axes.text Annotate the point xy with text text. 1399 Axes.table Add text to the Axes. 1399 Axes.arrow Add a table to an Axes. 1399 Axes.inset_axes Add an arrow to the Axes. 1399 Axes.indicate_inset Add a child inset Axes to this existing Axes. 1399 Axes.indicate_inset_z oom Add an inset indicator to the Axes. 1399 Axes.secondary_xaxisAdd an inset indicator rectangle to the Axes based on the axis limits for an inset_ax and draw connectors between inset_ax and the rectangle. 1399 Axes.secondary_yaxisAdd a second x-axis to this Axes. 1399 AXES METHODS TO CREATE DIFFERENT PLOTS AND TEXT (2/3) 75
  • 76. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 Category Method Description Page No. 2D Arrays Axes.imshow Display data as an image, i.e., on a 2D regular raster. 1369 Axes.matshow Plot the values of a 2D matrix or array as color-coded image. 1369 Axes.pcolor Create a pseudocolor plot with a non-regular rectangular grid. 1369 Axes.pcolorfast Create a pseudocolor plot with a non-regular rectangular grid. 1369 Axes.pcolormesh Create a pseudocolor plot with a non-regular rectangular grid. 1369 Axes.spy Plot the sparsity pattern of a 2D array. 1369 https://matplotlib.org/stable/api/axes_api.html?highlight=axes#module-matplotlib.axes AXES METHODS TO CREATE DIFFERENT PLOTS AND TEXT (3/3) 76
  • 77. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 matplotlib.collections.PolyCollection matplotlib.patches.Wedge Axes.patches Axes.texts matplotlib.patches.Rectangle Axes.patches Barcontainer matplotlib.legend.Legend matplotlib.patches.Rectangle Axes.patches Code Code Code Code 77
  • 78. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 Code Code 78
  • 79. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 VISUALIZING MULTI DIMENSIONAL DISTRIBUTIONS Code 79
  • 80. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 Source : https://www.machinelearningplus.com/plots/top-50-matplotlib-visualizations-the- master-plots-python/#13.-Diverging-Lollipop-Chart-with-Markers Code 80
  • 81. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 TICK TICK LABELS GRIDLINES 81
  • 82. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 Category Method Description Page No. Appearance ✓ Axes.axis Convenience method to get or set some axis properties. 1432 ✓ Axes.set_axis_off Turn the x- and y-axis off. 1432 ✓ Axes.set_axis_on Turn the x- and y-axis on. 1432 ✓ Axes.set_frame_on Set whether the axes rectangle patch is drawn. 1432 Axes.get_frame_on Get whether the axes rectangle patch is drawn. 1432 ✓ Axes.set_axisbelow Set whether axis ticks and gridlines are above or below most artists. 1432 Axes.get_axisbelow Get whether axis ticks and gridlines are above or below most artists. 1432 ✓ Axes.grid Configure the grid lines. 1432 Axes.get_facecolor Get the facecolor of the Axes. 1432 ✓ Axes.set_facecolor Set the facecolor of the Axes. 1432 Property Cycle ✓✓ Axes.set_prop_cycle Set the property cycle of the Axes. 1440 Axis / limits ✓ Axes.get_xaxis Return the XAxis instance. 1442 ✓ Axes.get_yaxis Return the YAxis instance. 1442 Axis limits and direction Axes.invert_xaxis Invert the x-axis. 1442 Axes.xaxis_inverted Return whether the xaxis is oriented in the "inverse" direction. 1442 ✓ Axes.invert_yaxis Invert the y-axis. 1442 Axes.yaxis_inverted Return whether the yaxis is oriented in the "inverse" direction. 1442 ✓✓ Axes.set_xlim Set the x-axis view limits. 1442 ✓ Axes.get_xlim Return the x-axis view limits. 1442 ✓✓ Axes.set_ylim Set the y-axis view limits. 1442 ✓ Axes.get_ylim Return the y-axis view limits. 1442 AXES HELPER METHODS FOR APPEARANCE, AXIS LIMITS ✓ indicates frequency and importance of usage of the method 82
  • 83. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 Category Method Description Page No. Ticks and Tick Labels Axes.set_xticks Set the xaxis' tick locations. 1491 Axes.get_xticks Return the xaxis' tick locations in data coordinates. 1491 ✓ Axes.set_xticklabels Set the xaxis' labels with list of string labels. 1491 ✓✓ Axes.get_xticklabels Get the xaxis' tick labels. 1491 ✓ Axes.get_xmajorticklabels Return the xaxis' major tick labels, as a list of Text. 1491 ✓ Axes.get_xminorticklabels Return the xaxis' minor tick labels, as a list of Text. 1491 ✓ Axes.get_xgridlines Return the xaxis' grid lines as a list of Line2Ds. 1491 ✓ Axes.get_xticklines Return the xaxis' tick lines as a list of Line2Ds. 1491 Axes.xaxis_date Set up axis ticks and labels to treat data along the xaxis as dates. 1491 Axes.set_yticks Set the yaxis' tick locations. 1491 Axes.get_yticks Return the yaxis' tick locations in data coordinates. 1491 ✓ Axes.set_yticklabels Set the yaxis' labels with list of string labels. 1491 ✓✓ Axes.get_yticklabels Get the yaxis' tick labels. 1491 ✓ Axes.get_ymajorticklabels Return the yaxis' major tick labels, as a list of Text. 1491 ✓ Axes.get_yminorticklabels Return the yaxis' minor tick labels, as a list of Text. 1491 ✓ Axes.get_ygridlines Return the yaxis' grid lines as a list of Line2Ds. 1491 ✓ Axes.get_yticklines Return the yaxis' tick lines as a list of Line2Ds. 1491 Axes.yaxis_date Set up axis ticks and labels to treat data along the yaxis as dates. 1491 ✓ Axes.minorticks_off Remove minor ticks from the axes. 1491 ✓ Axes.minorticks_on Display minor ticks on the axes. 1491 Axes.ticklabel_format Configure the ScalarFormatter used by default for linear axes. 1491 ✓✓ Axes.tick_params Change the appearance of ticks, tick labels, and gridlines. 1491 ✓ Axes.locator_params Control behavior of major tick locators. 1491 AXES HELPER METHODS FOR TICKS AND TICK LABELS, GRID LINES ✓ indicates frequency and importance of usage of the method Also, can use plt.yticks([ticks, labels]), plt.xticks([ticks,labels]) to set ticks and labels in one call.
  • 84. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 Category Method Description Page No. Axis labels, title, and legend ✓ Axes.set_xlabel Set the label for the x-axis. 1445 ✓ Axes.get_xlabel Get the xlabel text string. 1445 ✓ Axes.set_ylabel Set the label for the y-axis. 1445 ✓ Axes.get_ylabel Get the ylabel text string. 1445 ✓ Axes.set_title Set a title for the Axes. 1445 Axes.get_title Get an Axes title. 1445 ✓ Axes.legend Place a legend on the Axes. 1445 Axes.get_legend Return the Legend instance, or None if no legend is defined. 1445 Axes.get_legend_handl es_labels Return handles and labels for legend 1445 ✓ Axes.set_xlabel Set the label for the x-axis. 1445 Axes.get_xlabel Get the xlabel text string. 1445 ✓ Axes.set_ylabel Set the label for the y-axis. 1445 Axes.get_ylabel Get the ylabel text string. 1445 ✓ Axes.set_title Set a title for the Axes. 1445 Twinning and sharing Axes.twinx Create a twin Axes sharing the xaxis. 1513 Axes.twiny Create a twin Axes sharing the yaxis. Check link. 1513 Axes.sharex Share the x-axis with other. 1513 Axes.sharey Share the y-axis with other. 1513 Axes.get_shared_x_axesReturn a reference to the shared axes Grouper object for x axes. 1513 Axes.get_shared_y_axes Return a reference to the shared axes Grouper object for y axes. 1513 AXES HELPER METHODS FOR AXIS LABELS, TITLE AND LEGEND ✓ indicates frequency and importance of usage of the method 84
  • 85. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 AXIS HELPER METHODS (1/3) Category Method Description Page No. Formatters and Locators Axis.get_major_formatter Get the formatter of the major ticker. 1540 Axis.get_major_locator Get the locator of the major ticker. 1540 Axis.get_minor_formatter Get the formatter of the minor ticker. 1540 Axis.get_minor_locator Get the locator of the minor ticker. 1540 ✓ Axis.set_major_formatter Set the formatter of the major ticker. 1540 ✓ Axis.set_major_locator Set the locator of the major ticker. 1540 ✓ Axis.set_minor_formatter Set the formatter of the minor ticker. 1540 ✓ Axis.set_minor_locator Set the locator of the minor ticker. 1540 Axis.remove_overlapping_locs If minor ticker locations that overlap with major ticker locations should be trimmed. 1540 Axis Label Axis.set_label_coords Set the coordinates of the label. 1545 ✓ Axis.set_label_position Set the label position (top or bottom) 1545 Axis.set_label_text Set the text value of the axis label. 1545 ✓ indicates frequency and importance of usage of the method 85
  • 86. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 AXIS HELPER METHODS (2/3) Category Method Description Page No. Ticks, tick labels, Offset text, GridLines ✓ Axis.get_major_ticks Return the list of major Ticks. 1547 ✓ Axis.get_majorticklabels Return this Axis' major tick labels, as a list of Text. 1547 ✓ Axis.get_majorticklines Return this Axis' major tick lines as a list of Line2Ds. 1547 Axis.get_majorticklocs Return this Axis' major tick locations in data coordinates. 1547 Axis.get_minor_ticks Return the list of minor Ticks. 1547 Axis.get_minorticklabels Return this Axis' minor tick labels, as a list of Text. 1547 Axis.get_minorticklines Return this Axis' minor tick lines as a list of Line2Ds. 1547 Axis.get_minorticklocs Return this Axis' minor tick locations in data coordinates. 1547 Axis.get_offset_text Return the axis offsetText as a Text instance. 1547 Axis.get_tick_padding 1547 ✓ Axis.get_ticklabels Get this Axis' tick labels. 1547 ✓ Axis.get_ticklines Return this Axis' tick lines as a list of Line2Ds. 1547 ✓ Axis.get_ticklocs Return this Axis' tick locations in data coordinates. 1547 ✓ Axis.get_gridlines Return this Axis' grid lines as a list of Line2Ds. 1547 ✓ Axis.grid Configure the grid lines. 1547 ✓✓ Axis.set_tick_params Set appearance parameters for ticks, ticklabels, and gridlines. 1547 Axis.axis_date Set up axis ticks and labels to treat data along this Axis as dates. 1547 ✓ indicates frequency and importance of usage of the method 86
  • 87. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 AXIS HELPER METHODS (3/3) Category Method Description Page No. XAxis Specific XAxis.axis_name Read-only name identifying the axis. 1559 XAxis.get_text_heights Return how much space should be reserved for text above and below the axes, as a pair of floats. 1559 XAxis.get_ticks_position Return the ticks position ("top", "bottom", "default", or "unknown"). 1559 ✓ XAxis.set_ticks_position Set the ticks position. 1559 ✓ XAxis.tick_bottom Move ticks and ticklabels (if present) to the bottom of the axes. 1559 ✓ XAxis.tick_top Move ticks and ticklabels (if present) to the top of the axes. 1559 YAxis Specific YAxis.axis_name Read-only name identifying the axis. 1563 YAxis.get_text_widths 1563 YAxis.get_ticks_position Return the ticks position ("left", "right", "default", or "unknown"). 1563 YAxis.set_offset_position 1563 ✓ YAxis.set_ticks_position Set the ticks position. 1563 ✓ YAxis.tick_left Move ticks and ticklabels (if present) to the left of the axes. 1563 ✓ YAxis.tick_right Move ticks and ticklabels (if present) to the right of the axes. 1563 Other ✓ Axis.axes The Axes instance the artist resides in, or None. 1563 Use with Caution* Axis.set_ticks Set this Axis' tick locations. 1564 Axis.set_ticklabels Set the text values of the tick labels. 1565 ✓ indicates frequency and importance of usage of the method *Warning :This method should only be used after fixing the tick positions using Axis.set_ticks. Otherwise, the labels may end up in unexpected positions. 87
  • 88. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 TICK PARAMS METHOD – BULK PROPERTY SETTER Tick Params to change the appearance of ticks, tick labels, and gridlines. Call signature : Axes.tick_params(), axis.set_tick_params() Parameters Description axis : {'x', 'y', 'both'}, default: 'both' The axis to which the parameters are applied. which : {'major', 'minor', 'both'}, default: 'major' The group of ticks to which the parameters are applied. reset : bool, default: False Whether to reset the ticks to defaults before updating them. direction : {'in', 'out', 'inout'} Puts ticks inside the axes, outside the axes, or both. length : float Tick length in points. width : float Tick width in points. color : color Tick color. pad : float Distance in points between tick and label. labelsize : float or str Tick label font size in points or as a string (e.g., 'large'). labelcolor : color Tick label color. colors : color Tick color and label color. zorder: float Tick and label zorder. bottom, top, left, right : bool Whether to draw the respective ticks. labelbottom, labeltop, labelleft, labelright :bool Whether to draw the respective tick labels. labelrotation : float Tick label rotation grid_color : color Gridline color. grid_alpha : float Transparency of gridlines: 0 (transparent) to 1 (opaque). grid_linewidth : float Width of gridlines in points. grid_linestyle : str Any valid Line2D line style spec. Enter length = 0 if tick lines are not desired Matplotlib Release 3.4.2 document, Pg No. 1491, 1504-1506 88
  • 89. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 A Note on Practical implementation Matplotlib Release 3.4.2 document, Pg No. 1491, 1504-1506 | Tick_Params | GridLines | Example_Demo | 1. Multiple ways to set the different parameters of ticks, tick labels and grid lines 2. Tick_params is a bulk property setter allowing all the ticks to be modified. Frequent use cases are : • Direction of the tick either inwards or outwards • Making tick lengths equal to zero • Placing Tick labels inside the axes • Adjusting the padding between the tick and labels • Setting the label color and label size 3. However, note that Tick_params does not have horizontal alignment or vertical alignment argument. 4. Use Axes.Axis.get_ticklabels() to access tick labels as text instances within a list. [In]: type(ax1.xaxis.get_ticklabels()[0]) [Out]: matplotlib.text.Text 5. We can Iterate/Loop over all the tick labels with or without conditions and then use plt.setp or artist.set() method to modify text instance properties. for labels in ax.yaxis.get_ticklabels(): plt.setp(labels, size = 14, rotation_mode = 'anchor’, ha = 'right’) ax.yaxis.set_tick_params(rotation =0, labelsize = 15 ) [ticks.set(alpha = 0.5) for ticks in ax.get_xticklabels()] [ticks.set(alpha = 0.5) for ticks in ax.get_yticklabels()] 6. Grid lines can be set by using Axes.grid(b = None, which = ‘major’, axis = ‘both’, **kwargs) or Axes.Axis.grid(b = None, which = ‘major’,**kwargs). 7. Use Axes.set_axisbelow(True) to place gridlines/tick lines behind or below all the artists. 8. Use plt.xticks([ticks, labels]), plt.yticks([ticks, labels]) to place ticks and labels in one call. This is helpful for simple use-cases. 9. Beyond simple usecases, it is preferable to use Tick Locator/Formatter pairs to customize the tick positions and the string formatting of the ticks. For general tick locator/formatter, check matplotlib.ticker module. For datetime tick locator/formatter, check matplotlib.dates module. Check Examples Check Examples 89
  • 90. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 TICK LOCATORS FORMATTERS 90
  • 91. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 Locator class Description NullLocator No ticks FixedLocator Tick locations are fixed IndexLocator Locator for index plots (e.g., where x = range(len(y))) LinearLocator Evenly spaced ticks from min to max LogLocator Logarithmically ticks from min to max MultipleLocator Ticks and range are a multiple of base MaxNLocator Finds up to a max number of ticks at nice locations AutoLocator (Default.) MaxNLocator with simple defaults. AutoMinorLocator Locator for minor ticks SPECIAL NOTE ON TICKERS – LOCATOR CLASS *Matplotlib Release, 3.4.2, Page No. 2812 *https://jakevdp.github.io/PythonDataScienceHandbook/04.10-customizing-ticks.html Tick locators define the position of the ticks. Use tick locator/formatter pairs for controlling tick position and string representation 91
  • 92. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 SPECIAL NOTE ON TICKERS – LOCATORs - DEMO 1 import matplotlib.pyplot as plt import matplotlib.ticker as ticker fig, ax = plt.subplots(2,2,figsize= (10,5), sharey = True) ax1,ax2,ax3,ax4 = ax.flatten() #Unpacking numpy array of axes [ax.set_ylim(0,20) for ax in fig.axes] #Setting ylim as (0,20) ax1.xaxis.set_major_locator(ticker.NullLocator()) ax2.xaxis.set_major_locator(ticker.MultipleLocator(2)) ax3.xaxis.set_major_locator(ticker.FixedLocator([0,1,2,8,9])) ax4.xaxis.set_major_locator(ticker.LinearLocator(5)) 92 code
  • 93. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 SPECIAL NOTE ON TICKERS – LOCATORs - DEMO 2 93
  • 94. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 SPECIAL NOTE ON TICKERS – FORMATTER CLASS *Matplotlib Release, 3.4.2, Page No. 2814 *https://jakevdp.github.io/PythonDataScienceHandbook/04.10-customizing-ticks.html Formatter Class Description NullFormatter No labels on the ticks IndexFormatter Set the strings from a list of labels FixedFormatter Set the strings manually for the labels FuncFormatter User-defined function sets the labels FormatStrFormatter Use a format string for each value ScalarFormatter (Default.) Formatter for scalar values LogFormatter Default formatter for log axes Tick formatters define how the numeric value associated with a tick on an axis is formatted as a string. Use tick locator/formatter pairs for controlling tick position and string representation 94
  • 95. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 SPECIAL NOTE ON TICKERS – USING SIMPLE FORMATTING Code 95
  • 96. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 SPECIAL NOTE ON TICKERS – USING FORMATTER CLASS Code 96
  • 97. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 TICKERS FOR TIME SERIES PLOTTING (1/3) The dateutil module provides additional code to handle date ticking, making it easy to place ticks on any kinds of dates. dateutil datetime The datetime module is python’s built-in module that supplies classes for manipulating dates and times. Ø Matplotlib handles dates in its own format. Ø Helper Methods are available to convert datetime objects and timedelta objects to matplotlib dates and vice-versa. powerful extensions to datetime Python’s built-in module to handle date/ time date only year, month and day datetime All components of time and date timedelta An amount of time with maximum unit of days time only time in hours, minutes, seconds and microseconds Check Link Four Main Classes tzinfo timezone TZ Awareness 97
  • 98. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 TICKERS FOR TIME SERIES PLOTTING (2/3) Helper methods in matplotlib.dates for conversion between datetime objects and Matplotlib dates METHOD DESCRIPTION datestr2num Convert a date string to a datenum using dateutil.parser.parse date2num Convert datetime objects to Matplotlib dates. num2date Convert Matplotlib dates to datetime objects. num2timedelta Convert number of days to a timedelta object. drange Return a sequence of equally spaced Matplotlib dates. set_epoch Set the epoch (origin for dates) for datetime calculations. get_epoch Get the epoch used by dates. datetime object timedelta object matplotlib date date2num num2date Go to Section 98
  • 99. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 DATE LOCATORS MicrosecondLocator Locate microseconds. SecondLocator Locate seconds. MinuteLocator Locate minutes. HourLocator Locate hours. DayLocator Locate specified days of the month. WeekdayLocator Locate days of the week, e.g., MO, TU. MonthLocator Locate months, e.g., 7 for July. YearLocator Locate years that are multiples of base. RRuleLocator Locate using a matplotlib.dates.rrulewrapper. rrulewrapper is a simple wrapper around dateutil's dateutil.rrule which allow almost arbitrary date tick specifications. See rrule example. AutoDateLocator On autoscale, this class picks the best DateLocator (e.g., RRuleLocator) to set the view limits and the tick locations. DATE FORMATTERS AutoDateFormatter attempts to figure out the best format to use. This is most useful when used with the AutoDateLocator. ConciseDateFormatter also attempts to figure out the best format to use, and to make the format as compact as possible while still having complete date information. This is most useful when used with the AutoDateLocator. DateFormatter use strftime format strings. IndexDateFormatter date plots with implicit x indexing. TICKERS FOR TIME SERIES PLOTTING (3/3) Tick locators and formatters from matplotlib.dates module 99
  • 100. Ch Ch Ch Ch Ch Ch Ch DT CONCISE DATE FORMATTER EXAMPLE https://matplotlib.org/stable/gallery/ticks_and_spines/date_concise_fo rmatter.html?highlight=date import datetime import matplotlib.pyplot as plt import matplotlib.dates as mdates # For Date locators and formatters import numpy as np base = datetime.datetime(2005, 2, 1) dates = [base + datetime.timedelta(hours=(2 * i)) for i in range(732)] N = len(dates) #Fixing random state for reproducibility np.random.seed(19680801) y = np.cumsum(np.random.randn(N)) #Limits for the three subplots/axes lims = [(np.datetime64('2005-02'), np.datetime64('2005-04')), (np.datetime64('2005-02-03'), np.datetime64('2005-02-15')), (np.datetime64('2005-02-03 11:00'), np.datetime64('2005-02-04 13:20'))] fig, axs = plt.subplots(3, 1, constrained_layout=True, figsize=(6, 6)) for nn, ax in enumerate(axs): locator = mdates.AutoDateLocator(minticks=3, maxticks=7) formatter = mdates.ConciseDateFormatter(locator) ax.xaxis.set_major_locator(locator) ax.xaxis.set_major_formatter(formatter) ax.plot(dates, y) ax.set_xlim(lims[nn]) axs[0].set_title('Concise Date Formatter’) plt.show() 100
  • 101. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 OO STYLE ESSENCE 101
  • 102. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 • In OO style, we want to access the artists first. • This would allow us to access the attributes and properties of the artist. • We can then modify the properties using setter methods. • We can also inspect into the attributes, add primitives into them and modify using appropriate methods. 102
  • 103. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 One way to gain access to the primitives is to assign it to a variable when it is created using Axes helper method. Instead of : ax.bar(x,y) #returns rectangular patch objects we should assign it to a variable as below : bars = ax.bar(x,y) Now, bars variable contain the rectangular patch objects that were returned by the ax.bar() method. We can use below methods to inspect the children of the bars variable: str(bars) After finding the index location of Barcontainer object, we will access the rectangular patches : bars[0].get_children() ACCESSING ARTISTS Ø BY ASSIGNING TO A VARIABLE WHEN CREATING 103
  • 104. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 The Second way is to access the attributes. In the current example, we will access the patches attribute of ax object. ax.bar(x,y) ax.patches We can then iterate through the patches in ax.patches. In a similar manner, we have access to other axes attributes ax.patch ax.patches ax.lines ax.texts ax.spines ax.legend() ax.collections ax.containers ACCESSING ARTISTS Ø THROUGH ATTRIBUTES (1/2) 104
  • 105. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 ITERATING OVER THE ARTIST ATTRIBUTES CONTAINERS bars = [rect for rect in ax.get_children() if isinstance(rect, mpl.patches.Rectangle)] [p.set_label(x[i]) for i,p in zip(range(len(x)), ax.patches)] [p.set_label(x[i]) for i,p in enumerate(ax.patches)] ACCESSING ARTISTS Ø THROUGH ATTRIBUTES (2/2) 105
  • 106. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 USEFUL METHODS AND PYTHON BUILT IN METHODS FOR INSPECTION • print(dir(object)) • vars(ax) or ax.__dict__ • str(ax) • type(object) • len(object) • isinstance(obj, class) • get_text() • hasattr(), getattr(), setattr() ARTIST OBJECT METHODS • matplotlib.artist.getp(artistobject) • plt.setp(artistobject) • artist.get_children() • artist.findobj() FOR LOOPING/ ITERATION • enumerate() • zip() • flatten() • ravel() • list comprehension • dict.key() • dict.values() • list() • Lambda function Check 106
  • 107. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 Axes attribute Description artists A list of Artist instances patch Rectangle instance for Axes background collections A list of Collection instances images A list of AxesImage legends A list of Legend instances lines A list of Line2D instances patches A list of Patch instances texts A list of Text instances xaxis A matplotlib.axis.XAxis instance yaxis A matplotlib.axis.YAxis instance Figure attribute Description axes A list of Axes instances (includes Subplot) patch The Rectangle background images A list of FigureImage patches - useful for raw pixel display legends A list of Figure Legend instances (different from Axes.legends) lines A list of Figure Line2D instances (rarely used, see Axes.lines) patches A list of Figure Patchs (rarely used, see Axes.patches) texts A list Figure Text instances Tick attribute Description tick1line A Line2D instance tick2line A Line2D instance gridline A Line2D instance label1 A Text instance label2 A Text instance *Each of these is accessible directly as an attribute of the Tick. *Matplotlib Release, 3.4.2, Page No. 7-11, 113-120 FIGURE AXES TICK ACCESSING ARTISTS THROUGH ATTRIBUTES AXIS Use Axis Accessor/Helper Methods to access axis labels, tick lines, tick labels, tick locators 107
  • 108. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 AXES MAPPING THE JOURNEY FROM AXES INSTANCE TO ARTIST INSTANCE PROPERTIES ARTISTS PROPERTIES ATTRIBUTES COMMON PROPERTIES ARTIST SPECIFIC PROPERTIES Use setter and getter methods Use dot operator to access attribute Change Multiple properties • Artist.set() • plt.setp() RECTANGLE PATCH LINE2D CLASS INSTANCES ATTRIBUTES PROPERTIES LABEL GUIDE Attributes are list of Artists objects. TEXT 108
  • 109. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists through attributes. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend and Set Title to the Axes 7 PROPERTIES COMMON TO ALL ARTISTS (1/2) PROPERTY DESCRIPTION alpha The transparency - a scalar from 0-1 animated A boolean that is used to facilitate animated drawing axes The Axes that the Artist lives in, possibly None clip_box The bounding box that clips the Artist clip_on Whether clipping is enabled clip_path The path the artist is clipped to contains A picking function to test whether the artist contains the pick point figure The figure instance the artist lives in, possibly None label A text label (e.g., for auto-labeling) picker A python object that controls object picking transform The transformation visible A boolean whether the artist should be drawn zorder A number which determines the drawing order rasterized Boolean; Turns vectors into raster graphics (for compression & EPS transparency) *Matplotlib Release, 3.4.2, Page No. 113-120 Ø “Each of the properties is accessed with setter or getter“ Ø (The exception being axes property which can be accessed by dot operator) ax.patch.get_alpha( ) ax.patch.set_alpha( ax.patch.get_apha()*1.2 ) ax.patch.set_alpha( 0.5 ) 109
  • 110. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists through attributes. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend and Set Title to the Axes 7 PROPERTIES COMMON TO ALL ARTISTS (2/2) USE CASE SCENARIO ARTIST PROPERTY For User-defined function figure, axes in conjunction with plt.gcf(), plt.gca(), plt.scf(), plt.sca() For Adding artist to Legend label Setting ticks, axis, spines or even axes(for multiple subplots) invisible visible If multiple artists overlap such as axhspan, axvspan, fillbetween zorder If text, patch, arrows, annotation etc. are to be added either in data coordinates or in figure coordinates transform : ax.transData | ax.transAxes | fig.transfigure | fig. dpi_scale_trans where ax is an axes instance, fig is a figure instance. Refer to official docs for full list. 110
  • 111. Ch Ch Ch Ch Ch Ch Ch DT • Use setp function of Pyplot Interface • Use Artist.set() method to set properties of this Axes object. SETTING ARTIST PROPERTIES # Preparing the Data group_names = ['Nexus', 'Chiffron', 'Pendos', 'Forresters'] group_data = [105000, 125000, 70500, 90000] plt.rcdefaults() fig, ax = plt.subplots(figsize=(8, 4)) #Instantiating fig and axes ax.barh(group_names, group_data, height = 0.8) # Accessing the artist using axes accessor method labels = ax.get_xticklabels() # Setting the artist property using plt.setp method plt.setp(labels, rotation=15, ha = 'right', fontsize = 10) # Setting the artist property using artist.set() method ax.set(xlim=[-10000, 140000], xlabel='Total Revenue', ylabel='Company', title='Company Revenue') # Setting the artist property using artist.set_p() method [bar.set_facecolor('xkcd:water blue') for bar in ax.patches] fig.savefig("Setting property.png",dpi=300, format='png', bbox_inches='tight') 111
  • 112. Ch Ch Ch Ch Ch Ch Ch DT • matplotlib.artist.getp(artistobject) • plt.setp(artistobject) INSPECTING ARTIST PROPERTIES import matplotlib.pyplot as plt lines = plt.plot([1, 2, 3]) plt.setp(lines) # list of settable line properties marker: marker style string, `~.path.Path` or `~.markers.MarkerStyle` markeredgecolor or mec: color markeredgewidth or mew: float markerfacecolor or mfc: color markerfacecoloralt or mfcalt: color markersize or ms: float markevery: None or int or (int, int) or slice or List[int] or float or (float, float) or List[bool] path_effects: `.AbstractPathEffect` picker: unknown pickradius: float rasterized: bool or None sketch_params: (scale: float, length: float, randomness: float) snap: bool or None solid_capstyle: {'butt', 'round', 'projecting’} solid_joinstyle: {'miter', 'round', 'bevel’} transform: `matplotlib.transforms.Transform` url: str visible: bool xdata: 1D array ydata: 1D array zorder: float agg_filter: a filter function, which takes a (m, n, 3) float array and a dpi value, and returns a (m, n, 3) array alpha: float or None animated: bool antialiased or aa: bool clip_box: `.Bbox` clip_on: bool clip_path: Patch or (Path, Transform) or None color or c: color contains: unknown dash_capstyle: {'butt', 'round', 'projecting’} dash_joinstyle: {'miter', 'round', 'bevel’} dashes: sequence of floats (on/off ink in points) or (None, None) data: (2, N) array or two 1D arrays drawstyle or ds: {'default', 'steps', 'steps-pre', 'steps-mid', 'steps- post'}, default: 'default’ figure: `.Figure` fillstyle: {'full', 'left', 'right', 'bottom', 'top', 'none’} gid: str in_layout: bool label: object linestyle or ls: {'-', '--', '-.', ':', '', (offset, on-off-seq), ...} linewidth or lw: float #matplotlib.artist.getp also returns a listing of properties for a given object. import matplotlib.pyplot as plt import matplotlib.artist as mpa lines = plt.plot([1, 2, 3]) mpa.getp(lines) *Matplotlib, Release 3.4.2, Pg No. 33,76,111 112
  • 113. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 PROPERTY DESCRIPTION agg_filter a filter function, which takes a (m, n, 3) float array and a dpi value, and returns a (m, n, 3) array antialiased or aa [True | False] color or c any matplotlib color dash_capstyle [‘butt’ | ‘round’ | ‘projecting’] dash_joinstyle [‘miter’ | ‘round’ | ‘bevel’] dashes sequence of on/off ink in points drawstyle [‘default’ | ‘steps’ | ‘steps-pre’ | ‘steps-mid’ | ‘steps-post’] fillstyle [‘full’ | ‘left’ | ‘right’ | ‘bottom’ | ‘top’ | ‘none’] gid an id string linestyle or ls [‘solid’ | ‘dashed’, ‘dashdot’, ‘dotted’ | (offset, on-off-dash- seq) | '-' | '--' | '-.' | ':' | 'None' | ' ' | ''] linewidth or lw float value in points marker A valid marker style markeredgecolor or mec any matplotlib color markeredgewidth or mew float value in points markerfacecolor or mfc any matplotlib color markerfacecoloralt or mfcalt any matplotlib color markersize or ms float markevery [None | int | length-2 tuple of int | slice | list/array of int | float | length-2 tuple of float] path_effects AbstractPathEffect pickradius float distance in points solid_capstyle [‘butt’ | ‘round’ | ‘projecting’] solid_joinstyle [‘miter’ | ‘round’ | ‘bevel’] url a url string xdata 1D array ydata 1D array *Matplotlib Release, 3.4.2, Page No. 2260-61 ADDITIONAL PROPERTIES SPECIFIC TO LINE2D 113
  • 114. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 PROPERTY DESCRIPTION agg_filter a filter function, which takes a (m, n, 3) float array and a dpi value, and returns a (m, n, 3) array backgroundcolor color bbox dict with properties for patches.FancyBboxPatch color or c color fontfamily or family {FONTNAME, 'serif', 'sans-serif', 'cursive', 'fantasy', 'monospace'} fontproperties or font or font_properties font_manager.FontProperties or str or pathlib.Path fontsize or size float or {'xx-small', 'x-small', 'small', 'medium', 'large', 'x-large', 'xx- large'} fontstretch or stretch horizontal condensation or expansion {a numeric value in range 0-1000, 'ultra-condensed', 'extra- condensed', 'condensed', 'semi-condensed', 'normal', 'semi- expanded', 'expanded', 'extra-expanded', 'ultra-expanded'} fontstyle or style {'normal', 'italic', 'oblique'} fontvariant or variant {'normal', 'small-caps'} fontweight or weight {a numeric value in range 0-1000, 'ultralight', 'light', 'normal', 'regular', 'book', 'medium', 'roman', 'semibold', 'demibold', 'demi', 'bold', 'heavy', 'extra bold', 'black'} gid str *Matplotlib Release, 3.4.2, Page No. 2795-2796, 326-329 ADDITIONAL PROPERTIES SPECIFIC TO TEXT (1/2) 114
  • 115. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 PROPERTY DESCRIPTION horizontalalignment or ha {'center', 'right', 'left'} in_layout bool linespacing float (multiple of font size) math_fontfamily str multialignment or ma {'left', 'right', 'center'} path_effects AbstractPathEffect position (float, float) rotation float or {'vertical', 'horizontal'} rotation_mode {None, 'default', 'anchor'} text object transform_rotates_text bool url str usetex bool or None verticalalignment or va {'center', 'top', 'bottom', 'baseline', 'center_baseline'} visible bool wrap bool x float y float zorder float url str usetex bool or None *Matplotlib Release, 3.4.2, Page No. 2795-2796 ADDITIONAL PROPERTIES SPECIFIC TO TEXT (2/2) 115
  • 116. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 TEXT INSTANCE – HORIZONTAL ALIGNMENT DEMO Code 116
  • 117. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 SPECIAL LOOK INTO PATCH RECTANGLE (x,y) width height (x,y) [(float, float)] The anchor point width [float] Rectangle width height [float] Rectangle height angle [float, default: 0] Rotation in degrees anti- clockwise about xy *Matplotlib Release, 3.4.2, Page No. 2409 Note: the float values are in the coordinate system for the data, controlled by xlim and ylim. RECTANGLE PARAMETERS 117
  • 118. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 PROPERTY DESCRIPTION agg_filter a filter function, which takes a (m, n, 3) float array and a dpi value, and returns a (m, n, 3) array antialiased or aa unknown capstyle CapStyle or {'butt', 'projecting', 'round'} color color edgecolor or ec color or None or 'auto' facecolor or fc color or None fill bool gid str hatch {'/', '', '|', '-', '+', 'x', 'o', 'O', '.', '*'} in_layout bool joinstyle JoinStyle or {'miter', 'round', 'bevel'} linestyle or ls {'-', '--', '-.', ':', '', (offset, on-off-seq), ...} linewidth or lw float or None path_effects AbstractPathEffect sketch_params (scale: float, length: float, randomness: float) snap bool or None url str ADDITIONAL PROPERTIES PATCH RECTANGLE SPECIFIC 118
  • 119. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 USE CASES – MODIFYING THE RECTANGLE PATCH PROPERTIES Patch instance Use Case Scenario fig.patch Changing color of figure background ax.patch Changing color of axes background ax.patches Adding data labels on top of the bars in a bar plot ax.patches Assigning label values to each of the patches of bar plot for adding to legend import matplotlib.pyplot as plt import numpy as np fig, (ax1, ax2) = plt.subplots(1,2, sharey = True) x = np.random.randint(1,20,20) #Set the face color of figure background rect = fig.patch rect.set(facecolor = 'aliceblue’) #Set the face color of the axes backgrounds ax1.patch.set(facecolor = 'salmon') ax2.patch.set(facecolor = 'aquamarine') ax2.plot(x) plt.show() 119
  • 120. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 METHODS OF RECTANGLE PATCH Rectangle Patch Method Description __str__(self ) Return str(self). get_bbox(self ) Return the Bbox. get_height(self ) Return the height of the rectangle. get_patch_transform(self ) Return the Transform instance mapping patch coordinates to data coordinates. get_path(self ) Return the vertices of the rectangle. get_width(self ) Return the width of the rectangle. get_x(self ) Return the left coordinate of the rectangle. get_xy(self ) Return the left and bottom coords of the rectangle as a tuple. get_y(self ) Return the bottom coordinate of the rectangle. set_bounds(self, *args) Set the bounds of the rectangle as left, bottom, width, height. The values may be passed as separate parameters or as a tuple: set_height(self, h) Set the height of the rectangle. set_width(self, w) Set the width of the rectangle. set_x(self, x) Set the left coordinate of the rectangle. *Matplotlib Release, 3.4.2, Page No. 2409 USE CASE SCENARIO LABEL PROPERTY For Adding data labels on top of the bars in bar plot get_x(), get_y(), get_height(), get_width() For User defined function axes property, figure property in user defined function to retrieve the axes and figure respectively. 120
  • 121. Ch Ch Ch Ch Ch Ch Ch DT import matplotlib.pyplot as plt import numpy as np import matplotlib.spines as spines colors = [ plt.cm.Dark2(x) for x in np.linspace(0, 1, len(x))] plt.rcdefaults() %matplotlib inline fig, ax = plt.subplots(figsize = (10,6), frameon = True) bars = ax.bar(x,y, color = colors) StrTitle = 'Consumption of Packed beverages in billion liters,n by Beverage Type, Global, 2019' ax.set_title(StrTitle, size = 16) Add_data_labels(ax.patches) stylize_axes(ax) fig.savefig('Patches.png', dpi=300, format='png', bbox_inches='tight') data = {'Water':465, 'Alchoholic Beverages':271, 'Carbonated soft drinks': 224, 'Milk and Dairy products': 260, 'New Drinks': 75, 'Fruits and Vegetable Juices': 86} x = list(data.keys()) y = list(data.values()) DEMO EXAMPLE – APPLYING LEARNINGS TILL NOW PREPARING SAMPLE DATA CODE SCRIPT PLOT OUTPUT • User defined function to stylize the axes. • Uses Axes helper methods to access and modify artist properties • User defined function to add data labels • Uses Axes helper methods to access the individual bars and use its properties to add texts Check slide 121
  • 122. Ch Ch Ch Ch Ch Ch Ch DT def stylize_axes(ax): #Making the axes background light gray ax.set_facecolor('.85’) # Setting the y limits ax.set_ylim(0,ax.get_ylim()[1]*1.1) #Making the tick lines disppear ax.tick_params(length=0) #Making the xtick labels rotated for labels in ax.get_xticklabels() : labels.set(size = 13,rotation = 15, rotation_mode = 'anchor’, ha = 'right’) #Setting grid lines to white color ax.grid(True, axis='x', color='white’) #Setting grid lines below all the artists ax.set_axisbelow(True) #Making all the spines invisible [spine.set_visible(False) for spine in ax.spines.values()] CREATING USER DEFINED FUNCTION TO STYLIZE AXES # Adding Annotations to place data labels def Add_data_labels(rect): #rect is ax.patches object for p in rect: #Retrieving the Axes container of patch object ax = rect[0].axes #Adding text iteratively on top of each bar #Using get and set on x,y parameters of rectangle patch ax.text( x = p.get_x()+p.get_width()/2, y = ax.get_ylim()[1]*0.01+ p.get_height(), s = p.get_height(), ha = 'center', size = 12) CREATING USER DEFINED FUNCTION TO ADD DATA LABELS IN BAR PLOTS 122
  • 123. Ch Ch Ch Ch Ch Ch Ch DT # Doing the necessary imports import matplotlib.pyplot as plt import numpy as np import matplotlib.spines as spines plt.rcdefaults() %matplotlib inline #create a list of rgba colors from a matplotlib.cm colormap colors = [ plt.cm.Dark2(x) for x in np.linspace(0, 1, len(x))] #Instantiating figure, axes fig, ax = plt.subplots(figsize = (10,6), frameon = True) #Creating a bar plot by ax.bar method # Note the colors passed as the color argument bars = ax.bar(x,y, color = colors) #Setting the title of plot StrTitle = 'Consumption of Packed beverages in billion liters,n by Beverage Type, Global, 2019' ax.set_title(StrTitle, size = 16) #Using the user defined functions – to add data labels and stylize the axes Add_data_labels(ax.patches) stylize_axes(ax) fig.savefig('Patches.png', dpi=300, format='png', bbox_inches='tight') DEMO - USERDEFINED FUNCTIONS TO STYLIZE PLOTS AND ADD LABELS 123
  • 124. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 TRANSFORMATIONS 124
  • 125. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 Coordinates Transformation object Description "data" ax.transData The coordinate system for the data, controlled by xlim and ylim. "axes" ax.transAxes The coordinate system of the Axes; (0, 0) is bottom left of the axes, and (1, 1) is top right of the axes. "subfigure" subfigure.transSubfigure The coordinate system of the SubFigure; (0, 0) is bottom left of the subfigure, and (1, 1) is top right of the subfigure. If a figure has no subfigures, this is the same as transFigure. "figure" fig.transFigure The coordinate system of the Figure; (0, 0) is bottom left of the figure, and (1, 1) is top right of the figure. "figure- inches" fig.dpi_scale_trans The coordinate system of the Figure in inches; (0, 0) is bottom left of the figure, and (width, height) is the top right of the figure in inches. "display" None, or IdentityTransform() The pixel coordinate system of the display window; (0, 0) is bottom left of the window, and (width, height) is top right of the display window in pixels. "xaxis", "yaxis" ax.get_xaxis_transform(), ax.get_yaxis_transform() Blended coordinate systems; use data coordinates on one of the axis and axes coordinates on the other. *Matplotlib Release, 3.4.2, Page No. 227 *https://aosabook.org/en/matplotlib.html TRANSFORMATION FRAMEWORK Use Transformation Objects to easily move between coordinate systems 125
  • 126. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 TRANSFORMATION FRAMEWORK – THE COORDINATE SYSTEMS The commonly used coordinate systems are : • Data • Axes • Figure All the above will be converted to Display coordinates by appropriate transformation objects. Code 126
  • 127. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 Userland data coordinate system axes coordinate System figure coordinate System display coordinate System ax.transData ax.transAxes fig.transFigure figure inches System fig.dpi_scale_trans display coordinate System Ø All of the transformation objects in the table above take inputs in their coordinate system and transform the input to the display coordinate system. Ø transform is the property common to all artists. Ø While it works under the hood not requiring to be explicitly mentioned, but for customization and more control, it is immensely powerful. Ø Use case : Ø Adding annotations/text Ø Adding horizontal/vertical spans Ø Legend positioning TRANSFORMATION FRAMEWORK – TRANSFORMATION OBJECTS Transformation Objects to easily move between coordinate systems 127
  • 128. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 BLENDED TRANSFORMATIONS - EXAMPLE x to be in data coordinates y to span from 0 -1 in axes coords. import numpy as np import matplotlib.transforms as transforms import matplotlib.pyplot as plt import matplotlib.patches as mpatches fig, ax = plt.subplots(figsize = (6,5)) x = np.random.randn(1000) ax.hist(x, 30) ax.set_title(r'$sigma=1 / dots / sigma=2$', fontsize=16) # the x coords of this transformation are data, and the y coord are axes trans = transforms.blended_transform_factory( ax.transData, ax.transAxes) # highlight the 1..2 std dev region with a span. # x to be in data coordinates # y to span from 0..1 in axes coords. rect = mpatches.Rectangle((1, 0), width=1, height=1, transform=trans, color='yellow', alpha=0.5) ax.add_patch(rect) ax.text(0.5,0.9, s = "Blended Coordinates Transformation - Demo", transform = fig.transFigure, ha = 'center', fontsize = 15, fontweight = 'medium’) fig.patch.set(fc = 'salmon', alpha = 0.1) plt.tight_layout(rect = [0,0,1,0.9]) https://matplotlib.org/stable/tutorials/advanced/transforms_tutorial.html 128
  • 129. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 COLORS 129
  • 130. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 Format Example 1. RGB or RGBA (red, green, blue, alpha) tuple of float values in a closed interval [0, 1]. • (0.1, 0.2, 0.5) • (0.1, 0.2, 0.5, 0.3) 2. Case-insensitive hex RGB or RGBA string. • '#0f0f0f' • '#0f0f0f80' 3. Case-insensitive RGB or RGBA string equivalent hex shorthand of duplicated characters. • '#abc' as '#aabbcc' • '#fb1' as '#ffbb11' 4. String representation of float value in closed interval [0, 1] for black and white, respectively. • '0.8' as light gray • '0' as black • '1' as white 5. Single character shorthand notation for shades of colors. Note : The colors green, cyan, magenta, and yellow do not coincide with X11/CSS4 colors. • 'b' as blue • 'g' as green • 'r' as red • 'c' as cyan • 'm' as magenta • 'y' as yellow • 'k' as black • 'w' as white 6. Case-insensitive X11/CSS4 color name with no spaces. • 'aquamarine' • 'mediumseagreen' 7. Case-insensitive color name from xkcd color survey with 'xkcd:'prefix. • 'xkcd:sky blue' • 'xkcd:eggshell' Specifying Colors – Acceptable Formats 130
  • 131. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 Format Example 8. Case-insensitive Tableau Colors from 'T10' categorical palette. Note : This is the default color cycle. 'tab:blue' 'tab:orange' 'tab:green' 'tab:red' 'tab:purple' 'tab:brown' 'tab:pink' 'tab:gray' 'tab:olive' 'tab:cyan' 9. "CN" color spec where 'C' precedes a number acting as an index into the default property cycle. Note : Matplotlib indexes color at draw time and defaults to black if cycle does not include color. The default property cycle is rcParams["axes.prop_cycle"] (default: cycler('color',['#1f77b4', '#ff7f0e', '#2ca 02c', '#d62728', '#9467bd','#8c564b', '#e377c2', '#7f7f7f', '#bcbd22', '#17becf '])) 'C0' 'C1' Ø The alpha for an Artist controls opacity. It indicates how the RGB color of the new Artist combines with RGB colors already on the Axes. zorder of the artist also becomes important. 131
  • 132. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 Ø Basic (8 colors) Ø Tableau Palette (10 colors) Ø CSS colors (140+ colors) Ø xkcd colors (954 colors) https://colorhunt.co/palette/ Exploring the Color Charts Handy go to reference to pick colors USE CASE SCENARIO ARTIST PROPERTY Assign face color, edge color of figure, axes edgecolor (ec) , facecolor (fc) Assign color to texts, axis labels, tick labels color or c Assign color to marker edge and face in line plot Markeredgecolor (mec), markerfacecolor(mfc) Initialize the color in bar plots, pie charts. Use Tableau palette. If more number of categories present, then colormaps and external libraries like [palettable] and [colorcet]. edgecolor (ec) , facecolor (fc) Setting the property cycle of the axes (Note : The property cycle controls the style properties such as color, marker and linestyle of future plot commands. The style properties of data already added to the Axes are not modified.) ax.set_prop_cycle(color=['red', 'green', 'blue']) 132
  • 133. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 133
  • 134. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 Axis accessor method Description get_scale The scale of the Axis, e.g., 'log' or 'linear' get_view_interval The interval instance of the Axis view limits get_data_interval The interval instance of the Axis data limits get_gridlines A list of grid lines for the Axis get_label The Axis label - a Text instance get_offset_text The Axis offset text - a Text instance get_ticklabels A list of Text instances - keyword minor=True|False get_ticklines A list of Line2D instances - keyword minor=True|False get_ticklocs A list of Tick locations - keyword minor=True|False get_major_locator The ticker.Locator instance for major ticks get_major_formatter The ticker.Formatter instance for major ticks get_minor_locator The ticker.Locator instance for minor ticks get_minor_formatter The ticker.Formatter instance for minor ticks get_major_ticks A list of Tick instances for major ticks get_minor_ticks A list of Tick instances for minor ticks grid Turn the grid on or off for the major or minor ticks 134
  • 135. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 http://cng.seas.rochester.edu/CNG/docs/x11color.html 135
  • 136. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 http://cng.seas.rochester.edu/CNG/docs/x11color.html 136
  • 137. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 137
  • 138. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 138
  • 139. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 139
  • 140. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 140
  • 141. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 141
  • 142. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 Code 142
  • 143. Ch Ch Ch Ch Ch Ch Ch DT Additional Colormaps External libraries like [palettable] and [colorcet] Use Case Scenarios For categorical bar plots and pie charts, use qualitative colormaps such as tab10 (10 colors) , tab20, tab20b, tab20c (20 colors), Paired (12 colors) Accessing a Colour map using plt.cm.colormapname(np.linspace(0,1, number of colors)) colors attribute of listed colormap followed by indexing if required Classes of colormaps Sequential, Diverging, Cyclic ,Qualitative What are color maps ? Built-in colormaps accessible via matplotlib.cm.get_cmap to map data on to color values. Note : Colormaps is a very rich immersive topic. Check out the official matplotlib website to dwelve deeper. 143
  • 144. Ch Ch Ch Ch Ch Ch Ch DT Note : Colormaps is a very rich immersive topic. Check out the official matplotlib website to dwelve deeper. Classes of colormaps Based on Function Colormap Category Use Case Sequential: change in lightness and often saturation of color incrementally, often using a single hue; for representing information that has ordering. Diverging: change in lightness and possibly saturation of two different colors that meet in the middle at an unsaturated color when the information being plotted has a critical middle value, such as topography or when the data deviates around zero. Cyclic: change in lightness of two different colors that meet in the middle and beginning/end at an unsaturated color; for values that wrap around at the endpoints, such as phase angle, wind direction, or time of day. Qualitative: often are miscellaneous colors to represent information which does not have ordering or relationships. https://matplotlib.org/stable/tutorials/colors/colormaps.html https://matplotlib.org/stable/api/pyplot_summary.html?highlight=pyplot 144
  • 146. Ch Ch Ch Ch Ch Ch Ch DT Note : All colormaps can be reversed by appending _r. 146
  • 147. Ch Ch Ch Ch Ch Ch Ch DT Note : All colormaps can be reversed by appending _r. 147
  • 152. Ch Ch Ch Ch Ch Ch Ch DT import numpy as np import matplotlib.pyplot as plt from matplotlib import cm #matplotlib.colors module from matplotlib.colors import ListedColormap, LinearSegmentedColormap #Get 8 colours from viridis colormap viridis = cm.get_cmap('viridis', 8) OR plt.cm.viridis(np.linspace(0, 1, 8)) # Get first 4# colors (0th – 3rd index) from the colormaps # Using range function to get colour from particular indices viridis(range(4)) OR viridis(np.linspace(0, 0.5, 4)) # Get 4# colors (4th-7th index) from the colormaps viridis(range(4,8)) OR viridis(np.linspace(0.5, 1, 4)) plt.cm.viridis(np.linspace(start = 0, stop = 1, num = 8 )) 0 1 ACCESSING COLORS FROM A MATPLOTLIB COLORMAP COLORMAP is a 1D Array of colours. matplotlib.colors module provides functions and classes for color specification conversions, and mapping data/numbers onto colours on a colormap. 0.14 0.56 0.42 0.28 0.70 0.85 0.00 1 *Colormap instances are used to convert data values (floats) from the interval [0, 1] to the RGBA color that the respective Colormap represents. For scaling of data into the [0, 1] interval see matplotlib.colors.Normalize. 152
  • 153. Ch Ch Ch Ch Ch Ch Ch DT # Necessary imports – Especially ListedColormap Class [1] : import numpy as np import matplotlib.pyplot as plt from matplotlib import cm from matplotlib.colors import ListedColormap from matplotlib.colors import LinearSegmentedColormap # Note : cm.get_cmap() returns a colormap [2] : type(cm.get_cmap('YlGn', 256)) [2] : matplotlib.colors.LinearSegmentedColormap [3] : type(cm.get_cmap(‘viridis’, 20)) [3] : matplotlib.colors.ListedColormap [4] : type(plt.cm.viridis(np.linspace(0, 1, 8))) #returns numpy array [4] : numpy.ndarray # Creating a Listed Colormap [5] : YlGn_New = cm.get_cmap('YlGn', 256) [6] : ListedColormap(YlGn_New(np.linspace(0,1,10))) [6] : <matplotlib.colors.ListedColormap at 0x7ffdd4625700> [7] : viridis_short = cm.get_cmap(‘viridis’,10) ListedColormap(viridis_short(np.linspace(0,1,10))) [8] : <matplotlib.colors.ListedColormap at 0x7ffdd4626700> CREATING LISTED COLORMAP FROM EXISTING MATPLOTLIB COLORMAPs Linear Segmented ColorMaps Listed ColorMaps • Perceptually uniform sequential • Qualitative • Sequential • Diverging • Cyclic USE CASE SCENARIO PARAMETER/PROPERTY Setting cmap in scatter charts cmap kwd argument Initialize the color in bar plots, pie charts with colors of listed colormap. Check out tab10, tab20. Also, External libraries like [palettable] and [colorcet]. facecolor property can take a single color or list of colors #colors = plt.cm.tab10.colors[:8] #colors = plt.cm.Paired.colors Setting the property cycle of the axes ax.set_prop_cycle(color=plt.cm.ta b10.colors) Colormap object generated from a list of colors. Ø most useful when indexing directly into a colormap Ø used to generate special colormaps for ordinary mapping. 153
  • 154. Ch Ch Ch Ch Ch Ch Ch DT from matplotlib.colors import ListedColormap colarray = plt.cm.plasma(range(256)) newcmap = ListedColormap(colarray) #Colormap can be called with an integer array, or with a float array between 0 and 1. newcmap.colors #colors attribute of a listed colormap # Above returns colors in RGBA tuples of float [0,1]. Shape (NX4 Array) newcmap(range(100,150)) #Returns colors from index 100 to 150 newcmap(np.linspace(0,1,20)) #Returns 20 colors corresponding to equally spaced positions in between the specified start and end newcmap([0.0, 0.1, 0.4, 0.8]) #Returns colors from specified locations considering the first color in newcmap is considered 0 and last colour is 1. #colors = [ plt.cm.tab20c(x) for x in np.linspace(0, 1, len(dic.values()) ] cmap = ListedColormap(["darkorange", "gold", "lawngreen", "lightseagreen"]) CODE CHUNK TO CREATE LISTED COLORMAPS AND ACCESSING COLORS 154
  • 155. Ch Ch Ch Ch Ch Ch Ch DT # Doing the necessary imports import numpy as np import matplotlib.pyplot as plt from matplotlib import cm from matplotlib.colors import ListedColormap plt.rcdefaults() %matplotlib inline # colarray = plt.cm.plasma(range(256)) newcmp = ListedColormap(colarray) #Creating the 1X256 Numpy Array (Matrix in Matlab) of float values from 0..1 Z = np.linspace(0,1,256)[np.newaxis,:] fig, (ax0, ax1) = plt.subplots(2, 1, figsize = (10,3)) #Creating a pseudocolor plot without distinguishable edge between values c = ax0.pcolor(Z,cmap = newcmp) ax0.set_title('default: no edges', fontsize = 20) #Creating a pseudocolor plot with distinguishable edge between the values c = ax1.pcolor(Z, cmap = newcmp, edgecolors='k', linewidths=1) ax1.set_title('thick edges',fontsize = 20) fig.tight_layout() fig.savefig('Plasma_256_colours.png',dpi=300, format='png’, bbox_inches='tight') #colors = plt.cm.Dark2(range(15)) #colors = plt.cm.tab20(range(15)) #colors = plt.cm.nipy_spectral(np.linspace(0,0.9,len(dic.values()))) #colors = plt.cm.CMRmap(np.linspace(0.2,1,len(dic.values()))) #colors = [ plt.cm.viridis(x) for x in np.linspace(0, 1, len(dic.values()))] #colors = [ plt.cm.Set3(x) for x in np.linspace(0, 1, len(dic.values()))] .colors attribute for listed colormaps #colors = plt.cm.tab10.colors #colors = plt.cm.Paired.colors COLORS FROM EXTERNAL LIBRARY PALETTABLE from palettable.cartocolors.qualitative import Bold_10 #colors = Bold_10.mpl_colors[:5] colors = Bold_10.mpl_colors Ø Refer to below Code from Matplotlib Gallery. Note slight tweaking was done to change the shape of the rectangular grid by using np.newaxis Ø https://matplotlib.org/stable/gallery/images_contours_and_fields/pcolor_demo.html #sphx-glr-gallery-images-contours-and-fields-pcolor-demo-py CODE SCRIPT TO CREATE PSEUDO COLOR PLOT READY TO USE CODE TO ACCESS COLORS FROM CMAPS 155
  • 156. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 156
  • 157. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 CONTROLLING THE LEGEND ENTRIES Matplotlib, Release 3.4.2, Page No. 2567, 2245, 1469 https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.legend.html?highlight=legend#matplotlib.axes.Axes.legend https://matplotlib.org/stable/tutorials/intermediate/legend_guide.html, Legend Demo CONTROLLING THE LEGEND LOCATION, PLACEMENT DEFINING CUSTOM LEGEND HANDLERS • The legend handler map specifies how to create legend handles from artists (lines, patches, etc.) in the axes or figures. • Custom legend handlers can be defined to support arbitrary objects not covered by default legend handlers. CREATING PROXY ARTISTS SPECIFICALLY FOR THE LEGEND • loc • bbox_to_anchor • bbox_transform • legend entry • legend handles • legend labels • legend keys • m.patches • m.lines • Not all handles can be turned into legend entries automatically, so it is often necessary to create an artist which can. • Legend handles don't have to exist on the Figure or Axes in order to be used. • Next up is specifying the location of the legend • Two parameters to control this : • loc • bbox_to_anchor h,l = ax.get_legend_handles_labels() • The legend module defines the Legend class responsible for drawing legends associated with axes and/or figures. • The Legend class is a container of legend handles and legend texts. 1 2 3 4 ASPECTS OF LEGEND 157
  • 158. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 handles [list of Artist] A list of Artists (lines, patches) to be added to the legend. labels [list of str] A list of labels to show next to the artists. The length of handles and labels should be the same. If they are not, they are truncated to the smaller of both lengths. Read more on legend entry, key, label and handle Adding Legend to Axes Call signatures: • legend() • legend(labels) • legend(handles, labels) ax.legend() Automatic detection of elements to be shown in the legend • Suffices for most use-cases • Requires explicitly labeling the artist either at the time of creation or before calling legend • Artist whose label either starts with underscore or does not have label property set will be ignored from the Legend ax.legend(labels) Labeling existing plot elements This method is discouraged owing to an implicit ordering of labels and artists. ax.legend(handles, labels) Explicitly defining the elements in the legend • Gives Total Control over the artists displayed in the legend • This method is explicit and will accept the handles and labels at face value. • This implies the labels will override the label names assigned at the time of creation. • Also, one can pass additional artists or subset of artist to the handles list. Matplotlib, Release 3.4.2, Page No. 2567, 2245, 1469 https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.legend.html?highlight=legend#matplotlib.axes.Axes.legend https://matplotlib.org/stable/tutorials/intermediate/legend_guide.html, Legend Demo 158
  • 159. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 legend entry A legend is made up of one or more legend entries. An entry is made up of exactly one key and one label. legend key The colored/patterned marker to the left of each legend label. legend label The text which describes the handle represented by the key. legend handle The original object which is used to generate an appropriate entry in the legend. Important Terminologies Matplotlib, Release 3.4.2, Page No. 2567, 2245, 1469 https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.legend.html?highlight=legend#matplotlib.axes.Axes.legend https://matplotlib.org/stable/tutorials/intermediate/legend_guide.html, Legend Demo 159
  • 160. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 import matplotlib.pyplot as plt import numpy as np %matplotlib inline plt.rcdefaults() np.random.seed(450) x = ['A', 'B','C','D'] y = np.random.randint(100,400,4) fig, ax = plt.subplots() p = ax.bar(x,y, color = plt.cm.Set2.colors) #Setting labels to each of the bars [bar.set(label = text) for text,bar in zip(x,p)] print(ax.get_legend_handles_labels()) #Calling the legend function for automatic detection of elements with labels to be shown ax.legend() Automatic detection of elements to be shown in the legend Artists with valid labels are automatically detected. To check the list of handles detected, use ax.get_legend_handles_labels() Matplotlib, Release 3.4.2, Page No. 2567, 2245, 1469 https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.legend.html?highlight=legend#matplotlib.axes.Axes.legend https://matplotlib.org/stable/tutorials/intermediate/legend_guide.html, Legend Demo 160
  • 161. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 Explicitly defining the elements in the legend For full control of which artists have a legend entry, it is possible to pass an iterable of legend artists followed by an iterable of legend labels respectively: import matplotlib.pyplot as plt import numpy as np plt.rcdefaults() np.random.seed(450) x = ['A', 'B','C','D'] y = np.random.randint(100,400,4) fig, ax = plt.subplots() p = ax.bar(x,y, color = plt.cm.Set2.colors) #Setting labels to each of the bars [bar.set(label = text) for text,bar in zip(x,p)] handles = [] for bar in p: if bar.get_label() not in [‘A',’D']: handles.append(bar) print(handles) #Check the handles list ax.legend(handles = handles) fig.savefig('Explicitly_Passing_Iterable of handles.png',dpi=300, format='png', bbox_inches='tight') Context : EXCLUDE FROM LEGEND ARTISTs WITH LABELS ’A’ or ‘D’ Note : Its okay to omit labels from the call signature if all the handles have valid labels. Infact, either give the correct desired labels in this call corresponding to all the handles or omit labels altogether. Labels Omitted 161
  • 162. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 For full control of which artists have a legend entry, it is possible to pass an iterable of legend artists followed by an iterable of legend labels respectively: import matplotlib.pyplot as plt import numpy as np plt.rcdefaults() np.random.seed(450) x = ['A', 'B','C','D'] y = np.random.randint(100,400,4) fig, ax = plt.subplots() p = ax.bar(x,y, color = plt.cm.Set2.colors) #Setting labels to each of the bars [bar.set(label = text) for text,bar in zip(x,p)] handles = [] for bar in p: if bar.get_label() in ['B','C’]: #filter ‘B’, ‘C’ handles.append(bar) labels = ['Brazil', 'Chile’] #Creating labels ax.legend(handles = handles, labels = labels) fig.savefig('Explicitly_Passing_Iterable_ handles_labels.png',dpi=300, format='png', bbox_inches='tight') Context : • INCLUDE IN LEGEND, ARTISTs WITH LABELS ’B’ or ‘C’ • TWEAK ‘B’ AND ‘C’ LABELS IN LEGEND Note : If labels is included in call signature, then labels in the labels list will override all pre- existing labels of the handles. Labels Included 162 Explicitly defining the elements in the legend
  • 163. Ch Ch Ch Ch Ch Ch Ch DT Creating Proxy Artists and Adding in the legend 1. Use ax.get_legend_handles_labels() to get existing handles and labels 2. Create instances of patch and Line2D without drawing them on the axes. Also, add label in initialization itself 3. Add the patch and Line2D to the list of handles from step 1 4. Call the axes.legend() method with below signature : ax.legend(handles=handles, **kwargs) ax.legend(handles=handles, labels = labels , **kwargs) import matplotlib.patches as mpatches import matplotlib.lines as mlines plt.rcdefaults() fig, ax = plt.subplots(figsize = (7,4)) # where some data has already been plotted to ax # manually define a new patch patch = mpatches.Patch(color='grey', label='Manual Label patch style’) # manually define a new line withot any marker line = mlines.Line2D([],[],color='blue', label='Manual Label line style') # manually define a new line with marker '*' marker1 = mlines.Line2D([],[],marker = '*', color='green’, label='Manual Label Marker style') # manually define a new line with linestyle None marker2 = mlines.Line2D([],[],ls = '',marker = '.', color='red’, label='Manual Label Marker without linestyle') # handles is a list, so append manual patch [handles.append(obj) for obj in [patch, line, marker1, marker2]] # plot the legend ax.legend(handles=handles, loc='upper right', bbox_to_anchor = (1,1), title = 'Legend') leg = ax.get_legend() #fetches/gets the legend object #Use print(dir(leg)) to get list of all attributes and method of legend class leg._legend_title_box._text.set_color('Brown') fig.savefig('Legend_Manual_positioning.png',dpi=150, format='png’, bbox_inches='tight') Demo Steps Matplotlib, Release 3.4.2, Page No. 2567, 2245, 1469 https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.legend.html?highlight=legend#matplotlib.axes.Axes.legend https://matplotlib.org/stable/tutorials/intermediate/legend_guide.html, Legend Demo 163 handles, labels = ax.get_legend_handles_labels()
  • 164. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 Controlling Legend Location, Placement • Use loc in conjunction with bbox_to_anchor Note : Use transform object in bbox_transform argument to be explicit about the coordinate system for the bbox_to_anchor arguments upper left upper center upper right center left center center right lower left lower center lower right Specifies the location of the legend 2 Tuple of floats 4 Tuple of floats Represents a point at which the legend corner specified by loc will be anchored. Represents a bounding box within a corner of which the legend will be placed. Corner is specified by loc. Legend --- A * B loc = “upper left” bbox(x, y, width, height) to place legend Conceptual OR bbox – a box bbox - a point 164
  • 165. Ch Ch Ch Ch Ch Ch Ch DT Use Axes Helper Methods to add artists to respective containers within the Axes object. 3 Create a Figure instance 1 Use Figure to create one or more Axes or Subplot 2 Use Axes Methods (Accessors) to access artists. 5 Set/Modify the Artist properties after accessing the artists 6 Use Axes/Axis Helper Methods to access and set x-axis and y-axis tick, tick labels and axis labels. 4 Add Legend, Annotations if any and Set Title to the Axes 7 #Setting labels to each of the bars [bar.set(label = text) for text,bar in zip(x,p)] #Creating handles list handles = [] for bar in p: if bar.get_label() in ['B','C']: handles.append(bar) #Creating labels list labels = ['Brazil', 'Chile’] #Creating Axes Legend ax.legend(handles = handles, labels = labels, loc = 'upper left', bbox_to_anchor = (0,1), bbox_transform = ax.transAxes) leg = ax.get_legend() leg.set_title('South America') #Creating Figure Legend fig.legend(handles = handles, labels = labels, loc = 'upper right’, bbox_to_anchor = (1,1)) fig.patch.set(fc = 'lightyellow’) fig.savefig('Legend_location.png',dpi=300, format='png', bbox_inches='tight') Demo import matplotlib.pyplot as plt import numpy as np plt.rcdefaults() np.random.seed(450) x = ['A', 'B','C','D'] y = np.random.randint(100,400,4) fig, ax = plt.subplots() p = ax.bar(x,y, color = plt.cm.Set2.colors) Controlling Legend Location, Placement Axes Legend loc = 'upper left’ bbox_to_anchor = (0,1) Figure Legend loc = 'upper right’ bbox_to_anchor = (1,1) 165
  • 166. Ch Ch Ch Ch Ch Ch Ch DT EPILOGUE We are at the end of this review of matplotlib. We have covered lots of ground. Indeed, the learning curve is steep, As there is a very large code base teeming with both stateless (pyplot) and stateful (OO style) interfaces. However, certain fundamentals remain the same. That being, Every element in the plot is an artist that knows how to render itself on the figure canvas. Every Artist instance has properties, parameters and possibly attributes. Axes and Figure are the most important Artist/Containers in the matplotlib universe. It is through the the Axes instance, one can access almost all the artists in the figure. Once having accessed the specific artist instance, the properties can be modified using artist.set method. At any moment one can chose to inspect the artist objects. Deeper Understanding of Matplotlib A deeper understanding of matplotlib can emerge by understanding “Anatomy of a Figure” and the hierarchy of artists. This should be followed by sections - “the Lifecycle of a Plot” and “the Artist Tutorial” in Matplotlib Release, 3.4.2. Without understanding of these two foundational concepts, we would be limiting our capability and fail to leverage the full range of matplotlib functions and different class modules. Further, most efforts in debugging and coding would materialize into suboptimal coding practices. In terms of the resources, the latest official documentation “Matplotlib Release, 3.4.2” pdf is a veritable treasure trove along with the examples gallery. Though, it may take some time to establish a pattern to search the required information. It also requires a threshold knowledge on the part of the user. One section to look out for is – “WHAT'S NEW IN MATPLOTLIB 3.4.0” for the latest developments and feature rollouts. Further, I also would suggest to check Matplotlib Release, 2.0.2 version especially for the matplotlib examples section. Apart from the matplotlib official site, StackOverflow is the go to site for all queries with an active community. 166
  • 167. Ch Ch Ch Ch Ch Ch Ch DT As we have limited our focus here on becoming comfortable on OO style, we have not covered numerous topic such as color bars, Tight Layout guide, Constraint layout guide, Different Patches and Advanced tutorials such as image tutorials, path tutorials. The official documentation has a comprehensive coverage on those. Seaborn, Pandas and Matplotlib Depending on specific use case, we shall go for the best combination. For sophisticated plots like heat maps or even faceted grids, Seaborn churns out beautiful charts with minimal code. For time series, Pandas has a definitive edge. Use matplotlib axes instance methods to customize them further. In that sense, the whole is indeed greater than the sum of parts. From R, ggplot2 to Matplotlib and Python For R Users, matplotlib can be analogous to R base graphics. And very likely, one used to the elegant graphics of ggplot2 can find the default matplotlib graphics code verbose and plot outputs very basic or “bare bones” similar to plots in R base graphics. But, it’s only a matter of time before one gets comfortable to the extent of customizing the plots even to the minutest level of setting orientation, thickness, number, padding of ticks. There is immense flexibility and clarity that is on offer within a clear OO framework of matplotlib. While I would rather not choose sides between ggplot2 and Python, instead am in favor of viewing them as tools for analysis to be used depending on particular use contexts. And while ggplot2 is easy to get started with but there is learning curve to gain control there in as well. 167 If you are majorly a Seaborn User, this review will let you finetune the seaborn plots. As Seaborn is a high-level wrapper for Matplotlib, in essence, the OO interface will work there as well. While Seaborn plots take very less code and is intuitive from the moment go, this comes at the cost of lesser flexibility unless complemented by matplotlib. For instance, in a facetgrid object, modifying the row titles, column titles or the placement of figure legends, subplot legends becomes fairly easy with understanding of OO style. Same goes with Pandas plotting. Pandas has well developed charting capabilities. Pandas has powerful functionalities to deal with time series data. Using the three in conjunction particularly in multiple subplots will let you extract the best from each of the modules. Yes, this is the way.
  • 168. Ch Ch Ch Ch Ch Ch Ch DT Having advocated the matplotlib skills for Python work environment, a suitable learning journey for R user can be becoming comfortable with Python basics, the basic data structures, built in functions, Numpy, Pandas followed by the pyplot interface and Seaborn. Pyplot interface, Seaborn and Pandas would be good entry points to be culminated with matplotlib. A Note on Excel And for Excel users, a reason for learning matplotlib and even ggplot2 can be the vast library of visualizations that is simply on offer. Excel spreadsheet shall always remain relevant and has its own place in analysis. But it is no match for advanced data analysis workflow with tidyverse packages in R; and numpy, Pandas in Python. For quick Exploratory data analysis, you have ggplot2 in R and matplotlib/Seaborn/pandas in Python. Also, there are some basic limitations in the charts you can plot in Excel. Trellis chart is not possible unless some tricks in terms of arranging the data in separate columns with blanks is employed. But this is only a trick/ manoeuvre around an inherent limitation. To give another limitation, there is no fill and span feature. If daily sales data is plotted across months and I want to highlight particular days and weekends, it’s a hard ask. This and lot of other limitations are easily handled in R and Python. One definitive edge of R and Python over excel is the reproducibility of the plots. While excel is mainly drag and drop, point and click type GUI, so every time you are repeating an analysis, there are number of tasks that have to be redone. Data cleaning and aggregation followed by pivoting and then copying/pasting the relevant outputs. And then doing some EDA and then some graphs if found useful. This entire flow can of course be automated by VBA but that would boil down to individual expertise and additional time of code development and debugging. And if the data is huge, excel is no longer conducive even for pivoting. In comparison, R and Python being code based have a much simpler workflow, less error prone, shorter throughput and exact reproducibility. For a Excel user to get started on matplotlib, the learning journey would be same as suggested previously for R users. To get a headstart, check out below link for comparison of Pandas data structures and spreadsheet : https://pandas.pydata.org/pandas-docs/stable/getting_started/comparison/comparison_with_spreadsheets.html 168
  • 169. Ch Ch Ch Ch Ch Ch Ch DT Back to this Review Coming back to matplotlib, I would also emphasize being thorough with the basics of the color module, transformation framework and legend guide. Also, few basics like modifying tick lines, tick labels, spines, gridlines, fig patch, axes patch can elevate the visual quality of plots. You also have rcparams and style sheets for global changes. Adding subplots using with and without gridspec also requires due consideration. For modifying font properties, using font dictionary is useful. There are also args and kwargs that you will frequently encounter with every artist instance. It is convenient to create a dictionary containing keyword argument and value pairings in the code script. And later use dictionary style unpacking (**dict) to pass those arguments for setting the artist property. This helps in keeping code organized and avoid repetition of keyword arguments. Certain properties are common to all artists. Check this useful link. And there are certain artist specific properties. In this review, I have included the artist methods and instances with high likelihood of use. However, I would strongly recommend being familiar with the official documentation and website for the complete list and final verdict. It is also inevitable that we will be dealing with date and time. There are multiple classes to handle the various concepts of date and time – either a single moment in time or a duration of time. To capture a moment in time, we have the datetime object in Native Python, datetime64 in Numpy, timestamp in Pandas, matplotlib dates. For parsing strings and ISO 8601 date time formats, we have dateutil parser; datetime strft, strfp; Pandas pd.to_pydatetime to name a few. TimeDelta objects are very handy and will be frequently used in conjunction with the datetime equivalent objects. Eventually, we shall develop liking to a method/module/class and stick with it. 169 Further, this review would be incomplete without coverage of basic data structures in Python, Numpy Array Attributes and Methods, Pandas Objects Attributes and Methods, Pseudo Random number generation in Python and Numpy, Scipy for Statistics with python. I have included short sections on them in the unabridged version to make this review self contained. However, I would recommend to go to official documentation of Pandas and Numpy for details.
  • 170. Ch Ch Ch Ch Ch Ch Ch DT The Curtain Falls ! But The End is only the beginning !! On a final note, I have included an appendix containing multiple examples of plots and code implementations with different use cases. The intent here is to cover a gamut of plotting scenarios that can be always be referred to and specific code snippets be adapted for specific contexts. The references/source links for code implementations wherever applicable has been mentioned. There is also a references list at the very end presenting useful links related to matplotlib, python and data viz in general. Feel free to go through it. To reaffirm, Consider this review as a compilation of distilled personal learnings and a handy reference. 170 With this, I wrap up this first edition and wish you the very best ! Bhaskar Aug’2021
  • 171. Ch Ch Ch Ch Ch Ch Ch DT THANK YOU ! bhaskarjroy1605@gmail.com www.linkedin.com/in/bhaskar-j-roy-1605 Matplotlib Complete Review Link Other Interests : Excel VBA | Documentary Film1 | Documentary Film2 171
  • 173. Ch Ch Ch Ch Ch Ch Ch DT These Containers are also called iterables. They are unidimensional. Check • Learning Python by Mark Lutz • Fluent Python by Luciano Ramalho • Python-based library for mathematical computations and processing arrays. • Homogenous Arrays of Fixed size. • Vectorization and Broadcasting makes numpy fast. • Check Official Link, Scipy Lectures • Check “Numerical Python: Scientific Computing and Data Science Applications with Numpy, SciPy and Matplotlib” by Robert Johansson ndarray (N dimensional Array) List Tuples Dictionaries Sets Series DataFrame Index Indexing Indexing is fundamental to Pandas and is what makes retrieval and access to data much faster compared to other tools. It is crucial to set an appropriate index to optimize performance (Check A Python Data Analyst’s Toolkit by Gayathri Rajagopalan) NumericIndex CategoricalIndex IntervalIndex The inbuilt data types and containers in Python cannot be restructured into more than one dimension, and also do not lend themselves to complex computations. For analyzing data and building models, arrays is a vital data structure. MultiIndex DatetimeIndex TimedeltaIndex PeriodIndex Python Numpy Pandas • Provides efficient implementation of a DataFrame. • DataFrames are essentially multidimensional arrays with attached row and column labels, and often with heterogeneous types and/or missing data • Powerful data wrangling operations similar to ones in database and spreadsheet framework Popular tool for data wrangling and analysis • Grouping (Split-Apply-Combine) • Handling missing data • Input/output support • Data Visualisation • Integration with Numpy, Matplotlib, Scipy, and Scikit-learn • Check • Pandas online documention • Stackoverflow • Python Data Science Handbook Overview of Data Structures • Python • Numpy • Pandas Pandas •Pandas is implemented on top of NumPy arrays. •A Pandas dataframe is used to store a structured dataset. It is more like a spreadsheet than an array.. NumPy provides: •extension package to Python for multi- dimensional arrays •closer to hardware (efficiency) •designed for scientific computation (convenience) •Also known as array oriented computing Python objects: •high-level number objects: integers, floating point •containers: lists (costless insertion and append), dictionaries (fast lookup) 173
  • 174. Ch Ch Ch Ch Ch Ch Ch DT Arithmetic Operators Operator Name Example + Addition x + y - Subtraction x - y * Multiplication x * y / Division x / y % Modulus x % y ** Exponentiation x ** y // Floor division x // y Assignment Operators Operator Example Same As = x = 5 x = 5 += x += 3 x = x + 3 -= x -= 3 x = x - 3 *= x *= 3 x = x * 3 /= x /= 3 x = x / 3 %= x %= 3 x = x % 3 //= x //= 3 x = x // 3 **= x **= 3 x = x ** 3 &= x &= 3 x = x & 3 |= x |= 3 x = x | 3 ^= x ^= 3 x = x ^ 3 >>= x >>= 3 x = x >> 3 <<= x <<= 3 x = x << 3 Comparison Operators Operator Name Example == Equal x == y != Not equal x != y > Greater than x > y < Less than x < y >= Greater than or equal to x >= y <= Less than or equal to x <= y Logical Operators : used to combine conditional statements Operator Description Example and Returns True if both statements are true x <15 and x > 5 or Returns True if one of the statements is true x < 5 or y <10 not Reverse the result, returns False if the result is true not(x < 5 or y <10) Membership Operators : used to test if a sequence is presented in an objet Operator Description Example in Returns True if a sequence with the specified value is present in the object x in y not in Returns True if a sequence with the specified value is not present in the object x not in y Identity Operators : used to compare the objects, not if they are equal, but if they are actually the same object, with the same memory location: Operator Description Example is Returns True if both variables are the same object x is y is not Returns True if both variables are not the same object x is not y Difference between float division and floor division Link / Division x / y 101/25 = 4.04 (Python 3.X version) Not consistent across different versions of Python (referred to as floating point division in number of versions) % Modulus x % y 101/25 = 1 Return the remainder // Floor division x // y 101//25 = 4 Always Return floored quotient of x and y. (integer floor of division) Operators in Python 174 Also, check slide no.339 for ceiling division
  • 175. Ch Ch Ch Ch Ch Ch Ch DT Built-in Functions abs() delattr() hash() memoryview() set() all() dict() help() min() setattr() any() dir() hex() next() slice() ascii() divmod() id() object() sorted() bin() enumerate() input() oct() staticmethod() bool() eval() int() open() str() breakpoint() exec() isinstance() ord() sum() bytearray() filter() issubclass() pow() super() bytes() float() iter() print() tuple() callable() format() len() property() type() chr() frozenset() list() range() vars() classmethod() getattr() locals() repr() zip() compile() globals() map() reversed() __import__() complex() hasattr() max() round() 175 Back
  • 176. Ch Ch Ch Ch Ch Ch Ch DT Data representation •Series: 1D •DataFrame: 2D Loading data with *read_csv()* •header •names •sep •encoding •converters Getting information •index: Series and DataFrame index •columns: DataFrame column •shape: Series and DataFrame dimensions •info(): DataFrame informations •values: Index and Series values •unique(): Series unique values •nunique(): Series number of unique values Selecting data •head() and tail() •Column accessing •Row accessing •Fancy indexing •Logical masking Indexing and merging data •*set_index()* and *reset_index()* •*append()* and *concat()* Computing over data •sum() •str.len(), str.startswith() and str.contains() •*apply()* and lambda •isin() •div() Organizing data •transpose() or T •*sort_values()* •*groupby()* •*pivot_table()* and *crosstab()* Displaying data •*plot()* Check Link – PyParis 2017 Pandas DataFrame Attributes Indexing, Iteration Function application, GroupBy & window Binary Operator functions Computation/descriptive stats Reindexing/selection/label manipulation Missing Data handling Reshaping, sorting, transposing Combining/comparing/joining/m erging Time Series related Plotting Serialization/IO/Conversion Pandas Series Attributes Conversion Indexing, Iteration Function application, GroupBy & window Binary Operator functions Computation/descriptive stats Reindexing/selection/label manipulation Missing Data handling Reshaping, sorting Combining/comparing/joining/m erging Time Series related Accessors Datetime properties Datetime methods Period properties Timedelta properties Timedelta methods String handling Categorical Accessor Plotting Numpy Arrays Attributes Array Creation Routines Shape Manipulation - shape, resize, transpose Calculation Item Selection and Manipulation Array Conversions Time Series/Date Functionality Group by: split-apply-combine Windowing Index Objects Data manipulations melt(frame[, id_vars, value_vars, var_name, …]) Unpivot a DataFrame from wide to long format, optionally leaving identifiers set. pivot(data[, index, columns, values]) Return reshaped DataFrame organized by given index / column values. pivot_table(data[, values, index, columns, …]) Create a spreadsheet-style pivot table as a DataFrame. crosstab(index, columns[, values, rownames, …]) Compute a simple cross tabulation of two (or more) factors. cut(x, bins[, right, labels, retbins, …]) Bin values into discrete intervals. qcut(x, q[, labels, retbins, precision, …]) Quantile-based discretization function. merge(left, right[, how, on, left_on, …]) Merge DataFrame or named Series objects with a database-style join. merge_ordered(left, right[, on, left_on, …]) Perform merge with optional filling/interpolation. merge_asof(left, right[, on, left_on, …]) Perform an asof merge. concat(objs[, axis, join, ignore_index, …]) Concatenate pandas objects along a particular axis with optional set logic along the other axes. get_dummies(data[, prefix, prefix_sep, …]) Convert categorical variable into dummy/indicator variables. Top-level missing data isna(obj) Detect missing values for an array-like object. isnull(obj) Detect missing values for an array-like object. notna(obj) Detect non-missing values for an array-like object. notnull(obj) Detect non-missing values for an array-like object. Top-level conversions to_numeric(arg[, errors, downcast]) Convert argument to a numeric type. Top-level dealing with datetimelike to_datetime(arg[, errors, dayfirst, …]) Convert argument to datetime. to_timedelta(arg[, unit, errors]) Convert argument to timedelta. date_range([start, end, periods, freq, tz, …]) Return a fixed frequency DatetimeIndex. bdate_range([start, end, periods, freq, tz, …]) Return a fixed frequency DatetimeIndex, with business day as the default frequency. period_range([start, end, periods, freq, name]) Return a fixed frequency PeriodIndex. timedelta_range([start, end, periods, freq, …]) Return a fixed frequency TimedeltaIndex, with day as the default frequency. infer_freq(index[, warn]) Infer the most likely frequency given the input index. Pandas General Functions 176
  • 178. Ch Ch Ch Ch Ch Ch Ch DT Seeding for Initializing the Pseudo Random Number Generator What is normally called a random number sequence in reality is a "pseudo- random" number sequence because the values are computed using a deterministic algorithm and probability plays no real role. The "seed" is a starting point for the sequence and the guarantee is that if you start from the same seed you will get the same sequence of numbers. This is very useful for example for debugging (when you are looking for an error in a program you need to be able to reproduce the problem and study it, a non-deterministic program would be much harder to debug because every run would be different). Link § Python Stdlib Random Module § Numpy.random module § Legacy RandomState and § Generators Library Module Class Generator Type Python Standard Library random random.Random(seed) Mersenne Twister Generator MT19937 Numpy random numpy.random.RandomStat e(seed) Mersenne Twister Generator MT19937 Numpy random numpy.random.Generator(bi t_generator) Permuted Congruential Generator PCG64 Link #Setting the seed of the random number generator >>>np.random.seed(123456) #Sequence of numbers generated starting from the seed position >>>np.random.randint(10,50,5) array([11, 37, 42, 33, 46]) #Note that internally the state of the generator has changed and a new seed is set for the next function call >>>np.random.randint(10,50,5) #Output shall be based on deterministically created new seed internally array([18, 20, 20, 22, 21]) #Resetting the seed will produce the same sequence of random numbers >>>np.random.seed(123456) >>>np.random.randint(10,50,5) array([11, 37, 42, 33, 46]) Pseudo Random Number Generation in Python Note : PRGN is a vast topic with wide applications in simulations (MonteCarlo), game development, cryptography etc. We shall be covering a small part of it from the point of view of generating random numbers as and when required for simple use cases. Random number Seed Initialize State Next Update State Random number Next Update Pseudo-Random Number Generator (PRNG) generates a sequence of random numbers based on a seed. Using the same seed makes the PRNG produce the same sequence of random numbers. *Source : Real-World Cryptography Version 12 by David Wong Statistical Quality Prediction Difficulty Reproducible Results Multiple Streams Period Useful Features Time Perfomance Space Usage Code Size & Complexity k-Dimensional Equidistribution Evaluation Metrics PRNG 178
  • 179. Ch Ch Ch Ch Ch Ch Ch DT Random Module standard Library in Python Bookkeeping functions seed Initialize the random number generator getstate Return an object capturing the current internal state of the generator. setstate setstate() restores the internal state of the generator to what it was at the time getstate() was called. Functions for integers randrange Return a randomly selected element from range(start, stop, step). randint Return a random integer N such that a <= N <= b Functions for sequences choice Return a random element from the non-empty sequence seq. choices Return a k sized list of elements chosen from the population with replacement. shuffle Shuffle the sequence x in place. sample Return a k length list of unique elements chosen from the population sequence or set. Used for random sampling without replacement Real-valued distributions random Return the next random floating point number in the range [0.0, 1.0). uniform Return a random floating point number N such that a <= N <= b for a <= b and b <= N <= a for b < a. betavariate Beta distribution. expovariate Exponential distribution. gammavariate Gamma distribution. gauss Gaussian distribution. lognormvariate Log normal distribution. normalvariate Normal distribution. mu is the mean, and sigma is the standard deviation. paretovariate Pareto distribution. alpha is the shape parameter. triangular Return a random floating point number N such that low <= N <= high and with the specified mode between those bounds vonmisesvariate von Mises distribution weibullvariate Weibull distribution. alpha is the scale parameter and beta is the shape parameter. Alternative Generator class random.Random([seed])Class that implements the default pseudo- random number generator used by the random module. Note : The functions supplied by Random module are actually bound methods of a hidden instance of the random.Random class. You can instantiate your own instances of Random to get generators that don’t share state. Functions supplied by the Random Module Python uses the Mersenne Twister as the core generator. PRGN by Mersenne Twister Generator random.seed(a=None, version=2) Initialize the random number generator. If a is omitted or None, the current system time is used. [..] >>>import random >>>#random.seed(123456) >>> rints = random.randint(low=0, high=10, size=(4,4)) 179 >>> rchoice = random.choice(‘abcdefgh’) Also, check discussion thread on generating random integers.
  • 180. Ch Ch Ch Ch Ch Ch Ch DT PRNG in Numpy.Random Module PRNG by Mersenne Twister Generator The BitGenerator has a limited set of responsibilities. It manages state and provides functions to produce random doubles and random unsigned 32- and 64-bit values. The random generator takes the bit generator-provided stream and transforms them into more useful distributions, e.g., simulated normal random values. This structure allows alternative bit generators to be used with little code duplication. The Generator is the user-facing object that is nearly identical to the legacy RandomState. It accepts a bit generator instance as an argument. The default is currently PCG64 but this may change in future versions. As a convenience NumPy provides the default_rng function to hide these details: # Construct a new Generator import numpy as np >>> rng = np.random.default_rng(123456) >>> rints = rng.integers(low=0, high=10, size=(4,4)) >>> rints array([[0, 6, 8, 3], [3, 0, 0, 9], [2, 9, 3, 4], [5, 2, 4, 6]]) >>> rng = np.random.default_rng(123456) >>> rng.random(4) array([0.63651375, 0.38481166, 0.04744542, 0.95525274]) >>> rng = np.random.default_rng(123456) >>> rng.standard_normal(10) array([ 0.1928212 , -0.06550702, 0.43550665, 0.88235875, 0.37132785, 1.15998882, 0.37835254, - 0.11718594, 2.20800921, 1.95324484]) using combinations of a BitGenerator to create sequences and a Generator to use those sequences to sample from different statistical distributions PRNG by PCG 180
  • 181. Ch Ch Ch Ch Ch Ch Ch DT Seeding and State get_state() Return a tuple representing the internal state of the generator. set_state(state) Set the internal state of the generator from a tuple. seed(self[, seed]) Reseed a legacy MT19937 BitGenerator Simple random data rand(d0, d1, …, dn) Random values in a given shape. randn(d0, d1, …, dn) Return a sample (or samples) from the “standard normal” distribution. randint(low[, high, size, dtype]) Return random integers from low (inclusive) to high (exclusive). random_integers(low[, high, size]) Random integers of type np.int_ between low and high, inclusive. random_sample([size]) Return random floats in the half-open interval [0.0, 1.0). choice(a[, size, replace, p]) Generates a random sample from a given 1-D array bytes(length) Return random bytes. Permutations shuffle(x) Modify a sequence in-place by shuffling its contents. permutation(x) Randomly permute a sequence, or return a permuted range. Distributions beta(a, b[, size]) Draw samples from a Beta distribution. binomial(n, p[, size]) Draw samples from a binomial distribution. chisquare(df[, size]) Draw samples from a chi-square distribution. dirichlet(alpha[, size]) Draw samples from the Dirichlet distribution. exponential([scale, size]) Draw samples from an exponential distribution. f(dfnum, dfden[, size]) Draw samples from an F distribution. gamma(shape[, scale, size]) Draw samples from a Gamma distribution. geometric(p[, size]) Draw samples from the geometric distribution. gumbel([loc, scale, size]) Draw samples from a Gumbel distribution. hypergeometric(ngood, nbad, nsample[, s ize]) Draw samples from a Hypergeometric distribution. laplace([loc, scale, size]) Draw samples from the Laplace or double exponential distribution with specified location (or mean) and scale (decay). logistic([loc, scale, size]) Draw samples from a logistic distribution. lognormal([mean, sigma, size]) Draw samples from a log-normal distribution. logseries(p[, size]) Draw samples from a logarithmic series distribution. multinomial(n, pvals[, size]) Draw samples from a multinomial distribution. multivariate_normal(mean, cov[, size, …]) Draw random samples from a multivariate normal distribution. negative_binomial(n, p[, size]) Draw samples from a negative binomial distribution. noncentral_chisquare(df, nonc[, size]) Draw samples from a noncentral chi-square distribution. noncentral_f(dfnum, dfden, nonc[, size]) Draw samples from the noncentral F distribution. normal([loc, scale, size]) Draw random samples from a normal (Gaussian) distribution. pareto(a[, size]) Draw samples from a Pareto II or Lomax distribution with specified shape. poisson([lam, size]) Draw samples from a Poisson distribution. power(a[, size]) Draws samples in [0, 1] from a power distribution with positive exponent a - 1. rayleigh([scale, size]) Draw samples from a Rayleigh distribution. standard_cauchy([size]) Draw samples from a standard Cauchy distribution with mode = 0. standard_exponential([size]) Draw samples from the standard exponential distribution. standard_gamma(shape[, size]) Draw samples from a standard Gamma distribution. standard_normal([size]) Draw samples from a standard Normal distribution (mean=0, stdev=1). standard_t(df[, size]) Draw samples from a standard Student’s t distribution with df degrees of freedom. triangular(left, mode, right[, size]) Draw samples from the triangular distribution over the interval [left, right]. uniform([low, high, size]) Draw samples from a uniform distribution. vonmises(mu, kappa[, size]) Draw samples from a von Mises distribution. wald(mean, scale[, size]) Draw samples from a Wald, or inverse Gaussian, distribution. weibull(a[, size]) Draw samples from a Weibull distribution. zipf(a[, size]) Draw samples from a Zipf distribution. Random sampling (numpy.random) Using RandomState Class (Legacy Random Generation) • RandomState is a class in NumPy. We use an instance of this class to manage random number generation. • Random numbers are generated by methods in the class (e.g. the rand or randn methods). • Each instance of RandomState comes with its own specific random number stream. • The random number stream is initialized (“seeded”) when you create a RandomState instance. • You can reset the “seed” using the seed() method. • We can use the RandomState objects to have different random streams or to reset a stream. 181
  • 182. Ch Ch Ch Ch Ch Ch Ch DT Accessing the Bit Generator - Attribute bit_generator Gets the bit generator instance used by the generator Simple random data - Methods integers Return random integers from low (inclusive) to high(exclusive), or if endpoint=True, low (inclusive) to high(inclusive). random Return random floats in the half-open interval [0.0, 1.0). choice Generates a random sample from a given array bytes Return random bytes. Permutations - Methods shuffle Modify an array or sequence in-place by shuffling its contents. permutation Randomly permute a sequence, or return a permuted range. permuted Randomly permute x along axis axis. Distributions - Methods beta(a, b[, size]) Draw samples from a Beta distribution. binomial(n, p[, size]) Draw samples from a binomial distribution. chisquare(df[, size]) Draw samples from a chi-square distribution. dirichlet(alpha[, size]) Draw samples from the Dirichlet distribution. exponential([scale, size]) Draw samples from an exponential distribution. f(dfnum, dfden[, size]) Draw samples from an F distribution. gamma(shape[, scale, size]) Draw samples from a Gamma distribution. geometric(p[, size]) Draw samples from the geometric distribution. gumbel([loc, scale, size]) Draw samples from a Gumbel distribution. hypergeometric(ngood, nbad, nsample[, size]) Draw samples from a Hypergeometric distribution. laplace([loc, scale, size]) Draw samples from the Laplace or double exponential distribution with specified location (or mean) and scale (decay). logistic([loc, scale, size]) Draw samples from a logistic distribution. lognormal([mean, sigma, size]) Draw samples from a log-normal distribution. logseries(p[, size]) Draw samples from a logarithmic series distribution. multinomial(n, pvals[, size]) Draw samples from a multinomial distribution. multivariate_hypergeometric(colors, nsample) Generate variates from a multivariate hypergeometric distribution. multivariate_normal(mean, cov[, size, …]) Draw random samples from a multivariate normal distribution. negative_binomial(n, p[, size]) Draw samples from a negative binomial distribution. noncentral_chisquare(df, nonc[, size]) Draw samples from a noncentral chi-square distribution. noncentral_f(dfnum, dfden, nonc[, size]) Draw samples from the noncentral F distribution. normal([loc, scale, size]) Draw random samples from a normal (Gaussian) distribution. pareto(a[, size]) Draw samples from a Pareto II or Lomax distribution with specified shape. poisson([lam, size]) Draw samples from a Poisson distribution. power(a[, size]) Draws samples in [0, 1] from a power distribution with positive exponent a - 1. rayleigh([scale, size]) Draw samples from a Rayleigh distribution. standard_cauchy([size]) Draw samples from a standard Cauchy distribution with mode = 0. standard_exponential([size, dtype, method, out]) Draw samples from the standard exponential distribution. standard_gamma(shape[, size, dtype, out]) Draw samples from a standard Gamma distribution. standard_normal([size, dtype, out]) Draw samples from a standard Normal distribution (mean=0, stdev=1). standard_t(df[, size]) Draw samples from a standard Student’s t distribution with df degrees of freedom. triangular(left, mode, right[, size]) Draw samples from the triangular distribution over the interval [left, right]. uniform([low, high, size]) Draw samples from a uniform distribution. vonmises(mu, kappa[, size]) Draw samples from a von Mises distribution. wald(mean, scale[, size]) Draw samples from a Wald, or inverse Gaussian, distribution. weibull(a[, size]) Draw samples from a Weibull distribution. Use numpy.random.default_rng for PRNG instead of the legacy RandomState numpy.random.default_rng() : Construct a new Generator with the default BitGenerator (PCG64). import numpy as np #Using numpy.random.default_rng to instantiate a Generator with numpy’s default BitGenerator # We can specify a seed for reproducible results >>> rng = np.random.default_rng(12345) >>> rints = rng.integers(low=0, high=10, size=3) >>> rints array([6, 2, 7]) >>> type(rints[0]) <class 'numpy.int64'> Instead of reseeding, instantiate a new Generator object using np.random.default_rng Why So ? Avoid the use of numpy.random.seed(seed) for fixing reproducibility as this operates at the global state level. For multiple threads and projects with imports and scripts, the results will get obscured in case of multiple declarations of the np.random.seed(seed) with different seed arguments. 182
  • 183. Ch Ch Ch Ch Ch Ch Ch DT import numpy as np import matplotlib.pyplot plt.rcdefaults() fig, axes = plt.subplots(1, 3, figsize=(12, 4)) #Instantiating a generator rng = np.random.default_rng(12345) axes[0].hist(rng.random(10000)) axes[0].set_title("random") axes[1].hist(rng.standard_normal(10000)) axes[1].set_title("standard_normal") axes[2].hist(rng.integers(low=1, high=10, size=10000), bins=9, align='left') axes[2].set_title("integers(low=1, high=10)") fig.savefig('GeneratorObject.png', dpi = 300, transparent = True) plt.show() np.random.default_rng to generate random numbers Avoid np.random functions and instead use default_rng 183
  • 185. Ch Ch Ch Ch Ch Ch Ch DT Classes/Objects to handle date and time datetime time timedelta date datetime64 timedelta64 timestamp PeriodIndex period datetimeindex Pandas Python Numpy Python, Numpy, Pandas, Matplotlib date Matplotlib A Specific Date and Time With TimeZone support timedelta timedeltaindex tzinfo timezone Absolute time duration dateoffset The datetime module's datetime object has microsecond precision (one-millionth of a second). NumPy's datetime64 object allows you to set its precision from hours all the way to attoseconds (10 ^ - 18). Note : timedelta only holds days, seconds, microseconds internally 185
  • 186. Ch Ch Ch Ch Ch Ch Ch DT Conversion Matrix between datetime, Timestamp and datetime64 https://stackoverflow.com/questions/466345/converting-string-into-datetime/470303#470303 Across Pandas, numpy, Python, matplotlib, strings Conversion Matrix Python Numpy Pandas matplotlib To string datetime datetime64 timestamp matplotlib.date Python string strobj: string object >>>dt.strptime(strobj) np.datetime64('2005-02-25') #strings in ISO 8601 date or datetime format. pd.Timestamp('2017-01- 01T12’) #datetime like string datestr2num(strobj) Python datetime import datetime as dt dtobj : datetime object dtobj.strftime('%b %d, %Y’) dtobj.strftime('The date is %b %d, %Y') np.datetime64(dtobj) pd.Timestamp(dtobj) # or pd.to_datetime(dtobj) date2num(dtobj) Numpy datetime64 import numpy as np dt64 : datetime64 object np.datetime_as_string(d, unit='h’) #Check datetime support functions >>> dt64 = np.datetime64('2017-10-24 05:34:20.123456') >>> unix_epoch = np.datetime64(0, 's') >>> one_second = np.timedelta64(1, 's') >>> seconds_since_epoch = (dt64 - unix_epoch) / one_second >>> seconds_since_epoch 1508823260.123456 >>> datetime.datetime.utcfromtimestamp(seconds_since_epoch) datetime.datetime(2017, 10, 24, 5, 34, 20, 123456) pd.Timestamp(dt64) Pandas timestamp import pandas as pd ts : timestamp object ts.dt.strftime('%b %d %Y’) #dt is datetime accessor. Link ts.to_pydatetime() ts.to_datetime64() # or ts.to_numpy() # or ts.asm8 date2num(ts.to_pydatetime()) matplotlib matplotlib.date* num2date(mdate) From Go to Matplotlib.dates *Currently Matplotlib time is only converted back to datetime objects, which have microsecond resolution, and years that only span 0000 to 9999. Check Link. https://www.seehuhn.de/pages/pdate.html#unix 186
  • 187. Ch Ch Ch Ch Ch Ch Ch DT Across Pandas, numpy, Python, matplotlib, strings Conversion Matrix Platform Independent Conversion Matrix Platform Independent To Unix epoch or Unix time or POSIX time or Unix timestamp From Unix epoch or Unix time or POSIX time or Unix timestamp Python string strobj: string object >>>datetime.strptime(string). timestamp() >>> datetime.datetime(2021,8,1,0,0).timestamp() 1627756200.0 string >>> import datetime >>> timestamp = datetime.datetime.fromtimestamp(1600000000) >>> print(timestamp.strftime('%Y-%m-%d %H:%M:%S’)) 2020-09-13 17:56:40 Python datetime import datetime as dt dtobj : datetime object datetime.timestamp() Check link datetime >>> dt.datetime.fromtimestamp(1172969203.1) datetime.datetime(2007, 3, 4, 6, 16, 43, 100000) Numpy datetime64 import numpy as np dt64 : datetime64 object >>> dt64 = np.datetime64('2017-10-24 05:34:20.123456') >>> unix_epoch = np.datetime64(0, 's') >>> one_second = np.timedelta64(1, 's') >>> seconds_since_epoch = (dt64 - unix_epoch) / one_second >>> seconds_since_epoch 1508823260.123456 >>> datetime.datetime.utcfromtimestamp(seconds_since_epoch) datetime.datetime(2017, 10, 24, 5, 34, 20, 123456) datetime64 >>> dt64 = np.datetime64('2017-10-24 05:34:20.123456') >>> unix_epoch = np.datetime64(0, 's') >>> one_second = np.timedelta64(1, 's') >>> seconds_since_epoch = (dt64 - unix_epoch) / one_second >>> seconds_since_epoch 1508823260.123456 >>> datetime.datetime.utcfromtimestamp(seconds_since_epoch) datetime.datetime(2017, 10, 24, 5, 34, 20, 123456) Pandas timestamp import pandas as pd ts : timestamp object In [64]: stamps = pd.date_range("2012-10-08 18:15:05", periods=4, freq="D") In [66]: (stamps - pd.Timestamp("1970-01-01")) // pd.Timedelta("1s") Int64Index([1349720105, 1349806505, 1349892905, 1349979305], dtype='int64') timestamp >>> pd.to_datetime([1627756200.0, 1600000000], unit = 's’) DatetimeIndex(['2021-07-31 18:30:00', '2020-09-13 12:26:40'], dtype='datetime64[ns]', freq=None) Link matplotlib matplotlib.date matplotlib.dates.num2epoch(d) [source] matplotlib.date matplotlib.dates.epoch2num(unixtime) [source] From Go to Matplotlib.dates To https://stackoverflow.com/questions/466345/converting-string-into-datetime/470303#470303 https://www.seehuhn.de/pages/pdate.html#unix Conversion Matrix between Unix time vs datetime, Timestamp and datetime64 187
  • 188. Ch Ch Ch Ch Ch Ch Ch DT String to datetime object = strptime datetime object to other formats = strftime Jun 1 2021 1:33PM is equals to %b %d %Y %I:%M%p • %b :Month as locale’s abbreviated name(Jun) • %d :Day of the month as a zero-padded decimal number(1) • %Y :Year with century as a decimal number(2015) • %I :Hour (12-hour clock) as a zero-padded decimal number(01) • %M :Minute as a zero-padded decimal number(33) • %p :Locale’s equivalent of either AM or PM(PM) strptime means string parser, this will convert a string format to datetime. strftime means string formatter, this will format a datetime object to string format. dateutil capable of parsing most human intelligible date representations. dateutil.parser.parse to format a date/time from known formats. § Functions : parser.parse(parserinfo=None, **kwargs) classmethod parser.isoparse(dt_str) # full ISO-8601 format parser § Classes : class dateutil.parser.parserinfo(dayfirst=False, yearfirst=False) Handling Datetime objects and strings Using Pandas for String conversion to datetime object : link Check answer by alexander, Also check dt accessor methods >>>import pandas as pd >>>dates = ['2015-12-25', '2015-12-26’] # 1) Use a list comprehension. >>> [d.date() for d in pd.to_datetime(dates)] [datetime.date(2015, 12, 25), datetime.date(2015, 12, 26)] # 2) Convert the dates to a DatetimeIndex and extract the python dates. >>> pd.DatetimeIndex(dates).date.tolist() [datetime.date(2015, 12, 25), datetime.date(2015, 12, 26)] https://stackoverflow.com/questions/466345/converting- string-into-datetime/470303#470303 Code Meaning %a Weekday as Sun, Mon %A Weekday as full name as Sunday, Monday %w Weekday as decimal no as 0,1,2... %d Day of month as 01,02 %b Months as Jan, Feb %B Months as January, February %m Months as 01,02 %y Year without century as 11,12,13 %Y Year with century 2011,2012 %H 24 Hours clock from 00 to 23 %I 12 Hours clock from 01 to 12 %p AM, PM %M Minutes from 00 to 59 %S Seconds from 00 to 59 %f Microseconds 6 decimal numbers https://docs.python.org/3/library/datetime.html#strf time-and-strptime-behavior strftime() and strptime() Format Codes Strings to datetime ISO 8601 Date Format : YYYY-MM-DD • YYYY is the year [all the digits, i.e. 2012] • MM is the month [01 (January) to 12(December)] • DD is the day [01 to 31] https://www.iso.org/iso-8601-date-and-time-format.html https://www.cl.cam.ac.uk/~mgk25/iso-time.html RFC3339 Format https://datatracker.ietf.org/doc/html/rfc3339 strptime strftime datetime.datetime module class methods SO Thread Date Format standards Check Series.dt.strftime(*args, **kwargs) 188
  • 189. Ch Ch Ch Ch Ch Ch Ch DT Concept Scalar Class Array Class pandas Data Type Primary Creation Method Date times Timestamp DatetimeIndex datetime64[ns] or datetime64[ns, tz] to_datetime or date_range Time deltas Timedelta TimedeltaIndex timedelta64[ns] to_timedelta or timedelta_range Time spans Period PeriodIndex period[freq] Period or period_range Date offsets DateOffset None None DateOffset pandas captures 4 general time related concepts: Date times : A specific date and time with timezone support. Similar to datetime.datetime from the standard library. Time deltas: An absolute time duration. Similar to datetime.timedelta from the standard library. Time spans: A span of time defined by a point in time and its associated frequency. Date offsets: A relative time duration that respects calendar arithmetic. Similar to dateutil.relativedelta.relativedelta from dateutil package. Under the hood, pandas represents timestamps using instances of Timestamp and sequences of timestamps using instances of DatetimeIndex. For regular time spans, pandas uses Period objects for scalar values and PeriodIndex for sequences of spans What is epoch time? The Unix epoch (or Unix time or POSIX time or Unix timestamp) is the number of seconds that have elapsed since January 1, 1970 (midnight UTC/GMT), not counting leap seconds (in ISO 8601: 1970-01-01T00:00:00Z). Literally speaking the epoch is Unix time 0 (midnight 1/1/1970), but 'epoch' is often used as a synonym for Unix time. SO Thread In [21]: pd.Series(pd.period_range("1/1/2011", freq="M", periods=3)) Out[21]: 0 2011-01 1 2011-02 2 2011-03 dtype: period[M] import datetime In [2]: dti = pd.to_datetime( ["1/1/2018", np.datetime64("2018-01-01"), datetime.datetime(2018, 1, 1)] In [3]: dti Out[3]: DatetimeIndex(['2018-01-01', '2018-01-01', '2018-01-01'], dtype='datetime64[ns]', freq=None) Pandas Official Link Comprehensive Tutorial 189
  • 190. Ch Ch Ch Ch Ch Ch Ch DT Concept Scalar Class Array Class pandas Data Type Primary Creation Method Date times Timestamp DatetimeIndex datetime64[ns] or datetime64[ns, tz] to_datetime or date_range Time deltas Timedelta TimedeltaIndex timedelta64[ns] to_timedelta or timedelta_range Time spans Period PeriodIndex period[freq] Period or period_range Date offsets DateOffset None None DateOffset >>> df = pd.DataFrame({'year': [2015, 2016], 'month': [2, 3], 'day': [4, 5]}) >>> pd.to_datetime(df) 0 2015-02-04 1 2016-03-05 dtype: datetime64[ns] >>> pd.to_datetime('13000101', format='%Y%m%d', errors='ignore’) datetime.datetime(1300, 1, 1, 0, 0) >>> pd.to_datetime('13000101', format='%Y%m%d', errors='coerce’) NaT >>> pd.date_range(start='1/1/2018', end='1/08/2018’) DatetimeIndex(['2018-01-01', '2018-01-02', '2018-01-03’, '2018-01-04', '2018-01-05’, '2018-01-06’, '2018-01-07', '2018-01-08’],dtype='datetime64[ns]’, freq='D') >>> pd.date_range(start='1/1/2018', periods=5, freq='M’) DatetimeIndex(['2018-01-31', '2018-02-28', '2018-03-31’, '2018-04-30', '2018-05-31’], dtype='datetime64[ns]’, freq='M') >>> pd.date_range(start='1/1/2018', periods=5, freq=pd.offsets.MonthEnd(3)) DatetimeIndex(['2018-01-31', '2018-04-30', '2018-07-31’, '2018-10-31', '2019-01-31’], dtype='datetime64[ns]’, freq='3M') >>> pd.period_range(start='2017-01-01', end='2018-01-01', freq='M’) PeriodIndex(['2017-01', '2017-02', '2017-03', '2017-04', '2017-05’, '2017-06’, '2017-07', '2017-08', '2017-09', '2017-10’, '2017-11', '2017-12’, '2018-01'], dtype='period[M]') Frequency codes/ Offset Aliases Alias Description B business day frequency C custom business day frequency D calendar day frequency W weekly frequency M month end frequency SM semi-month end frequency (15th and end of month) BM business month end frequency CBM custom business month end frequency MS month start frequency SMS semi-month start frequency (1st and 15th) BMS business month start frequency CBMS custom business month start frequency Q quarter end frequency BQ business quarter end frequency QS quarter start frequency BQS business quarter start frequency A, Y year end frequency BA, BY business year end frequency AS, YS year start frequency BAS, BYS business year start frequency BH business hour frequency H hourly frequency T, min minutely frequency S secondly frequency L, ms milliseconds U, us microseconds N nanoseconds >>> pd.timedelta_range(start='1 day', periods=4) TimedeltaIndex(['1 days', '2 days', '3 days', '4 days'], dtype='timedelta64[ns]’, freq='D’) >>> pd.timedelta_range(start='1 day', end='2 days', freq='6H’) TimedeltaIndex(['1 days 00:00:00', '1 days 06:00:00', '1 days 12:00:00’, '1 days 18:00:00', '2 days 00:00:00'], dtype='timedelta64[ns]’, freq='6H') >>> pd.timedelta_range(start='1 day', end='5 days', periods=4) TimedeltaIndex(['1 days 00:00:00', '2 days 08:00:00', '3 days 16:00:00’, '5 days 00:00:00’], dtype='timedelta64[ns]', freq=None) >>> pd.to_timedelta(np.arange(5), unit='s’) TimedeltaIndex(['0 days 00:00:00', '0 days 00:00:01', '0 days 00:00:02’, '0 days 00:00:03', '0 days 00:00:04'], dtype='timedelta64[ns]', freq=None) >>> pd.to_timedelta(np.arange(5), unit='d’) TimedeltaIndex(['0 days', '1 days', '2 days', '3 days', '4 days'], dtype='timedelta64[ns]', freq=None) # This particular day contains a day light savings time transition >>> ts = pd.Timestamp("2016-10-30 00:00:00", tz="Europe/Helsinki") # Respects absolute time >>> ts + pd.Timedelta(days=1) Timestamp('2016-10-30 23:00:00+0200', tz='Europe/Helsinki’) # Respects calendar time >>> ts + pd.DateOffset(days=1) Timestamp('2016-10-31 00:00:00+0200', tz='Europe/Helsinki') Pandas Date and time handling classes : Creation Methods Also, Check Slide for division of timedeltas to find number of years between two dates. 190
  • 191. Ch Ch Ch Ch Ch Ch Ch DT Attributes year The year of the datetime. month The month as January=1, December=12. day The day of the datetime. hour The hours of the datetime. minute The minutes of the datetime. second The seconds of the datetime. microsecond The microseconds of the datetime. nanosecond The nanoseconds of the datetime. date Returns numpy array of python datetime.date objects (namely, the date part of Timestamps without timezone information). time Returns numpy array of datetime.time. timetz Returns numpy array of datetime.time also containing timezone information. dayofyear The ordinal day of the year. day_of_year The ordinal day of the year. weekofyear (DEPRECATED) The week ordinal of the year. week (DEPRECATED) The week ordinal of the year. dayofweek The day of the week with Monday=0, Sunday=6. day_of_week The day of the week with Monday=0, Sunday=6. weekday The day of the week with Monday=0, Sunday=6. quarter The quarter of the date. tz Return timezone, if any. freq Return the frequency object if it is set, otherwise None. freqstr Return the frequency object as a string if its set, otherwise None. is_month_start Indicates whether the date is the first day of the month. is_month_end Indicates whether the date is the last day of the month. is_quarter_startIndicator for whether the date is the first day of a quarter. is_quarter_end Indicator for whether the date is the last day of a quarter. is_year_start Indicate whether the date is the first day of a year. is_year_end Indicate whether the date is the last day of the year. is_leap_year Boolean indicator if the date belongs to a leap year. inferred_freq Tries to return a string representing a frequency guess, generated by infer_freq. REMEMBER • Valid date strings can be converted to datetime objects using to_datetimefunction or as part of read functions. • Datetime objects in pandas support calculations, logical operations and convenient date-related properties using the dt accessor. • A DatetimeIndex contains these date-related properties and supports convenient slicing. • Resample is a powerful method to change the frequency of a time series. The DatetimeIndex class contains many time series related optimizations: • A large range of dates for various offsets are pre-computed and cached under the hood in order to make generating subsequent date ranges very fast (just have to grab a slice). • Fast shifting using the shift method on pandas objects. • Unioning of overlapping DatetimeIndex objects with the same frequency is very fast (important for fast data alignment). • Quick access to date fields via properties such as year, month, etc. • Regularization functions like snap and very fast as of logic. Datetime Index 191
  • 192. Ch Ch Ch Ch Ch Ch Ch DT Methods normalize(*args, **kwargs) Convert times to midnight. strftime(*args, **kwargs) Convert to Index using specified date_format. snap([freq]) Snap time stamps to nearest occurring frequency. tz_convert(tz) Convert tz-aware Datetime Array/Index from one time zone to another. tz_localize Localize tz-naive Datetime Array/Index to tz-aware Datetime Array/Index. round(*args, **kwargs) Perform round operation on the data to the specified freq. floor(*args, **kwargs) Perform floor operation on the data to the specified freq. ceil(*args, **kwargs) Perform ceil operation on the data to the specified freq. to_period(*args, **kwargs) Cast to PeriodArray/Index at a particular frequency. to_perioddelta(freq) Calculate TimedeltaArray of difference between index values and index converted to PeriodArray at specified freq. to_pydatetime Return Datetime Array/Index as object ndarray of datetime.datetime objects. to_series Create a Series with both index and values equal to the index keys useful with map for returning an indexer based on an index. to_frame([index, name]) Create a DataFrame with a column containing the Index. month_name Return the month names of the DateTimeIndex with specified locale. day_name(*args, **kwargs) Return the day names of the DateTimeIndex with specified locale. mean(*args, **kwargs) Return the mean value of the Array. to_perioddelta(freq) Calculate TimedeltaArray of difference between index values and index converted to PeriodArray at specified freq. to_pydatetime Return Datetime Array/Index as object ndarray of datetime.datetime objects. to_series Create a Series with both index and values equal to the index keys useful with map for returning an indexer based on an index. Check if two datetime indexes are equal >>> import pandas as pd >>> idx1 = pd.date_range('2020-01-01','2020-12-31',freq='D’) >>> idx2 = pd.date_range('2020-01-01','2020-11-01',freq='D’) >>> idx3 = pd.date_range('2020-01-01','2020-12-31',freq='D’) >>> help(idx1.equals) Help on method equals in module pandas.core.indexes.datetimelike: equals(other: object) -> bool method of pandas.core.indexes.datetimes.DatetimeIndex instance Determines if two Index objects contain the same elements. >>> print(idx1.equals(idx2)) False >>> print(idx1.equals(idx3)) True Datetime Index 192
  • 193. Ch Ch Ch Ch Ch Ch Ch DT APPENDIX Package Name pandas numpy scipy Version 1.2.1 1.19.5 1.6.0 Summary Powerful data structures for data analysis, time series, and statistics NumPy is the fundamental package for array computing with Python. SciPy: Scientific Library for Python All Code examples have been checked with Python Version 3.8.3 and IDE JupyterLab The package details are as below : * Use pip show <package_name> to get details of a package 193
  • 194. Ch Ch Ch Ch Ch Ch Ch DT #https://stackoverflow.com/questions/24547047/how-to- make-matplotlib-graphs-look-professionally-done-like-this # Check out the answer by DrV # Necessary imports import numpy as np import matplotlib.pyplot as plt plt.rcdefaults() # create some fictive access data by hour xdata = np.arange(25) ydata = np.random.randint(10, 20, 25) ydata[24] = ydata[0] # let us make a simple graph fig = plt.figure(figsize=[7,5]) ax = plt.subplot(111) l = ax.fill_between(xdata, ydata) # set the basic properties ax.set_xlabel('Time of posting (US EST)') ax.set_ylabel('Percentage of Frontpaged Submissions') ax.set_title('Likelihood of Reaching the Frontpage') # set the limits ax.set_xlim(0, 24) ax.set_ylim(6, 24) # set the grid on ax.grid('on’) # change the fill into a blueish color with opacity .3 l.set_facecolors([[.5,.5,.8,.3]]) # change the edge color (bluish and transparentish) and thickness l.set_edgecolors([[0.3, 0.6, .9, 1]]) l.set_linewidths([2]) # add more ticks ax.set_xticks(np.arange(25)) # remove tick marks ax.xaxis.set_tick_params(size=0) ax.yaxis.set_tick_params(size=0) # change the color of the top and right spines to opaque gray ax.spines['right'].set_color((.8,.8,.8)) ax.spines['top'].set_color((.8,.8,.8)) # tweak the axis labels xlab = ax.xaxis.get_label() ylab = ax.yaxis.get_label() xlab.set_style('italic') xlab.set_size(10) ylab.set_style('italic') ylab.set_size(10) # tweak the title ttl = ax.title ttl.set_weight('bold') fig.savefig('Filline plot.png',dpi=300, format='png', bbox_inches='tight') Back 194
  • 195. Ch Ch Ch Ch Ch Ch Ch DT #https://stackoverflow.com/questions/66011487/highlighti ng-weekends-in-small-multiples # Check the answer by Patrick Fitzgerald CONTEXT • Raw data is time Series with Daily frequency • Figure with multiple subplots with each one for a particular month (iteration used) • Weekends need to be highlighted Code 195
  • 196. Ch Ch Ch Ch Ch Ch Ch DT #https://stackoverflow.com/questions/66011487/highlighti ng-weekends-in-small-multiples # Check the answer by Patrick Fitzgerald CONTEXT • Raw data is time Series with Daily frequency • Figure with multiple subplots with each one for a particular month (iteration used) • Weekends need to be highlighted #Create a date time index dti = pd.date_range('2020-01-01 00:00', '2020-12-31 23:59', freq='6H’) consumption = rng.integers(1000, 2000, size=dti.size) # Create the data frame df = pd.DataFrame(dict(consumption=consumption), index=dti) # Draw and format subplots by looping through months and flattened array of axes fig, axs = plt.subplots(4, 3, figsize=(13, 9), sharey=True) for month, ax in zip(df.index.month.unique(), axs.flat): # Select monthly data and plot it df_month = df[df.index.month == month] ax.plot(df_month.index, df_month['consumption’]) # set limit similar to plot shown in question ax.set_ylim(0, 2500) # Draw vertical spans for weekends: computing the time delta and adding it to the date solves the problem of exceeding the df_month.index timedelta = pd.to_timedelta(df_month.index.freq) weekends =df_month.index[df_month.index.weekday >=5].to_series() for date in weekends: ax.axvspan(date, date+timedelta, facecolor='k’, edgecolor=None, alpha=.1) # Format tick labels ax.set_xticks(ax.get_xticks()) tk_labels = [pd.to_datetime(tk, unit='D').strftime('%d’) for tk in ax.get_xticks()] ax.set_xticklabels(tk_labels, rotation=0, ha='center’) # Add x labels for months ax.set_xlabel(df_month.index[0].month_name().upper(), labelpad=5) ax.xaxis.set_label_position('top’) # Add title and edit spaces between subplots year = df.index[0].year freq = df_month.index.freqstr title = f'{year} consumption displayed for each month with a {freq} frequency’ fig.suptitle(title.upper(), y=0.95, fontsize=12) fig.subplots_adjust(wspace=0.1, hspace=0.5) fig.text(0.5, 0.99, 'Weekends are highlighted by using the DatetimeIndex’, ha='center', fontsize=14, weight='semibold’) plt.savefig('Daily consumption with weekends highlighted.png’, bbox_inches = "tight",pad_inches = 0.5, dpi=300) plt.show() # Also, Check below excellent links on using spans and fill areas • https://stackoverflow.com/questions/8270981/in-a- matplotlib-plot-can-i-highlight-specific-x-value- ranges?noredirect=1&lq=1 • https://stackoverflow.com/questions/48973471/how-to- highlight-weekends-for-time-series-line-plot-in- python/66052245#66052245 • https://stackoverflow.com/questions/64356412/highlig ht-time-interval-in-multivariate-time-series-plot-using- matplotlib-and-se?rq=1 import numpy as np import pandas as pd import matplotlib.pyplot as plt import matplotlib.dates as mdates # used only for method 2 plt.rcdefaults() # Create sample dataset # random number generator rng = np.random.default_rng(seed=1) 196
  • 198. Ch Ch Ch Ch Ch Ch Ch DT #https://stackoverflow.com/questions/48973471/how-to- highlight-weekends-for-time-series-line-plot-in- python/66052245#66052245 # Check the answer by Patrick Fitzgerald #Highlighting Weekends on a time series plot import numpy as np import pandas as pd import matplotlib.pyplot as plt import matplotlib.dates as mdates # Create sample dataset # random number generator rng = np.random.default_rng(seed=1234) dti = pd.date_range('2017-01-01', '2017-05-15', freq='D’) counts = 5000 + np.cumsum(rng.integers(-1000, 1000, size=dti.size)) df = pd.DataFrame(dict(Counts=counts), index=dti) # Draw pandas plot: x_compat=True converts the pandas x-axis units to matplotlib # date units (not strictly necessary when using a daily frequency like here) ax = df.plot(x_compat=True, figsize=(10, 7), legend = None, ylabel='Counts’) # reset y limits to display highlights without gaps ax.set_ylim(*ax.get_ylim()) # Highlight weekends based on the x-axis units xmin, xmax = ax.get_xlim() days = np.arange(np.floor(xmin), np.ceil(xmax)+2) weekends = [(dt.weekday()>=5)|(dt.weekday()==0) for dt in mdates.num2date(days)] ax.fill_between(days, *ax.get_ylim(), where=weekends, facecolor='k', alpha=.1) ax.set_xlim(xmin, xmax) # set limits back to default values # Create appropriate ticks using matplotlib date tick locators and formatters ax.xaxis.set_major_locator(mdates.MonthLocator()) ax.xaxis.set_minor_locator(mdates.MonthLocator(bymonthday=np.arange(5, 31, step=7))) ax.xaxis.set_major_formatter(mdates.DateFormatter('n%b')) ax.xaxis.set_minor_formatter(mdates.DateFormatter('%d')) # Additional formatting ax.figure.autofmt_xdate(rotation=0, ha='center') title = 'Daily count of trips with weekends highlighted from SAT 00:00 to MON 00:00' ax.set_title(title, pad=20, fontsize=14) plt.savefig('Daily count of trips with weekends highlighted.png',bbox_inches = "tight",pad_inches = 0.5, dpi=300) plt.show() Highlighting Weekends in a Time Series 198
  • 199. Ch Ch Ch Ch Ch Ch Ch DT #Matplotlib Release, Page no.120-121 #plt.figure creates a matplotlib.figure.Figure instance fig = plt.figure(figsize = (10,5)) rect = fig.patch # a rectangle instance rect.set_facecolor('lightgoldenrodyellow') ax1 = fig.add_axes([0.1, 0.3, 0.4, 0.4]) rect = ax1.patch rect.set_facecolor('lightslategray’) for label in ax1.xaxis.get_ticklabels(): # label is a Text instance label.set_color('red') label.set_rotation(45) label.set_fontsize(16) for line in ax1.yaxis.get_ticklines(): # line is a Line2D instance line.set_color('green') line.set_markersize(25) line.set_markeredgewidth(3) fig.savefig(f'tick_labels and lines.png',dpi=300, format='png', bbox_inches='tight') plt.show() import matplotlib.pyplot as plt import numpy as np fig, (ax1, ax2) = plt.subplots(1,2, sharey = True) x = np.random.randint(1,20,20) #Set the face color of figure background rect = fig.patch rect.set(facecolor = 'aliceblue’) #Set the face color of the axes backgrounds ax1.patch.set(facecolor = 'salmon') ax2.patch.set(facecolor = 'aquamarine') ax2.plot(x) plt.show() #Matplotlib Release, Page no.120-121 import numpy as np import matplotlib.pyplot as plt # Fixing random state for reproducibility np.random.seed(19680801) fig, ax = plt.subplots() ax.plot(100*np.random.rand(20)) ax.patch.set(facecolor="yellow",alpha =0.5) ax.patch.set_facecolor("yellow") ax.patch.set_alpha(0.5) fig.set(facecolor = "salmon") # Use automatic StrMethodFormatter ax.yaxis.set_major_formatter('${x:1.2f}') ax.yaxis.set_tick_params(which='major’, labelcolor='green’, labelleft=False, labelright=True) #ax.set_xticks(ax.get_xticks()) #ax.set_xticklabels(ax.get_xticklabels(), rotation = 30) plt.setp(ax.get_xticklabels(), rotation=30, ha='right') plt.show() Appearance of Plot Fig, Axes background Tick labels, Tick lines 199
  • 200. Ch Ch Ch Ch Ch Ch Ch DT #https://stackoverflow.com/questions/3584805/in- matplotlib-what-does-the-argument-mean-in-fig-add- subplot111?rq=1 Check the answer by compuphys The link lucidly discusses the multiple call signatures of fig.add_subplot method. The add_subplot() method has several call signatures: • add_subplot(nrows, ncols, index, **kwargs) • add_subplot(pos, **kwargs) • add_subplot(ax) • add_subplot() The following code demonstrates the difference between the first two call signatures. • add_subplot(nrows, ncols, index, **kwargs) • add_subplot(pos, **kwargs) The first call signature can accommodate any number of rows, columns. Whereas the second call signature is limited to total 9 subplots pos is a three digit integer, where the first digit is the number of rows, the second the number of columns, and the third the index of the subplot. i.e. fig.add_subplot(235) is the same as fig.add_subplot(2, 3, 5). Also, Check the documentation : https://matplotlib.org/stable/api/_as_gen/matplotlib.pypl ot.subplot.html import matplotlib.pyplot as plt def plot_and_text(axis, text): '''Simple function to add a straight line and text to an axis object''' axis.plot([0,1],[0,1]) axis.text(0.02, 0.9, text) f = plt.figure() f2 = plt.figure() _max = 12 for i in range(_max): axis = f.add_subplot(3,4,i+1, fc=(0,0,0,i/(_max*2)), xticks=[], yticks=[]) plot_and_text(axis,chr(i+97) + ') ' + '3,4,' +str(i+1)) # If this check isn't in place, a # ValueError: num must be 1 <= num <= 15, not 0 is raised if i < 9: axis = f2.add_subplot(341+i, fc=(0,0,0,i/(_max*2)), xticks=[], yticks=[]) plot_and_text(axis,chr(i+97) + ') ' + str(341+i)) f.tight_layout() f2.tight_layout() f._label = 'fig1' f2._label = 'fig2' for fig in [f, f2]: print(f'{fig._label}') fig.savefig(f'Adding Subplots_{fig._label}.png’, dpi = 300,format='png', bbox_inches='tight') plt.show() Making multiple subplots fig.add_subplot() Method 200
  • 201. Ch Ch Ch Ch Ch Ch Ch DT #Legend Call Signature : ax.legend() #All the lines have been assigned valid text labels import matplotlib.pyplot as plt fig, ax = plt.subplots() lgh1, = ax.plot([0, 0.5], [0, 1], '-g', label="a") lgh2, = ax.plot([0, 1], [0, 1], '-r', label = 'b') lgh3, = ax.plot([0, 1], [0, 0.5], '-b', label="c") #plt.legend() #This is equivalent to ax.legend() ax.legend() plt.show() #Legend Call Signature : ax.legend() #Except the red line, the other lines have been assigned text labels import matplotlib.pyplot as plt fig, ax = plt.subplots() lgh1, = ax.plot([0, 0.5], [0, 1], '-g', label="a") lgh2, = ax.plot([0, 1], [0, 1], '-r') lgh3, = ax.plot([0, 1], [0, 0.5], '-b', label="c") #plt.legend() #This is equivalent to ax.legend() ax.legend() plt.show() #https://stackoverflow.com/questions/54390421/matplotli b-legend-not-working-correctly-with-handles #Legend Call Signature : ax.legend(handles, labels) #Explicitly Passing the handles and corresponding labels import matplotlib.pyplot as plt fig, ax = plt.subplots() lgh1, = ax.plot([0, 0.5], [0, 1], '-g', label="a") lgh2, = ax.plot([0, 1], [0, 1], '-r') lgh3, = ax.plot([0, 1], [0, 0.5], '-b', label="c") handles = [lgh1, lgh2, lgh3] labels = [‘A’, ‘B’, ‘C’] #plt.legend(handles = handles, labels = labels) ax.legend(handles = handles, labels =labels) plt.show() Automatic Detection Legend Automatic Detection Legend Explicitly Define Legend Elements 201
  • 202. Ch Ch Ch Ch Ch Ch Ch DT #Legend Call Signature : ax.legend(handles) #The patches are not in the axes but specifically added for legend #Secondly, we have used a dictionary of labels and colors #https://stackoverflow.com/questions/39500265/manually -add-legend-items-python-matplotlib import matplotlib.patches as mpatches import matplotlib.pyplot as plt fig, ax = plt.subplots() legend_dict = { 'data1' : 'green', 'data2' : 'red’, 'data3' : 'blue' } patchList = [] for key in legend_dict: data_key = mpatches.Patch(color=legend_dict[key], label=key) patchList.append(data_key) ax.legend(handles=patchList) fig.savefig('legend_from dict.png',bbox_inches='tight') #Legend Call Signature : ax.legend() #Except the red line, the other lines have been assigned text labels #https://stackoverflow.com/questions/39500265/manually -add-legend-items-python-matplotlib import matplotlib.pyplot as plt from matplotlib.lines import Line2D fig, ax = plt.subplots(figsize=(5,3)) colors = ['black', 'red', 'green’] #Instantiating proxy line artists lines = [Line2D([0], [0], color=c, linewidth=3, linestyle='--’) for c in colors] labels = ['black data', 'red data', 'green data'] ax.legend(lines, labels) fig.savefig('proxy artists_line 2D.png',dpi = 150, bbox_inches='tight') #https://stackoverflow.com/questions/24867363/matplotli b-put-legend-symbols-on-the-right-of-the-labels fig, ax = plt.subplots(figsize = (5,3)) ax.plot([1,2],[3,4], label='labeltext') ax.legend(markerfirst=False) plt.gcf().savefig('markerfirst_legend.png',dpi = 150, bbox_inches='tight') plt.show() Proxy Artists : Manual Patch Addition Proxy Artists : Manual Lines Addition Labels Appearing before Legend key 202
  • 203. Ch Ch Ch Ch Ch Ch Ch DT #Legend Call Signature : ax.legend(handles) #The patches are not in the axes but specifically added for legend #https://stackoverflow.com/questions/27826064/matplotli b-make-legend-keys-square import matplotlib.lines as mlines plt.rcdefaults() fig, ax = plt.subplots(figsize = (5,3)) rect1 = mlines.Line2D([], [], marker="s", markersize=10, linewidth=0, color="red") rect2 = mlines.Line2D([], [], marker="s", markersize=10, linewidth=0, color="yellow") ax.legend((rect1, rect2), ('Men', 'Women')) fig.savefig('Making square markers.png',dpi = 150, bbox_inches='tight') plt.show() https://matplotlib.org/stable/gallery/text_labels_and_ann otations/legend_demo.html#sphx-glr-gallery-text-labels- and-annotations-legend-demo-py Check the link above. There are various other use cases of legend covered. #https://stackoverflow.com/questions/44603480/remove- legend-key-in-matplotlib import matplotlib.pyplot as plt import numpy as np np.random.seed(100) x = np.random.normal(size=5000) fig, ax = plt.subplots() ax.hist(x, label = 'something') ax.legend() leg = plt.gca().get_legend() leg.legendHandles[0].set_visible(False) plt.show() Making Legend Keys square Legend Addition in Complex Plots Remove Legend key 203
  • 204. Ch Ch Ch Ch Ch Ch Ch DT #https://stackoverflow.com/questions/44413020/how-to-specify-legend-position-in-matplotlib-in-graph-coordinates? import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = 12, 4 fig, axes = plt.subplots(ncols=3) locs = ["upper left", "lower left", "center right"] for l, ax in zip(locs, axes.flatten()): #Iterating over the subplots to create line plots ax.set_title(l) ax.plot([1,2,3],[2,3,1], "b-", label="blue") ax.plot([1,2,3],[1,2,1], "r-", label="red") ax.legend(loc=l, bbox_to_anchor=(0.6,0.5)) #Note the green dot in the axes is the bbox_to_anchor ax.scatter((0.6),(0.5), s=81, c="limegreen", transform=ax.transAxes) plt.tight_layout() fig.savefig('Legend loc.png', dpi = 300) plt.show() Legend ‘loc’ argument upper left upper center upper right center left center center right lower left lower center lower right 204
  • 205. Ch Ch Ch Ch Ch Ch Ch DT import matplotlib.pyplot as plt fig = plt.figure(dpi=100, figsize=(11, 8), tight_layout=True) available = ['default'] + plt.style.available #Creating sample data x = ['Product A','Product B', 'Product C'] y = [100,130,220] n, p = 6, 1 for i, style in enumerate(available[n*p:n*(p+1)]): with plt.style.context(style): ax = fig.add_subplot(2,3, i + 1) ax.bar(x,y) ax.set_title(style) plt.savefig('Styles_second_2.png',bbox_inches = "tight",pad_inches = 0.05, dpi=300) plt.show() import matplotlib.pyplot as plt fig = plt.figure(dpi=100, figsize=(11, 8), tight_layout=True) available = ['default'] + plt.style.available #Creating sample data x = ['Product A','Product B', 'Product C'] y = [100,130,220] n,p = 6, 0 for i, style in enumerate(available[n*p:n*(p+1]): with plt.style.context(style): ax = fig.add_subplot(2,3, i + 1) ax.bar(x,y) ax.set_title(style) plt.savefig('Styles_second_2.png',bbox_inches = "tight",pad_inches = 0.05, dpi=300) plt.show() Style Sheets in Matplotlib Back 205
  • 206. Ch Ch Ch Ch Ch Ch Ch DT import matplotlib.pyplot as plt import numpy as np import matplotlib.ticker as ticker #Instantiating figure and axes fig, ax = plt.subplots(2,2,figsize= (10,5), sharey = True) x = np.random.randint(3,20,20) #Setting background color of figure rect = fig.patch rect.set(facecolor = 'lightgrey', alpha = 0.5) ax1,ax2,ax3,ax4 = ax.flatten() #ax1.patch.set(facecolor = 'salmon') #ax2.patch.set(facecolor = 'aquamarine’) # Setting different colors for the axes using colormap #colors = [ plt.cm.Pastel1(x) for x in np.linspace(0, 1,4)] #colors = [ plt.cm.tab20c(x) for x in np.linspace(0, 1,4)] #Accessing colors from the colormap colors = [ plt.cm.Accent(x) for x in np.linspace(0,0.4,4)] [ax.patch.set(facecolor = colors[i]) for i,ax in enumerate(fig.axes)] #Line Plot in axes ax2 ax2.plot(x) # Setting x and y limits in all the axes [ax.set_xlim(0,10) for ax in fig.axes] [ax.set_ylim(0,20) for ax in fig.axes] #Setting the tick locators using locator class ax1.xaxis.set_major_locator(ticker.NullLocator()) ax2.xaxis.set_major_locator(ticker.MultipleLocator(2)) ax3.xaxis.set_major_locator(ticker.FixedLocator([0,1,2,8,9])) ax4.xaxis.set_major_locator(ticker.LinearLocator(5)) #Making list of Locator labels Locator_labels = ['NullLocator()', 'MultipleLocator(2)', 'FixedLocator([0,1,2,8,9])', 'LinearLocatorn(numticks = 5)’] #Adding text iteratively to the figure axes for ax, label in zip(fig.axes,Locator_labels): ax.text(x = 5,y = 2, s = label, rotation_mode = 'anchor', ha = 'center', size = 18) fig.savefig('Tick Locators_2.png', dpi=300, format='png', bbox_inches='tight’) plt.show() Tick Locators Back 206
  • 207. Ch Ch Ch Ch Ch Ch Ch DT #https://matplotlib.org/stable/gallery/ticks_and_spines/ti ck-formatters.html?highlight=formatter import matplotlib.pyplot as plt from matplotlib import ticker def setup(ax, title): """Set up common parameters for the Axes in the example.""" # only show the bottom spine ax.yaxis.set_major_locator(ticker.NullLocator()) ax.spines.right.set_color('none’) ax.spines.left.set_color('none’) ax.spines.top.set_color('none’) # define tick positions ax.xaxis.set_major_locator(ticker.MultipleLocator(1.00)) ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.25)) ax.xaxis.set_ticks_position('bottom’) ax.tick_params(which='major', width=1.00, length=5) ax.tick_params(which='minor', width=0.75, length=2.5, labelsize=10) ax.set_xlim(0, 5) ax.set_ylim(0, 1) ax.text(0.0, 0.2, title, transform=ax.transAxes, fontsize=14, fontname='Monospace’, color='tab:blue’) TICK FORMATTERS (1 of 3) - CODE Back Code continues to Next slide Code Start 207
  • 208. Ch Ch Ch Ch Ch Ch Ch DT #https://matplotlib.org/stable/gallery/ticks_and_spines/tick-formatters.html?highlight=formatter # Tick formatters can be set in one of two ways, either by passing a ``str`` # or function to `~.Axis.set_major_formatter` or `~.Axis.set_minor_formatter`, # or by creating an instance of one of the various `~.ticker.Formatter` classes # and providing that to `~.Axis.set_major_formatter` or`~.Axis.set_minor_formatter`. # The first two examples directly pass a ``str`` or function. fig0, axs0 = plt.subplots(2, 1, figsize=(8, 2)) fig0.suptitle('Simple Formatting’) # A ``str``, using format string function syntax, can be used directly as a # formatter. The variable ``x`` is the tick value and the variable ``pos`` is # tick position. This creates a StrMethodFormatter automatically. setup(axs0[0], title="'{x} km’”) axs0[0].xaxis.set_major_formatter('{x} km’) # A function can also be used directly as a formatter. The function must take # two arguments: ``x`` for the tick value and ``pos`` for the tick position, # and must return a ``str`` This creates a FuncFormatter automatically. setup(axs0[1], title="lambda x, pos: str(x-5)") axs0[1].xaxis.set_major_formatter(lambda x, pos: str(x-5)) fig0.tight_layout() TICK FORMATTERS(2 of 3) – CODE CONTINUED Code continues to Next slide Code continues from Prev slide 208
  • 209. Ch Ch Ch Ch Ch Ch Ch DT # StrMethod formatter setup(axs1[1], title="StrMethodFormatter('{x:.3f}’)”) axs1[1].xaxis.set_major_formatter(ticker.StrMethodFormatter("{x:.3f}")) # FuncFormatter can be used as a decorator @ticker.FuncFormatter def major_formatter(x, pos): return f'[{x:.2f}]’ setup(axs1[2], title='FuncFormatter("[{:.2f}]".format)’) axs1[2].xaxis.set_major_formatter(major_formatter) # Fixed formatter setup(axs1[3], title="FixedFormatter(['A', 'B', 'C', ...])") # FixedFormatter should only be used together with FixedLocator. # Otherwise, one cannot be sure where the labels will end up. positions = [0, 1, 2, 3, 4, 5] labels = ['A', 'B', 'C', 'D', 'E', 'F’] axs1[3].xaxis.set_major_locator(ticker.FixedLocator(positions)) axs1[3].xaxis.set_major_formatter(ticker.FixedFormatter(labels)) # Scalar formatter setup(axs1[4], title="ScalarFormatter()") axs1[4].xaxis.set_major_formatter(ticker.ScalarFormatter(useMathText=True)) # FormatStr formatter setup(axs1[5], title="FormatStrFormatter('#%d’)”) axs1[5].xaxis.set_major_formatter(ticker.FormatStrFormatter("#%d")) # Percent formatter setup(axs1[6], title="PercentFormatter(xmax=5)") axs1[6].xaxis.set_major_formatter(ticker.PercentFormatter(xmax=5)) fig1.tight_layout() plt.show() #https://matplotlib.org/stable/gallery/ticks_and_spines/tick- formatters.html?highlight=formatter # The remaining examples use Formatter objects. fig1, axs1 = plt.subplots(7, 1, figsize=(8, 6)) fig1.suptitle('Formatter Object Formatting’) # Null formatter setup(axs1[0], title="NullFormatter()") axs1[0].xaxis.set_major_formatter(ticker.NullFormatter()) TICK FORMATTERS(3 of 3) – CODE CONTINUED Code End Code continues from Prev slide 209
  • 210. Ch Ch Ch Ch Ch Ch Ch DT import matplotlib.pyplot as plt from matplotlib.patches import Ellipse import numpy as np from matplotlib.text import OffsetFrom plt.rcdefaults() #Instantiating Figure and Axes fig, (ax1, ax2) = plt.subplots(1, 2, figsize = (12,5)) fig.patch.set(edgecolor = ‘0.8’, lw = 4) #Configuring bbox and arrow arguments for axes, data and fig bbox_args_axes = dict(boxstyle ="round", fc ="salmon", alpha = 0.6) bbox_args_data = dict(boxstyle ="round", fc ="cyan", alpha = 0.6) bbox_args_fig = dict(boxstyle ="round", fc ="lightgreen", alpha = 0.6) arrow_args = dict(arrowstyle ="->") ax1.annotate(text = '(1,0)nax1 axesncoordinate system’, xy =(1,0), xycoords = ax1.transAxes, xytext=(2,5 ), textcoords=ax1.transData,fontsize = 15, bbox = bbox_args_axes, arrowprops = arrow_args, ha = 'left') ax1.annotate(text = '(1,1)nax1 axesncoordinate system', xy =(1,1),xycoords = ax1.transAxes, xytext=(2,17.5), textcoords=ax1.transData,fontsize = 15, bbox = bbox_args_axes, arrowprops = arrow_args, va = 'top') ax2.annotate(text = '(0,1)nax2 axesncoordinate system’, xy =(0.0,1),xycoords = ax2.transAxes, xytext=(0.5, 22.5), textcoords=ax2.transData,fontsize = 15, bbox = bbox_args_axes, arrowprops = arrow_args) ax2.annotate(text = '(0,20)nax2 datancoordinate system', xy =(0,20),xycoords = ax2.transData, xytext=(-5, 22.5), textcoords=ax2.transData,fontsize = 15, bbox = bbox_args_data, arrowprops = arrow_args) ax1.annotate(text = '(0,0.5)nfigncoordinate system', xy =(0,0.5),xycoords = fig.transFigure, xytext=(0.03, 0.95), textcoords=fig.transFigure, fontsize = 15, bbox = bbox_args_fig, arrowprops = arrow_args) ax2.annotate(text = '(1,1)nfigncoordinate system’, xy =(1,1.1),xycoords = fig.transFigure, xytext=(7, 25), textcoords=ax2.transData,fontsize = 15, va = 'top’, bbox = bbox_args_fig, arrowprops = arrow_args ) ax2.annotate(text = '(1,1)nax2 axesncoordinate system’, xy =(1,1),xycoords = ax2.transAxes, xytext=(2,17.5), textcoords=ax2.transData,fontsize = 15, va = 'top', bbox = bbox_args_axes, arrowprops = arrow_args) [ax.set_xlim(0,10) for ax in fig.axes] #Setting x limits in all the axes [ax.set_ylim(0,20) for ax in fig.axes] #Setting y limits in all the axes ax2.text(x = 0.5, y = 0.5, s = “Axes instance : ax2”, ha = ‘center’,transform = ax2.transAxes, fontsize = 20) ax1.text(x = 0.5, y = 0.5, s = “Axes instance : ax1”, ha = ‘center’,transform = ax1.transAxes, fontsize = 20) fig.text(x = 0.02, y = 0.02, s = “Figure instance”, fontsize = 20) plt.subplots_adjust(bottom =0.2) #Adjusting padding #plt.tight_layout(rect = [0,0,0.8,0.8]) fig.savefig(‘coordinate systems.png’, dpi = 150, format=‘png’, bbox_inches='tight’ ) Coordinate System Transform Objects Back Code End Code start 210
  • 211. Ch Ch Ch Ch Ch Ch Ch DT import matplotlib.gridspec as gridspec import matplotlib.pyplot as plt fig = plt.figure(figsize=(13.5, 7)) #Creating Gridspec object spec = gridspec.GridSpec(nrows = 5,ncols = 11, wspace = 0.2, hspace=0.1) #Create Axes for Jupyter ax1 = fig.add_subplot(spec[:, 0]) #Create Axes IPython ax2 = fig.add_subplot(spec[:, 1]) # Create Axes for scikit ax3 = fig.add_subplot(spec[0, 2::]) # Create Axes for Statsmodels ax4 = fig.add_subplot(spec[1, 2:4]) # Create Axes for Seaborn ax5 = fig.add_subplot(spec[1, 4:8]) # Create Axes for Scipy ax6 = fig.add_subplot(spec[1:3, 8:11]) # Create Axes for Pandas ax7 = fig.add_subplot(spec[2, 2:5]) # Create Axes for Matplotlib ax8 = fig.add_subplot(spec[2, 5:8]) # Create Axes for Numpy ax9 = fig.add_subplot(spec[3, 2:11]) # Create Axes for Python ax10 = fig.add_subplot(spec[4, 2:11]) for ax in fig.axes: plt.sca(ax) plt.xticks([]) #Removing x ticks plt.yticks([]) #Removing y ticks #Create labels list containing axes names labels = ['Jupyter', 'Ipython', 'scikit', 'statsmodels’, 'Seaborn', 'Scipy’, 'Pandas', 'Matplotlib’, 'Numpy', 'Python’] ax_list = [ax1, ax2, ax3, ax4, ax5, ax6, ax7, ax8,ax9, ax10] #Adding text to axes for i,(ax,label) in enumerate(zip(ax_list,labels)): if i <=1 : ax.text(0.5, 0.5, label, transform = ax.transAxes, fontsize = 20, ha = 'center’, rotation = 90, va = 'center') else : ax.text(0.5, 0.5, label, transform = ax.transAxes, fontsize = 20, ha = 'center') fig.savefig('Libraries_Python.png', dpi = 300, format='png’, bbox_inches='tight') GridSpec Use Display structure of important packages Back 211
  • 212. Ch Ch Ch Ch Ch Ch Ch DT import matplotlib.pyplot as plt import matplotlib.ticker as ticker fig, ax = plt.subplots(1,3, figsize=(18,6)) # Setting both xlim and ylim as (0,20) [ax_obj.set(ylim = (0,20), xlim = (0,20)) for ax_obj in fig.axes] # Plotting a scatter point at (10,10) [ax_obj.scatter([10],[10], color = 'r') for ax_obj in fig.axes] # Setting number of ticks in both x axis and y axis to 5 in all the axes [ax_obj.xaxis.set_major_locator(ticker.LinearLocator(5)) for ax_obj in fig.axes] [ax_obj.yaxis.set_major_locator(ticker.LinearLocator(5)) for ax_obj in fig.axes] # Creating a list of horizontal alignment values ha_values = ['left', 'center', 'right’] # Setting the horizontal alignment by iterating over axes for i, ax_obj in enumerate(fig.axes): if i < len(ha_values): ax_obj.text(10,10, f'ha : {ha_values[i]}', rotation = 20, rotation_mode = 'anchor’, ha = ha_values[i],fontsize = 22) #Setting the figure background color to white fig.patch.set(color = 'white') # Adding a title fig.suptitle("Horizontal Alignment Demo - 'left', 'center’, 'right'nrotation = 20, rotation_mode = 'anchor' nposition = (10,10)", x = 0.05, y = 0.95, fontsize = 22, ha = 'left' ) plt.tight_layout(rect = [0,0,1,0.95]) fig.savefig("Horizontal Alignment_vertical plots.png”, dpi=300, format='png', bbox_inches='tight') Horizontal Alignment of Text Instances Back Code End Code Start 212
  • 213. Ch Ch Ch Ch Ch Ch Ch DT # UDF for Adding Annotations to place data labels def Add_data_labels(rect): #rect is ax.patches object for p in rect: #Retrieving the Axes container of patch object ax = rect[0].axes #Adding text iteratively on top of each bar #Using get and set on x,y parameters of rectangle patch ax.text( x = p.get_x()+p.get_width()/2, y = ax.get_ylim()[1]*0.01+ p.get_height(), s = p.get_height(), ha = 'center', size = 12) #User Defined Function to stylize Axes def stylize_axes(ax): #Making the axes background light gray ax.set_facecolor('.85’) # Setting the y limits ax.set_ylim(0,ax.get_ylim()[1]*1.1) #Making the tick lines disppear ax.tick_params(length=0) #Making the xtick labels rotated [labels.set(size = 13,rotation = 15, rotation_mode = 'anchor',ha = 'right’) for labels in ax.get_xticklabels()] #Setting grid lines to white color ax.grid(True, axis='x', color='white’) #Setting grid lines below all the artists ax.set_axisbelow(True) #Making all the spines invisible [spine.set_visible(False) for spine in ax.spines.values()] #Code snippet that uses User defined functions – stylize_axes and Add_data_labels # Doing the necessary imports import matplotlib.pyplot as plt import numpy as np import matplotlib.spines as spines plt.rcdefaults() %matplotlib inline #create a list of rgba colors from a matplotlib.cm colormap colors = [ plt.cm.Dark2(x) for x in np.linspace(0, 1, len(x))] #Instantiating figure, axes fig, ax = plt.subplots(figsize = (10,6), frameon = True) #Creating a bar plot by ax.bar method # Note the colors passed as the color argument bars = ax.bar(x,y, color = colors) #Setting the title of plot StrTitle = 'Consumption of Packed beverages in billion liters,n by Beverage Type, Global, 2019' ax.set_title(StrTitle, size = 16) #Using the user defined functions – to add data labels and stylize the axes Add_data_labels(ax.patches) stylize_axes(ax) fig.savefig('Patches.png', dpi=300, format='png', bbox_inches='tight') data = {'Water':465, 'Alchoholic Beverages':271, 'Carbonated soft drinks': 224, 'Milk and Dairy products': 260, 'New Drinks': 75, 'Fruits and Vegetable Juices': 86} x = list(data.keys()) y = list(data.values()) Also check : https://www.dunderdata.com/blog/create-a- bar-chart-race-animation-in-python-with-matplotlib Note : The data figures in the graph are fictitious. UDF stands for User Defined Function. Bar Charts Back Code End Code Start 213
  • 214. Ch Ch Ch Ch Ch Ch Ch DT import numpy as np import matplotlib.pyplot as plt import matplotlib.spines as spines from matplotlib import cm colors = [ cm.Dark2(x) for x in np.linspace(0, 1, len(x))] %matplotlib inline #Instantiating figure and axes fig, ax = plt.subplots(figsize = (9,7), frameon = True) t = ax.bar(x,y, color = colors) #Setting xticklines visibility to false [ticks.set(visible= False)for ticks in ax.get_xticklines()] #Setting the y limits to leave ample room above bar ax.set_ylim(0,ax.get_ylim()[1]*1.1) #Rotating and aligning the xtick labels for labels in ax.get_xticklabels(): labels.set(rotation = 15, size = 12, rotation_mode = 'anchor', ha = 'right' ) #Rotating and aligning the ytick labels for labels in ax.yaxis.get_ticklabels(): plt.setp(labels, size = 14, rotation_mode = 'anchor’, ha = 'right’) #Setting the figure and axes background colors ax.patch.set(facecolor = 'white') plt.setp(fig.patch, facecolor = 'lightgoldenrodyellow’, edgecolor = 'grey', lw = 4) #Making the y gridlines lighter by setting opacity to 0.5 for lines in ax.get_ygridlines(): lines.set(alpha = 0.5) #Setting off the xgridlines to False ax.grid(axis = 'x', b = False) # Adding Annotations to place data labels for p in ax.patches: ax.text(p.get_x()+p.get_width()/2, ax.get_ylim()[1]*0.01+ p.get_height(), p.get_height(), ha = 'center', size = 14 ) # Adding Legend labels and handles [p.set_label(x[i]) for i,p in zip(range(len(x)), ax.patches)] ax.legend(bbox_to_anchor = (1.04,.5), loc="upper left", borderaxespad=0, fontsize = 'large’) #Setting title of the axes StrTitle = 'Consumption of Packed beverages in billion liters,n by Beverage Type, Global, 2019' ax.set_title(StrTitle, size = 16) fig.savefig('Consumption_4.png', dpi=300, format='png', bbox_inches='tight') data = {'Water':465, 'Alchoholic Beverages':271, 'Carbonated soft drinks': 224, 'Milk and Dairy products': 260, 'New Drinks': 75, 'Fruits and Vegetable Juices': 86} x = list(data.keys()) y = list(data.values()) #Basic Bar Chart import matplotlib.pyplot as plt #data x = [1, 2, 3, 4, 5] h = [10, 8, 12, 5, 7] c = ['red', 'yellow', 'black', 'blue', 'orange'] #bar plot plt.bar(x, height = h, color = c) fig = plt.gcf() plt.show() Bar Chart With Legend Back 214
  • 215. Ch Ch Ch Ch Ch Ch Ch DT #Using CN color specification color = ’C9’ from matplotlib import cm colors = cm.Set1.colors[1:] ax.barh(x,y, color = colors) #Alternatively Accessing colors from Palettable external library # https://jiffyclub.github.io/palettable/#matplotlib-discrete-colormap colors =palettable.colorbrewer.qualitative.Dark2_7.mpl_colors color = ’C9’ #Using CN color specification ax.set_axisbelow(False) #To place grid lines (integral part of axes) above bars ax.grid(axis = 'y', which = 'major',color = '0.75',b = True,ls = '-') https://stackoverflow.com/questions/1726391/matplotlib-draw-grid-lines-behind-other- graph-elements #Using Named color in top right for the facecolor or fc of bars color = ’salmon’ ax.bar(x,y, color = 'salmon') Bar Chart Styling 215
  • 216. Ch Ch Ch Ch Ch Ch Ch DT #https://datascience.stackexchange.com/questions/57853 /group-small-values-in-a-pie-chart #Changes vis-à-vis the code in the link : • Using colors from external library • Dictionary key value pairings • Placement of the legend and the corresponding transform object used import matplotlib.pyplot as plt import numpy as np plt.rcdefaults() dic = {'Class a': 26.9, 'Class b': 18, 'Class c': 16.8, 'Class d': 13, 'Class e': 9.83, 'Class f': 9, 'Class g': 3.59, 'Class h': 0.08, 'Class i': 1.42, 'Class j': 1.09, 'Class k': 0.903, 'Class l': 0.873, 'Class m': 0.28, 'Class n': 0.24, 'Class o': 0.112} # group together all elements in the dictionary whose value is less than 2 # name this group 'All the rest' import itertools newdic={} for key, group in itertools.groupby(dic, lambda k: 'All the rest' if (dic[k]<2) else k): newdic[key] = sum([dic[k] for k in list(group)]) #Extracting labels and sizes from the dictionary labels = newdic.keys() sizes = newdic.values() #Accessing colors from external library Palettable from palettable.cartocolors.qualitative import Bold_10 colors = Bold_10.mpl_colors #Instantiating figure and axes fig, ax = plt.subplots() #Creating pie chart and assigning to variable pie_list = ax.pie(sizes, labels=labels, autopct='%1.1f%%', explode=(0,0,0,0,0,0,0,.2), startangle=0, colors = colors) #Note pie_list is a tuple containing list of wedges and text #Set equal scaling of the axes (make circles circular) ax.axis('equal’) #Setting the background color of figure to white fig.patch.set(facecolor = "white") #Setting the font size and colors for the value and text labels [txt.set(color = 'white', fontsize = 15) for txt in pie_list[2]] [txt.set(color = 'black', fontsize = 12) for txt in pie_list[1]] #Adding a Legend. Note the transform object used plt.legend(pie_list[0],labels, bbox_to_anchor=(1,0.9), loc="upper left", fontsize=10, bbox_transform=plt.gca().transAxes) #Alternate Legend #plt.legend(pie_list[0],labels, bbox_to_anchor=(1,1), loc="upper right", fontsize=10, bbox_transform=plt.gcf().transFigure) plt.tight_layout() #The color code can be replaced by any of the other colormaps as below #Accessed colors count same as number of values num = len(newdic.values()) #num for number of values colors = [ plt.cm.tab20c(x) for x in np.linspace(0, 1,num)] colors = plt.cm.Dark2(range(15)) colors = plt.cm.tab20(range(15)) colors = [ plt.cm.viridis(x) for x in np.linspace(0, 1, num)] colors = [ plt.cm.Set3(x) for x in np.linspace(0, 1, num)] colors = plt.cm.tab10.colors colors = plt.cm.Paired.colors colors = plt.cm.nipy_spectral(np.linspace(0.1,1,num)) colors = plt.cm.CMRmap(np.linspace(0.2,1,num)) Pie Charts Back Code End Code Start fig.savefig('PieChart.png',dpi=300, format='png’, bbox_inches='tight') plt.show() #Also check link 216
  • 217. Ch Ch Ch Ch Ch Ch Ch DT import palettable import matplotlib.pyplot as plt import numpy as np #Creating random data for the pie chart data = np.random.randint(50,200, size=30) #Instantiating figure fig = plt.figure(figsize = (10,7)) #Axes for nipy_spectral colormap color = plt.cm.nipy_spectral(np.linspace(0,.9,len(data))) cc = plt.cycler("color", color) with plt.style.context({"axes.prop_cycle" : cc}): ax1 = fig.add_subplot(231, aspect="equal") ax1.pie(data) ax1.set_title('nipy_spectral', fontsize = 20, fontweight = 'medium', y = 1, x= 0.5, transform = ax.transAxes) #Axes for CMRmap color = plt.cm.CMRmap(np.linspace(0,0.9,len(data))) cc = plt.cycler("color", color) with plt.style.context({"axes.prop_cycle" : cc}): ax2 = fig.add_subplot(232, aspect="equal") ax2.pie(data) ax2.set_title('CMRmap', fontsize = 20, fontweight = 'medium’, y = 1, x= 0.5, transform = ax2.transAxes) #Axes for jet colormap color = plt.cm.jet(np.linspace(0,0.9,len(data))) cc = plt.cycler("color", color) with plt.style.context({"axes.prop_cycle" : cc}): ax3 = fig.add_subplot(233, aspect="equal") ax3.pie(data) ax3.set_title('jet', fontsize = 20, fontweight = 'medium’, y = 1, x= 0.5, transform = ax3.transAxes) #Axes for tab20 colormap color = plt.cm.tab20(np.linspace(0,1,len(data)))) cc = plt.cycler("color", with plt.style.context({"axes.prop_cycle" : cc}): ax4 = fig.add_subplot(234, aspect="equal") ax4.pie(data) ax4.set_title('tab20', fontsize = 20, fontweight = 'medium', y = 1, x= 0.5, transform = ax4.transAxes) #Axes for tab10 colormap color = plt.cm.tab10(np.linspace(0,1,len(data))) cc = plt.cycler("color", color) with plt.style.context({"axes.prop_cycle" : cc}): ax5 = fig.add_subplot(235, aspect="equal") ax5.pie(data) ax5.set_title('tab10', fontsize = 20, fontweight 'medium', y = 1, x= 0.5, transform = ax5.transAxes) #Axes for Bold_10 colormap from palettable from palettable.cartocolors.qualitative import Bold_10 color = Bold_10.mpl_colors cc = plt.cycler("color",Bold_10.mpl_colors) with plt.style.context({"axes.prop_cycle" : cc}): ax6 = fig.add_subplot(236, aspect="equal") ax6.pie(data) ax6.set_title('Bold_10', fontsize = 20, fontweight = 'medium', y = 1, x= 0.5, transform = ax6.transAxes) fig.savefig("Piecharts 3.png",dpi=300, format='png’, bbox_inches='tight') plt.show() Check the link : https://stackoverflow.com/questions/52134364/how- could-i-get-a-different-pie-chart-color Check answer by ImportanceOfBeingErnest The code has been tweaked for six pie charts in this slide. There is scope to optimize the code with iteration through a list of colors from six colormaps to draw the corresponding pie charts. Further, check the discussion in the link. Currently maximum 20 colors available from the listed colormaps under the qualitative colormaps list. Namely the colormaps tab20, tab20b, tab20c. However, more colors can be obtained from Linear Segmented Colormaps by using the methods in section ACCESSING COLORS FROM A MATPLOTLIB COLORMAP. Note depending on the colormap accessed, the colors extracted may look similar but having different luminosity levels. Pie Chart colors 217
  • 218. Ch Ch Ch Ch Ch Ch Ch DT #https://stackoverflow.com/questions/20551477/changing- line-properties-in-matplotlib-pie-chart Three use case scenarios : ü Setting the width of the lines separating the pie wedges ü Setting the facecolor or edgecolor of the wedges ü Retrieving the wedges from ax.patches if one has an axes object but no direct access to the pie's wedges Also check : • Axes.pie() method Matplotlib, Release 3.4.2, Pg No. 1286 • wedge properties in Matplotlib, Release 3.4.2, Pg No. 2421-23 #Setting the linewidth, edge color of the wedges #Necessary imports import matplotlib.pyplot as plt import numpy as np # Fixing random state for reproducibility np.random.seed(19680765) ax = plt.subplot(111) wedges, texts = ax.pie(np.abs(np.random.randn(6))) for w in wedges: w.set_linewidth(2) w.set_edgecolor('cyan’) fig = ax.figure fig.savefig('PieChart_linewidths.png',dpi=300, format='png') # Retrieving wedges from axes object when no direct access wedges = [patch for patch in ax.patches if isinstance(patch, matplotlib.patches.Wedge)] Pie Chart Wedge Properties 218
  • 219. Ch Ch Ch Ch Ch Ch Ch DT cell_width = 212 cell_height = 22 swatch_width = 48 margin = 12 topmargin = 40 p = colcount*rowcount # Sort colors by hue, saturation, value and name if sort_colors is True: by_hsv = sorted((tuple(mcolors.rgb_to_hsv(mcolors.to_rgb(color))), name) for name, color in colors.items()) names = [name for hsv, name in by_hsv] else: names = list(colors) n = len(names[x*p:(x+1)*p]) nrows = rowcount - emptyrows ncols = n // nrows + int(n % nrows > 0) width = cell_width * ncols + 2 * margin height = cell_height * nrows + margin + topmargin dpi = 72 fig, ax = plt.subplots(figsize=(width / dpi, height / dpi), dpi=dpi) fig.subplots_adjust(margin/width, margin/height, (width-margin)/width, (height-topmargin)/height) XKCD Colors ax.set_xlim(0, cell_width * ncols) ax.set_ylim(cell_height * (nrows-0.5), -cell_height/2.) ax.yaxis.set_visible(False) ax.xaxis.set_visible(False) ax.set_axis_off() ax.set_title(title, fontsize=20, loc="left", pad=10) for i, name in enumerate(names[x*p:(x+1)*p]): row = i % nrows col = i // nrows y = row * cell_height swatch_start_x = cell_width * col text_pos_x = cell_width * col + swatch_width + 7 ax.text(text_pos_x, y, name, fontsize=14, horizontalalignment='left', verticalalignment='center') ax.add_patch(Rectangle(xy=(swatch_start_x, y-9), width=swatch_width, height=18, facecolor=colors[name], edgecolor='0.7')) filename = f'XKCD_Colors_{x+2}.png' return fig, filename #Creating the figure using the user defined function xkcd_fig, filename = plot_colortable(mcolors.XKCD_COLORS, "XKCD Colors", x = 7, rowcount = 30, colcount = 4) xkcd_fig.patch.set(facecolor = 'white') #Creating user defined function to return a tuple – first element being a figure containing color patches and second element being the figure name from matplotlib.patches import Rectangle import matplotlib.pyplot as plt import matplotlib.colors as mcolors def plot_colortable(colors, title, sort_colors=True, emptyrows=0, x=0, colcount = 4 , rowcount = 30 ): """colors : any of the color tables from mcolors module title : title to be displayed in the figure sort_colors : Whether the colors are to be sorted x : the iteration count colcount : number of columns rowcount : number of rows """ Back 219
  • 220. Ch Ch Ch Ch Ch Ch Ch DT #https://matplotlib.org/stable/gallery/statistics/hist.html# sphx-glr-gallery-statistics-hist-py import matplotlib.pyplot as plt import numpy as np from matplotlib import colors from matplotlib.ticker import PercentFormatter plt.rcdefaults() # Fixing random state for reproducibility np.random.seed(19680801) N_points = 100000 n_bins = 20 # Generate a normal distribution, center at x=0 and y=5 x = np.random.randn(N_points) y = .4 * x + np.random.randn(100000) + 5 #Instantiating figure and axes fig, axs = plt.subplots(1, 2, tight_layout=True) # N is the count in each bin, bins is the lower-limit of the bin N, bins, patches = axs[0].hist(x, bins=n_bins) # We'll color code by height, but you could use any scalar fracs = N / N.max() # we need to normalize the data to 0..1 for the full range of the colormap norm = colors.Normalize(fracs.min(), fracs.max()) # Now, we'll loop through our objects and set the color of each accordingly for thisfrac, thispatch in zip(fracs, patches): color = plt.cm.viridis(norm(thisfrac)) thispatch.set_facecolor(color) # We can also normalize our inputs by the total number of counts axs[1].hist(x, bins=n_bins, density=True) # Now we format the y-axis to display percentage axs[1].yaxis.set_major_formatter(PercentFormatter(xmax= 1)) fig.savefig(f'Histograms_colored.png',dpi=300, format='png', bbox_inches='tight’) plt.show() Histogram Color Coded Back 220
  • 221. Ch Ch Ch Ch Ch Ch Ch DT #https://jakevdp.github.io/PythonDataScienceHandbook/04.08-multiple- subplots.html import matplotlib.pyplot as plt import numpy as np plt.rcdefaults() # Create some normally distributed data mean = [0, 0] cov = [[1, 1], [1, 2]] prng = np.random.default_rng(123456) x, y = prng.multivariate_normal(mean, cov, 3000).T # Set up the axes with gridspec fig = plt.figure(figsize=(6, 6)) grid = plt.GridSpec(4, 4, hspace=0.2, wspace=0.2) main_ax = fig.add_subplot(grid[:-1, 1:]) y_hist = fig.add_subplot(grid[:-1, 0], xticklabels=[], sharey=main_ax) x_hist = fig.add_subplot(grid[-1, 1:], yticklabels=[], sharex=main_ax) # scatter points on the main axes main_ax.plot(x, y, 'ok', markersize=3, alpha=0.2) # histogram on the attached axes x_hist.hist(x, 40, histtype='stepfilled’, orientation='vertical', color='gray') x_hist.invert_yaxis() y_hist.hist(y, 40, histtype='stepfilled', orientation='horizontal', color='gray') y_hist.invert_xaxis() fig.savefig('Marginal histograms.png', dpi = 300, transparent = True) Marginal Histogram Back 221
  • 222. Ch Ch Ch Ch Ch Ch Ch DT #https://stackoverflow.com/questions/43962735/creating-barplots-using-for-loop- using-pandas-matplotlib import numpy as np import pandas as pd import matplotlib.pyplot as plt # Create some dummy data data = pd.DataFrame() zones = ["east","west","south","central"] data['zone']=np.hstack([[zone]*np.random.randint(10, 20) for zone in zones]) data['OS Usage']=np.random.random(data.shape[0]) # Now create a figure fig, axes = plt.subplots(1,4, figsize=(12,3)) # Now plot each zone on a particular axis for i, zone in enumerate(zones): data.loc[data.zone==zone].hist(column='OS Usage', bins=np.linspace(0,1,10), ax=axes[i], sharey=True) axes[i].set_title('OS Usage in {0}'.format(zone)) axes[i].set_xlabel('Value') axes[i].set_ylabel('Count') fig.tight_layout() fig.savefig(f'Barplots_using_for_loops_Pandas.png',dpi=300, format='png’, bbox_inches='tight') Histograms • Using for loop to create histograms in multiple subplots 222
  • 223. Ch Ch Ch Ch Ch Ch Ch DT #https://stackoverflow.com/questions/37106828/how-to-get-data-labels-on-a-seaborn-pointplot Soldier_years = [1850, 1851, 1852, 1853, 1854, 1855, 1856, 1857, 1858, 1859, 1860, 1861, 1862, 1863, 1864, 1865, 1866, 1867, 1868, 1869, 1870] num_records_yob = [7, 5, 8, 9, 15, 17, 23, 19, 52, 55, 73, 73, 107, 137, 65, 182, 228, 257, 477, 853, 2303] %matplotlib inline import seaborn as sns import matplotlib.pyplot as plt sns.set(style="darkgrid") f, (ax) = plt.subplots(figsize=(12, 6), sharex=True) sns.set_style("darkgrid") ax = sns.pointplot(x=Soldier_years, y=num_records_yob) [ax.text(p[0], p[1]+50, p[1], color='g') for p in zip(ax.get_xticks(), num_records_yob)] ax.figure.savefig(f'Datalabels_Seaborn_Pointplot.png',dpi=150, format='png', bbox_inches='tight') Seaborn point plot Adding data labels 223
  • 224. Ch Ch Ch Ch Ch Ch Ch DT #https://stackoverflow.com/questions/35788626/how-to-plot-a-heatmap-from-pandas-dataframe import numpy as np import matplotlib.pyplot as plt import seaborn as sns import pandas as pd # create some random data; replace that by your actual dataset data = pd.DataFrame(np.random.rand(11, 5), columns=['A', 'B', 'C', 'D', 'E'], index = range(2000, 2011, 1)) # plot heatmap ax = sns.heatmap(data.T) # turn the axis label for item in ax.get_yticklabels(): item.set_rotation(0) for item in ax.get_xticklabels(): item.set_rotation(90) # save figure ax.figure.savefig(f'Heatmap_Pandas_Dataframe.png',dpi=150, format='png', bbox_inches='tight') plt.show() Plotting Heat Map from Pandas Pandas + Seaborn + Matplotlib 224
  • 225. Ch Ch Ch Ch Ch Ch Ch DT #https://stackoverflow.com/users/190597/unutbu import matplotlib.pyplot as plt import numpy as np def make_image(data, outputname, size=(1, 1), dpi=80): fig = plt.figure() fig.set_size_inches(size) ax = plt.Axes(fig, [0., 0., 1., 1.]) ax.set_axis_off() fig.add_axes(ax) plt.set_cmap('hot') ax.imshow(data, aspect='equal') plt.savefig(outputname, dpi=dpi) # data = mpimg.imread(inputname)[:,:,0] data = np.arange(1,10).reshape((3, 3)) make_image(data, 'test2.png', dpi = 300) matplotlib.axes.Axes.imshow #https://stackoverflow.com/users/249341/hooked from numpy import random import matplotlib.pyplot as plt data = random.random((5,5)) img = plt.imshow(data, interpolation='nearest') img.set_cmap('hot') plt.axis('off') plt.savefig("test3.png", bbox_inches='tight', dpi = 300) #https://stackoverflow.com/questions/9295026/matplotlib -plots-removing-axis-legends-and-white-spaces?rq=1 fig = plt.figure(figsize=(5, 5)) fig.patch.set_visible(False) # turn off the patch plt.imshow([[0, 1], [0.5, 0.7]], interpolation="nearest", cmap='hot') plt.axis('off') plt.savefig("test.png", dpi = 300) 225
  • 226. Ch Ch Ch Ch Ch Ch Ch DT Plotting heat maps From Pandas Data frame and Correlation Matrix #https://stackoverflow.com/questions/55663030/ #plotting-two-heat-maps-side-by-side-in-matplotlib/ def corr_heatmaps(data1, data2, method='pearson'): # Basic Configuration fig, axes = plt.subplots(ncols=2, figsize=(12, 12)) ax1, ax2 = axes corr_matrix1 = data1.corr(method=method) corr_matrix2 = data2.corr(method=method) columns1 = corr_matrix1.columns columns2 = corr_matrix2.columns # Heat maps. im1 = ax1.matshow(corr_matrix1, cmap='coolwarm') im2 = ax2.matshow(corr_matrix2, cmap='coolwarm') # Formatting for heat map 1. ax1.set_xticks(range(len(columns1))) ax1.set_yticks(range(len(columns1))) ax1.set_xticklabels(columns1) ax1.set_yticklabels(columns1) ax1.set_title(data1.name, y=-0.1) plt.setp(ax1.get_xticklabels(), rotation=45, ha='left', rotation_mode='anchor') plt.colorbar(im1, fraction=0.045, pad=0.05, ax=ax1) # Formatting for heat map 2. ax2.set_xticks(range(len(columns2))) ax2.set_yticks(range(len(columns2))) ax2.set_xticklabels(columns2) ax2.set_yticklabels(columns2) ax2.set_title(data2.name, y=-0.1) plt.setp(ax2.get_xticklabels(), rotation=45, ha='left', rotation_mode='anchor') plt.colorbar(im2, fraction=0.045, pad=0.05, ax=ax2) fig.tight_layout() import numpy as np import pandas as pd import matplotlib.pyplot as plt df = pd.DataFrame(np.random.rand(10, 10)) fig, axes = plt.subplots(ncols=2, figsize=(8, 4)) ax1, ax2 = axes im1 = ax1.matshow(df.corr(), cmap = 'YlGnBu') im2 = ax2.matshow(df.corr(), cmap = 'YlGn') fig.colorbar(im1, ax=ax1) fig.colorbar(im2, ax=ax2) fig.savefig('plotting_two_adjacent_heatmaps.png’, dpi = 300, bbox_inches='tight') Back Creating User Defined Function Code End Code Start 226
  • 227. Ch Ch Ch Ch Ch Ch Ch DT Seaborn horizontal bar plot boxstyle: round roundingsize:.015 Seaborn horizontal bar plot boxstyle: round roundingsize : 2 Seaborn horizontal bar plot • Create spacing between bars • Adding dummy row in data Seaborn horizontal bar plot • Create spacing between bars • Resetting position of tick/ ticklabel/ Basic horizontal bar plot Basic horizontal bar plot Secondary Axis twinx Gridlines above bars horizontal bar plot seaborn style Only y gridlines horizontal bar plot seaborn style Only xgridlines Axes.set_belowaxis(True) horizontal bar plot default style Tick labels inside direction Padding for tick labels horizontal bar plot Secondary Axis twinx Gridlines below bars Connected dot plots default style Using Circle patches Note on Practical Implementation of tick related methods 227
  • 228. Ch Ch Ch Ch Ch Ch Ch DT plt.subplots(figsize=(6, 3)) sns.set_color_codes("pastel") ax = sns.barplot(x=df.Completion, y=df.index, orient='h', joinstyle='bevel') new_patches = [] for patch in reversed(ax.patches): bb = patch.get_bbox() color = patch.get_facecolor() p_bbox = FancyBboxPatch((bb.xmin, bb.ymin), abs(bb.width), abs(bb.height), boxstyle="round,pad=-0.0040,rounding_size=0.015", ec="none", fc=color, mutation_aspect=4 ) patch.remove() new_patches.append(p_bbox) for patch in new_patches: ax.add_patch(patch) sns.despine(left=True, bottom=True) ax.tick_params(axis=u'both', which=u'both', length=0) #Make ticklines zero length plt.tight_layout() ax.figure.savefig(f'Seaborn bar plot.png',dpi=150, format='png', bbox_inches='tight') plt.show() #https://stackoverflow.com/questions/61568935/seaborn-barplot-with-rounded- corners import pandas as pd import matplotlib.pyplot as plt import seaborn as sns from matplotlib.patches import FancyBboxPatch plt.style.use("seaborn") mydict = { 'Event': ['Running', 'Swimming', 'Biking', 'Hiking'], 'Completed': [2, 4, 3, 7], 'Participants': [10, 20, 35, 10]} df = pd.DataFrame(mydict).set_index('Event') df = df.assign(Completion=(df.Completed / df.Participants) * 100) print(df) Back to Bar Charts List 228
  • 229. Ch Ch Ch Ch Ch Ch Ch DT plt.subplots(figsize=(6, 3)) sns.set_color_codes("pastel") ax = sns.barplot(x=df.Completion, y=df.index, joinstyle='bevel') new_patches = [] for patch in reversed(ax.patches): # print(bb.xmin, bb.ymin,abs(bb.width), abs(bb.height)) bb = patch.get_bbox() color = patch.get_facecolor() p_bbox = FancyBboxPatch((bb.xmin, bb.ymin), abs(bb.width), abs(bb.height), boxstyle="round, pad=-0.0040, rounding_size=2", ec="none", fc=color, mutation_aspect=0.2 ) patch.remove() new_patches.append(p_bbox) for patch in new_patches: ax.add_patch(patch) sns.despine(left=True, bottom=True) #Removing all spines from plot ax.grid(b = True, axis = 'x') ax.tick_params(axis=u'both', which=u'both', length=0) #Make ticklines zero length plt.tight_layout() ax.figure.savefig(f'Seaborn_bar_plot_rounded_corners.png',dpi=300, format='png’, bbox_inches='tight') plt.show() #https://stackoverflow.com/questions/61568935/seaborn-barplot-with-rounded- corners import pandas as pd import matplotlib.pyplot as plt import seaborn as sns from matplotlib.patches import FancyBboxPatch plt.style.use("seaborn") mydict = { 'Event': ['Running', 'Swimming', 'Biking', 'Hiking'], 'Completed': [2, 4, 3, 7], 'Participants': [10, 20, 35, 10]} df = pd.DataFrame(mydict).set_index('Event') df = df.assign(Completion=(df.Completed / df.Participants) * 100) print(df) Back to Bar Charts List 229
  • 230. Ch Ch Ch Ch Ch Ch Ch DT # Hack of adding dummy row to create spacing between bars import pandas as pd import matplotlib.pyplot as plt import seaborn as sns mydict = { 'Event': ['Running', 'Swimming', 'Biking', '', 'Hiking', 'Jogging'], 'Completed': [2, 4, 3, 0, 7, 9], 'Participants': [10, 20, 35, 0, 10, 20]} df = pd.DataFrame(mydict).set_index('Event') df = df.assign(Completion=(df.Completed / df.Participants) * 100) plt.subplots(figsize=(5, 4)) print(df.index) ax = sns.barplot(x=df.Completion, y=df.index, color="orange", orient='h') plt.xticks(rotation=60). #Rotating x ticks by 60 plt.tight_layout() ax.figure.savefig('Matplotlib Spacing between bars_dummy data.png',dpi=150, bbox_inches='tight') plt.show() #https://stackoverflow.com/questions/61705783/matplotlib-control-spacing- between-bars #Matplotlib Control Spacing Between Bars Back to Bar Charts List 230
  • 231. Ch Ch Ch Ch Ch Ch Ch DT plt.style.use('seaborn’) #define a function to add space starting a specific label def add_space_after(ax, label_shift='', extra_space=0): bool_space = False label_list = [t.get_text() for t in ax.get_yticklabels()] # get postion of current ticks ticks_position = np.array(ax.get_yticks()).astype(float) # iterate over the boxes/ticks/tick label and change the y position for i, (patch, label) in enumerate(zip(ax.patches, ax.get_yticklabels())): # if the label to start the shift found if label.get_text()==label_shift: bool_space = True # reposition the boxes and the labels afterward if bool_space: patch.set_y(patch.get_y() + extra_space) ticks_position[i] += extra_space # in the case where the spacing is needed if bool_space: print(f'ytick positions after: {ax.get_yticks()} nytick labels after: {ax.get_yticklabels()}’) #ax.yaxis.set_major_locator(ticker.FixedLocator(ticks_position)) ax.set_yticks(ticks_position) ax.set_ylim([ax.get_ylim()[0]+extra_space, ax.get_ylim()[1]]) print(f'ytick positions after: {ax.get_yticks()} nytick labels after: {ax.get_yticklabels()}’) ax.yaxis.set_major_formatter(ticker.FixedFormatter(label_list)) fig, ax = plt.subplots(figsize = (7,4)) sns.barplot(x=df.Completion, y=df.index, color="orange", ax = ax, orient='h' ) plt.xticks(rotation=60) #Rotating x ticks by 60 plt.tight_layout() #use the function add_space_after(ax, 'Hiking', 0.6) ax.figure.savefig(f'Matplotlib Spacing between bars.png',dpi=150, bbox_inches='tight') #https://stackoverflow.com/questions/61705783/matplotlib-control-spacing- between-bars #Matplotlib Control Spacing Between Bars import pandas as pd import matplotlib.pyplot as plt import seaborn as sns import matplotlib.ticker as ticker mydict = { 'Event': ['Running', 'Swimming', 'Biking', 'Hiking', 'Jogging'], 'Completed': [2, 4, 3, 7, 9], 'Participants': [10, 20, 35, 10, 20]} df = pd.DataFrame(mydict).set_index('Event') df = df.assign(Completion=(df.Completed / df.Participants) * 100) #note: no more blank row Back to Bar Charts List 231
  • 232. Ch Ch Ch Ch Ch Ch Ch DT #https://stackoverflow.com/questions/49335512/ticklabels-inside-a-plot-in-matplotlib #https://stackoverflow.com/questions/14800973/matplotlib-tick-labels-position-relative-to-axes #https://matplotlib.org/stable/gallery/lines_bars_and_markers/barh.html plt.rcdefaults() fig, ax = plt.subplots() # Example data people = ('Tom', 'Dick', 'Harry', 'Slim', 'Jim') y_pos = np.arange(len(people)) performance = 3 + 10 * np.random.rand(len(people)) error = np.random.rand(len(people)) ax.barh(y_pos, performance, xerr=error, align='center’, color='green', ecolor='black') ax.set_yticks(y_pos) ax.set_yticklabels(people) ax.invert_yaxis() # labels read top-to-bottom ax.set_xlabel('Performance') ax.set_title('How fast do you want to go today?') fig.savefig('BasicHorizontalBarplot.png', dpi = 300, bbox_inches='tight') plt.show() Back to Bar Charts List 232
  • 233. Ch Ch Ch Ch Ch Ch Ch DT #https://stackoverflow.com/questions/49335512/ticklabels-inside-a-plot-in-matplotlib #https://stackoverflow.com/questions/14800973/matplotlib-tick-labels-position-relative-to-axes #https://matplotlib.org/stable/gallery/lines_bars_and_markers/barh.html import matplotlib.pyplot as plt import pandas as pd import numpy as np np.random.seed(123456) # Fixing random state for reproducibility plt.style.use('seaborn') fig, ax = plt.subplots(figsize = (6,4)) # Example data people = ('Tom', 'Dick', 'Harry', 'Slim', 'Jim') y_pos = np.arange(len(people)) performance = 3 + 10 * np.random.rand(len(people)) error = np.random.rand(len(people)) ax.barh(y_pos, performance, xerr=error, align='center', color='green',alpha = 0.8, ecolor='black') ax.set_yticks(y_pos) ax.set_yticklabels(people, ha = 'right') ax.invert_yaxis() # labels read top-to-bottom ax.set_xlabel('Performance') ax.set_title('How fast do you want to go today?’) [tkl.set(fontweight = 'bold') for tkl in ax.get_yticklabels()] #Making tick labels bold ax.tick_params(axis = 'both', labelcolor = 'black', labelsize = 10) #Setting ticklabel size and color ax.grid(axis = 'y', b = True). #Setting on gridlines from x axis ax.grid(axis = 'x', b = False) #Setting off gridlines from x axis fig.savefig('SeabornHorizontalBarplot_textbold.png', dpi = 300, bbox_inches='tight') plt.show() Back to Bar Charts List Note on Practical Implementation of tick related methods 233
  • 234. Ch Ch Ch Ch Ch Ch Ch DT #https://stackoverflow.com/questions/49335512/ticklabels-inside-a-plot-in-matplotlib #https://stackoverflow.com/questions/14800973/matplotlib-tick-labels-position-relative-to-axes #https://matplotlib.org/stable/gallery/lines_bars_and_markers/barh.html import matplotlib.pyplot as plt import pandas as pd import numpy as np np.random.seed(123456) # Fixing random state for reproducibility plt.style.use('seaborn') fig, ax = plt.subplots(figsize = (6,4)) # Example data people = ('Tom', 'Dick', 'Harry', 'Slim', 'Jim') y_pos = np.arange(len(people)) performance = 3 + 10 * np.random.rand(len(people)) error = np.random.rand(len(people)) ax.barh(y_pos, performance, xerr=error, align='center’, color='green',alpha = 0.8, ecolor='black') ax.set_yticks(y_pos) ax.set_yticklabels(people, ha = 'right') ax.invert_yaxis() # labels read top-to-bottom ax.set_xlabel('Performance') ax.set_title('How fast do you want to go today?') [tkl.set(fontweight = 'semibold') for tkl in ax.get_yticklabels()] #Making ticklabels semi bold ax.tick_params(axis = 'y', labelcolor = 'black’) #Setting ticklabel size and color ax.grid(axis = 'y', b = False) stylize_axes(ax). # Using the user defined function stylize_axes fig.savefig('SeabornHorizontalBarplot_textbold.png', dpi = 300, bbox_inches='tight') plt.show() #Creating a User Defined function def stylize_axes(ax): ax.set_facecolor('.8') ax.tick_params(labelsize=10, length=0) ax.grid(True, axis='x', color='white') ax.set_axisbelow(True) #Ticks and gridlines below other artists [spine.set_visible(False) for spine in ax.spines.values()] Back to Bar Charts List 234
  • 235. Ch Ch Ch Ch Ch Ch Ch DT #https://stackoverflow.com/questions/49335512/ticklabels-inside-a-plot-in-matplotlib #https://stackoverflow.com/questions/14800973/matplotlib-tick-labels-position-relative-to-axes #https://matplotlib.org/stable/gallery/lines_bars_and_markers/barh.html import matplotlib.pyplot as plt import pandas as pd import numpy as np np.random.seed(123456) plt.rcdefaults() fig, ax = plt.subplots(figsize = (6,4)) # Example data people = ('Tom', 'Dick', 'Harry', 'Slim', 'Jim') y_pos = np.arange(len(people)) performance = 3 + 10 * np.random.rand(len(people)) error = np.random.rand(len(people)) plt.rcParams["axes.axisbelow"] = 'line’ #tick and gridlines above other artists except lines #Can use the below line as well instead of above #ax.set_axisbelow(False) ax.barh(y_pos, performance, xerr=error, align='center’, color='green', ecolor='black’) ax.invert_yaxis() # labels read top-to-bottom ax.set_xlabel('Performance') ax.set_title('How fast do you want to go today?’) ax.set_yticks(y_pos) ax.set_yticklabels(people, horizontalalignment = 'left', fontweight = 'medium’) #ha is important ax.tick_params(axis = 'y', pad = -20, labelcolor = 'white’) #pad as -20 for ticklabels inside plt.show() • https://matplotlib.org/stable/gallery/misc/zorder_demo.html?highli ght=zorder • https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.set _axisbelow.html?highlight=axisbelow#matplotlib.axes.Axes.set_axi sbelow • https://stackoverflow.com/questions/48327099/is-it-possible-to-set- higher-z-index-of-the-axis-labels Axes.set_axisbelow(self, b) : Set whether axis ticks and gridlines are above or below most artists. This controls the zorder of the ticks and gridlines. Back to Bar Charts List 235
  • 236. Ch Ch Ch Ch Ch Ch Ch DT #https://stackoverflow.com/questions/52171321/ # creating-alternative-y-axis-labels-for-a-horizontal-bar-plot-with-matplotlib/52171704 #Check answer by Sheldore import matplotlib.pyplot as plt plt.rcdefaults() fig, ax = plt.subplots(figsize = (6,4)) y = ['Physics','Chemistry','Economics','Philosophy','Computer Science','Sociology','Biology','History','Overall'] x = [0.0,33.333333333333336,50.0,50.0,50.0,54.54545454545455,58.333333333333336,100.0, 51.851851851851855] alternativeYlabels = ['54', '1', '3', '12', '12', '2', '11', '12', '1'] plt.barh(y,x) plt.title('Percent Completion Rate of Each Course') ax = plt.gca() ax.xaxis.grid(True) # adding vertical grid lines ax.set_axisbelow(‘line’) #Setting Ticks and gridlines above all Artists except lines # Creating the right hand side y axis ax2 = ax.twinx() ax_lim = ax.get_ylim() ax2_lim = (ax_lim[0], ax_lim[1]) # Aligning the limits of both y axes ax2.set_ylim(ax2_lim) ax2.set_yticks(range(0, len(y))) ax2.set_yticklabels(alternativeYlabels[::-1]) # Reverses the list ax2.set_ylabel("# enrolled in each course",rotation = 270, labelpad=15) fig.savefig('SecondaryAxisBarplot_2.png', dpi = 300, bbox_inches='tight') plt.show() Creating alternative y axis labels for a horizontal bar plot with matplotlib • Axes.twinx • (grid lines above bars) Back to Bar Charts List 236
  • 237. Ch Ch Ch Ch Ch Ch Ch DT #https://stackoverflow.com/questions/52171321/ #creating-alternative-y-axis-labels-for-a-horizontal-bar-plot-with-matplotlib/52171704 #Check answer by Sheldore import matplotlib.pyplot as plt plt.rcdefaults() fig, ax = plt.subplots(figsize = (6,4)) y = ['Physics','Chemistry','Economics','Philosophy','Computer Science','Sociology','Biology','History','Overall'] x = [0.0,33.333333333333336,50.0,50.0,50.0,54.54545454545455,58.333333333333336,100.0, 51.851851851851855] alternativeYlabels = ['54', '1', '3', '12', '12', '2', '11', '12', '1'] plt.barh(y,x) plt.title('Percent Completion Rate of Each Course') ax = plt.gca() ax.xaxis.grid(True) # adding vertical grid lines ax.yaxis.grid(True, alpha = 0.5) # adding horizontal grid lines ax.set_axisbelow(True) #Setting Ticks and gridlines below all Artists. # Creating the right hand side y axis ax2 = ax.twinx() ax_lim = ax.get_ylim() ax2_lim = (ax_lim[0], ax_lim[1]) # Aligning the limits of both y axes ax2.set_ylim(ax2_lim) ax2.set_yticks(range(0, len(y))) ax2.set_yticklabels(alternativeYlabels[::-1]) # Reverses the list ax2.set_ylabel("# enrolled in each course",rotation = 270, labelpad=15) fig.savefig('SecondaryAxisBarplot_2.png', dpi = 300, bbox_inches='tight') plt.show() Creating alternative y axis labels for a horizontal bar plot with matplotlib • Axes.twinx • (grid lines below bars) Back to Bar Charts List 237
  • 238. Ch Ch Ch Ch Ch Ch Ch DT #https://www.reddit.com/r/matplotlib/comments/i3t5uv/any_example_code_or_tips_for_h ow_to_make_this/ import matplotlib.pyplot as plt import numpy as np import matplotlib.patches as mpatches # Fixing random state for reproducibility np.random.seed(19680801) plt.rcdefaults() fig, ax = plt.subplots() # Example data people = ('Tom', 'Dick', 'Harry', 'Slim', 'Jim','John') y_pos = np.arange(len(people)) performance = 3 + 10 * np.random.rand(len(people)) error = np.random.rand(len(people)) start = 3+np.random.rand(len(people)) ax.barh(y_pos, performance, left=start, height=0.03, align='center') ax.set_yticks(y_pos) ax.set_yticklabels(people) ax.invert_yaxis() # labels read top-to-bottom ax.set_xlabel('Performance') ax.set_title('How fast do you want to go today?') ax.xaxis.grid(b=True, alpha = 0.6) ax.set_axisbelow(True). #Tick and Gridlines below other artists for x,y,x1 in zip(start, y_pos, performance): circ = mpatches.Circle((x,y),0.2,fc='red’) #Adding red circles for start position ax.add_patch(circ) circ = mpatches.Circle((x+x1,y),0.2,fc='blue’) #Adding blue circles for start position ax.add_patch(circ) plt.axis('scaled') fig.savefig('Different Styles_Dot_Plot.png', dpi = 300, format = 'png',bbox_inches='tight') plt.show() Using seaborn plot style, code line : plt.style.use(‘seaborn’) plt.rcdefaults() Cleveland Dot plots Using Circle Patches Back to Bar Charts List 238
  • 239. Ch Ch Ch Ch Ch Ch Ch DT Cleveland Dot plots Using Circle Patches Multiple Plot Styles 239
  • 240. Ch Ch Ch Ch Ch Ch Ch DT # Example data people = ('Tom', 'Dick', 'Harry', 'Slim', 'Jim','John') y_pos = np.arange(len(people)) performance = 3 + 10 * np.random.rand(len(people)) error = np.random.rand(len(people)) start = 3+np.random.rand(len(people)) fig = plt.figure(figsize = (25,9)) for i,ax in enumerate(styles): with plt.style.context(styles[i]): print(styles[i]) ax = fig.add_subplot(2,3,i+1) ax.barh(y_pos, performance, left=start, height=0.03, align='center') ax.set_yticks(y_pos) ax.set_yticklabels(people,**font_tick) #Setting yticklabel props using ** unpacking [tkl.set(**font_tick) for tkl in ax.get_xticklabels()] #Setting xticklabel props using ** unpacking ax.invert_yaxis() # labels read top-to-bottom ax.set_xlabel('Performance', **font_text) ax.set_title('How fast do you want to go today?', loc = 'left', fontdict = font_title) #ax.grid(b=True) for x,y,x1 in zip(start, y_pos, performance): #Adding Circle Patches circ = Circle((x,y),0.2,fc='red', transform = ax.transData) ax.add_patch(circ) circ = Circle((x+x1,y),0.2,fc='blue',transform = ax.transData) ax.add_patch(circ) ax.axis('scaled') #Adding name of the plot style for the subplot within fancy bbox ax.text(0.99,1.05,s = f'{styles[i]}',ha = 'right',transform = ax.transAxes, **font_text, bbox=dict(facecolor='lightyellow', edgecolor='green', boxstyle='round',pad =.3)) plt.subplots_adjust(wspace = .2, hspace = .1) #Adjust horizontal & vertical space btw subplots fig.savefig('Different Styles_Dot_Plot_fancybox_text.png', dpi = 300, bbox_inches='tight') #https://www.reddit.com/r/matplotlib/comments/i3t5uv/any_example_cod e_or_tips_for_how_to_make_this/ import matplotlib.pyplot as plt from matplotlib.patches import Circle import numpy as np # Fixing random state for reproducibility np.random.seed(19680801) #Listing the styles for plotting with for loop styles = ['bmh', 'Solarize_Light2','seaborn', 'fast', 'seaborn-darkgrid','seaborn-whitegrid’] #Creating font dicts for different use cases font_text = {'family': 'EB Garamond', 'color': 'darkred', 'weight': 'normal’,'size': 16} #for label plot style font_title = {'family': 'Dejavu Sans', 'color': 'darkred', 'weight': 'normal','size': 14} #for axes title font_tick = {'family': 'Dejavu Sans', 'color': 'darkred’, 'weight': 'normal’, 'size': 12} #for tick labels 240
  • 241. Ch Ch Ch Ch Ch Ch Ch DT # libraries import numpy as np import pandas as pd import matplotlib.pyplot as plt plt.rcdefaults() # Create a dataframe value1=np.random.uniform(size=20) value2=value1+np.random.uniform(size=20)/4 df = pd.DataFrame({'group':list(map(chr, range(65, 85))), 'value1':value1 , 'value2':value2 }) # Reorder it following the values of the first value: ordered_df = df.sort_values(by='value1') my_range=range(1,len(df.index)+1) fig, ax = plt.subplots(figsize = (8,6)) # The horizontal plot is made using the hline function ax.hlines(y=my_range, xmin=ordered_df['value1'], xmax=ordered_df['value2'], color='grey’, alpha=0.4) ax.scatter(ordered_df['value1'], my_range, color='skyblue', alpha=1, label='value1', s = 90) ax.scatter(ordered_df['value2'], my_range, color='green', alpha=0.4 , label='value2', s= 90) ax.legend() # Add title and axis names ax.set_yticks(my_range) ax.set_yticklabels(ordered_df['group']) ax.set_title("Comparison of the value 1 and the value 2", loc='left') ax.set_xlabel('Value of the variables') ax.set_ylabel('Group') # Configuring the grid lines ax.grid(True, alpha = 0.6, ls = 'dashed') ax.set_axisbelow(True) fig.savefig('lollipop_charts.png', dpi = 300, format = 'png',bbox_inches='tight') plt.show() #Show the graph #https://www.python-graph-gallery.com/184-lollipop-plot-with-2-groups Changes done in the code in link : • Changed pyplot interface style to OO style • Configured the grid lines to dashed line style, set_axisbelow to put ticks/gridlines below other artists • Changed the scatter marker size to 90 Cleveland Dot plots • Using Axes Methods • Axes.Scatter and Axes.hlines 241
  • 242. Ch Ch Ch Ch Ch Ch Ch DT Using matplotlib in Python Cleveland Dot plots 242
  • 243. Ch Ch Ch Ch Ch Ch Ch DT # libraries import numpy as np import pandas as pd import matplotlib.pyplot as plt import matplotlib.transforms as transforms import seaborn as sns import io sns.set(style="whitegrid") # set style #plt.style.use('seaborn-whitegrid') data = io.StringIO(""""Country" 1990 2015 "Russia" 71.5 101.4 "Canada" 74.4 102.9 "Other non-OECD Europe/Eurasia" 60.9 135.2 "South Korea" 127.0 136.2 "China" 58.5 137.1 "Middle East" 170.9 158.8 "United States" 106.8 169.0 "Australia/New Zealand" 123.6 170.9 "Brazil" 208.5 199.8 "Japan" 181.0 216.7 "Africa" 185.4 222.0 "Other non-OECD Asia" 202.7 236.0 "OECD Europe" 173.8 239.9 "Other non-OECD Americas" 193.1 242.3 "India" 173.8 260.6 "Mexico/Chile" 221.1 269.8""") df = pd.read_csv(data, sep="s+", quotechar='"') df = df.set_index("Country").sort_values("2015") df["change"] = df["2015"] / df["1990"] - 1 font_suptitle = {'fontsize' : 16} #https://stats.stackexchange.com/questions/423735/what-is-the-name-of- this-plot-that-has-rows-with-two-connected-dots/423861 #http://thewhyaxis.info/gap-remake/ #Dot plots in Excel #https://www.eia.gov/outlooks/ieo/pdf/0484%282016%29.pdf Cleveland Dot plots • Using Axes Methods • Axes.Scatter and Axes.hlines Code snippet 1 of 2 Code continues to Next slide Code Start 243
  • 244. Ch Ch Ch Ch Ch Ch Ch DT fig = plt.figure(figsize=(13,5)) ax = fig.add_subplot() y_range = np.arange(1, len(df.index) + 1) colors = np.where(df['2015'] > df['1990'], '#d9d9d9', '#d57883') ax.hlines(y=y_range, xmin=df['1990'], xmax=df['2015'], color=colors, lw=5) ax.scatter(df['1990'], y_range, color='#0096d7', s=150, label='1990', zorder=3) ax.scatter(df['2015'], y_range, color='#003953', s=150 , label='2015', zorder=3) for (_, row), y in zip(df.iterrows(), y_range): ax.annotate(f"{row['change']:+.0%}", (max(row["1990"], row["2015"]) + 6, y - 0.25)) ax.grid(True,alpha = 0.6) #Make grid lines lighter ax.set_axisbelow(False) # Make all spines visible ax.spines['right'].set(visible = True) ax.spines['top'].set(visible =True) ax.spines['left'].set(visible =True) ax.spines['right'].set(visible =True) ax.set_yticks(y_range) ax.set_yticklabels(df.index) ax.legend(ncol=2, bbox_to_anchor=(1, 1), loc="lower right", frameon=True ) # the x coords of this transformation are data, and the y coord are figure trans = transforms.blended_transform_factory(ax.transData, fig.transFigure) fig.suptitle("Energy productivity in selected countries and regions,1990 and 2015", **font_suptitle, x = 50,y = 1, transform = trans, ha = 'left', va = 'top') ax.set_title("Billion dollars GDP per quadrillion BTU",loc = 'left') ax.set_xlim(50, 300) fig.subplots_adjust(left=0.35, top = 0.87) fig.savefig('connected dot plots_sample2.png', dpi = 300, format = 'png’, bbox_inches='tight') plt.show() Cleveland Dot plots • Using Axes Methods • Axes.Scatter and Axes.hlines Code snippet 2 of 2 Code End #https://stats.stackexchange.com/questions/423735/what-is-the-name-of- this-plot-that-has-rows-with-two-connected-dots/423861 #http://thewhyaxis.info/gap-remake/ #Dot plots in Excel #https://www.eia.gov/outlooks/ieo/pdf/0484%282016%29.pdf Code continues from Prev slide 244
  • 245. Ch Ch Ch Ch Ch Ch Ch DT Using ggplot2 in R Cleveland Dot plots 245
  • 246. Ch Ch Ch Ch Ch Ch Ch DT # create the data frame # (in wide format, as needed for the line segments): dat_wide = tibble::tribble( ~Country, ~Y1990, ~Y2015, 'Russia', 71.5, 101.4, 'Canada', 74.4, 102.9, 'Other non-OECD Europe/Eurasia', 60.9, 135.2, 'South Korea', 127, 136.2, 'China', 58.5, 137.1, 'Middle East', 170.9, 158.8, 'United States', 106.8, 169, 'Australia/New Zealand', 123.6, 170.9, 'Brazil', 208.5, 199.8, 'Japan', 181, 216.7, 'Africa', 185.4, 222, 'Other non-OECD Asia', 202.7, 236, 'OECD Europe', 173.8, 239.9, 'Other non-OECD Americas', 193.1, 242.3, 'India', 173.8, 260.6, 'Mexico/Chile', 221.1, 269.8 ) # a version reshaped to long format (for the points): dat_long = dat_wide %>% gather(key = 'Year', value = 'Energy_productivity', Y1990:Y2015) %>% mutate(Year = str_replace(Year, 'Y', ’’)) # create the graph: ggplot() + geom_segment(data = dat_wide, aes(x = Y1990, xend = Y2015, y = reorder(Country, Y2015), yend = reorder(Country, Y2015)), size = 3, colour = '#D0D0D0') + geom_point(data = dat_long, aes(x = Energy_productivity, y = Country, colour = Year), size = 4) + labs(title = 'Energy productivity in selected countries nand regions', subtitle = 'Billion dollars GDP per quadrillion BTU', caption = 'Source: EIA, 2016', x = NULL, y = NULL) + scale_colour_manual(values = c('#1082CD', '#042B41')) + theme_bw() + theme(legend.position = c(0.92, 0.20), legend.title = element_blank(), legend.box.background = element_rect(colour = 'black'), panel.border = element_blank(), axis.ticks = element_line(colour = '#E6E6E6’)) ggsave('energy.png', width = 20, height = 10, units = 'cm') #https://stats.stackexchange.com/questions/423735/what-is-the-name-of- this-plot-that-has-rows-with-two-connected-dots/423861 library(dplyr) # for data manipulation library(tidyr) # for reshaping the data frame library(stringr) # string manipulation library(ggplot2) # graphing Cleveland Dot plots • Using ggplot2 in R 246
  • 247. Ch Ch Ch Ch Ch Ch Ch DT #https://stackoverflow.com/questions/61527817/ #formatting-date-labels-using-seaborn-facetgrid #Check answer by Diziet Asahi import pandas as pd import numpy as np import seaborn as sns import matplotlib.pyplot as plt from datetime import datetime import matplotlib.dates as mdates import random datelist = pd.date_range(start="march 1 2020", end="may 20 2020", freq="w").tolist() varlist = ["x", "y", "z", "x", "y", "z", "x", "y", "z", "x", "y", "z"] deptlist = ["a", "a", "b", "a", "a", "b", "a", "a", "b", "a", "a", "b"] vallist = random.sample(range(10, 30), 12) df = pd.DataFrame({'date': datelist, 'value': vallist, 'variable': varlist, 'department': deptlist}) g = sns.FacetGrid(df, row="department", col="variable", sharey='row') g = g.map(plt.plot, "date", "value", marker='o', markersize=0.7) xformatter = mdates.DateFormatter("%m/%d") g.axes[0,0].xaxis.set_major_formatter(xformatter) g.axes[0,0].figure.savefig(f’Date labels Seaborn plot.png',dpi=300, format='png’, bbox_inches='tight') plt.show() Seaborn Facet Grids • pd.date_range() to generate datetimeindex • DateFormatter for formatting xtick labels Note on Practical Implementation of tick related methods 247
  • 248. Ch Ch Ch Ch Ch Ch Ch DT #https://stackoverflow.com/questions/12945971/ #pandas-timeseries-plot-setting-x-axis-major-and-minor-ticks-and-labels? #Check answer by bmu import numpy as np import pandas as pd import matplotlib.pyplot as plt import matplotlib.dates as mdates plt.style.use('seaborn') idx = pd.date_range('2011-05-01', '2011-07-01') prng = np.random.default_rng(123456) s = pd.Series(prng.standard_normal(len(idx)), index=idx) fig, ax = plt.subplots() ax.plot_date(idx.to_pydatetime(), s, 'v-') #x values converted to python datetime objects ax.xaxis.set_minor_locator(mdates.WeekdayLocator(byweekday=(1),interval=1)) ax.xaxis.set_minor_formatter(mdates.DateFormatter('%dn%a')) ax.xaxis.grid(True, which="minor") ax.yaxis.grid() ax.xaxis.set_major_locator(mdates.MonthLocator()) ax.xaxis.set_major_formatter(mdates.DateFormatter('nnn%bn%Y')) plt.tight_layout() fig.savefig('Major and Minor date ticks in multiline.png',dpi=300, format='png’, bbox_inches='tight') dates module • pd.date_range() to generate datetime index • MonthLocator() for major ticks • WeekdayLocator() for minor ticks • Date Formatter Note on Practical Implementation of tick related methods 248
  • 249. Ch Ch Ch Ch Ch Ch Ch DT import matplotlib.pyplot as plt from matplotlib.dates import MonthLocator, YearLocator import numpy as np plt.rcdefaults() fig, ax = plt.subplots(1, 1, figsize = (10,5)) #Using np.arange to create a range of datetime64 values x = np.arange('2010-01', '2021-07', dtype='datetime64[D]') # np.random.seed(12345) #y = np.random.randn(x.size).cumsum() # Using the generator np.random.default_rng to generate values from standard normal rng = np.random.default_rng(12345) y = rng.standard_normal(x.size).cumsum() ax.plot(x, y) yloc = YearLocator() mloc = MonthLocator() ax.xaxis.set_major_locator(yloc) ax.xaxis.set_minor_locator(mloc) ax.xaxis.set_tick_params(rotation = 30) [tkl.set_ha('right') for tkl in ax.get_xticklabels()] ax.grid(True) savekargs = dict(dpi = 300, transparent = True, pad_inches = 0.5, bbox_inches = 'tight') fig.savefig('Numpy_datetime64_plotting.png', **savekargs) Numpy datetime64 • Using np.arange to generate array of datetime64[D] values • Using generator np.random.default_range • YearLocator and MonthLocator Classes Note on Practical Implementation of tick related methods 249
  • 250. Ch Ch Ch Ch Ch Ch Ch DT #https://stackoverflow.com/questions/48790378/how-to-get-ticks-every-hour import pandas as pd import numpy as np import matplotlib.pyplot as plt import matplotlib.dates as mdates style = ['fivethirtyeight', 'seaborn', 'ggplot', 'default'] plt.style.use(style[3]) idx = pd.date_range('2017-01-01 05:03', '2017-01-01 18:03', freq = 'min') df = pd.Series(np.random.randn(len(idx)), index = idx) fig, ax = plt.subplots(figsize = (10,5)) hours = mdates.HourLocator(interval = 1) h_fmt = mdates.DateFormatter('%H:%M:%S') ax.plot(df.index, df.values, color = 'black', linewidth = 0.4) #or use #df.plot(ax = ax, color = 'black', linewidth = 0.4, x_compat=True, figsize = (10,5)) #Then tick and format with matplotlib: ax.xaxis.set_major_locator(hours) ax.xaxis.set_major_formatter(h_fmt) fig.autofmt_xdate() savekargs = dict(dpi = 300, transparent = True, pad_inches = 0.5, bbox_inches = 'tight') fig.savefig('datetimeindex_plotting.png', **savekargs) plt.show() Datetime index Pandas Tseries Plotting • Suppressing Tick Resolution Adjustment by Pandas plotting • x_compat = True to apply matplotlib.dates locator/formatter • Ticks at 1 hour interval Note on Practical Implementation of tick related methods 250
  • 251. Ch Ch Ch Ch Ch Ch Ch DT #https://stackoverflow.com/questions/38154489/ #force-pandas-xaxis-datetime-index-using-a-specific-format import pandas as pd from datetime import datetime import numpy as np import matplotlib.pyplot as plt import matplotlib.dates as mdates timeInd = pd.date_range(start = datetime(2016,4,17,23,0,0), end = datetime(2016,4,20,1,0,0), freq = 'H') d = {'data1': np.random.randn(len(timeInd)), 'data2': np.random.randn(len(timeInd)), 'data3': np.random.randn(len(timeInd))} df = pd.DataFrame(data = d, index = timeInd) plt.style.use('ggplot') ax0 = df.plot(subplots=True, grid=True, x_compat=True, figsize = (10,6)) ax = plt.gca() # set major ticks location every day ax.xaxis.set_major_locator(mdates.DayLocator()) # set major ticks format ax.xaxis.set_major_formatter(mdates.DateFormatter('nnn%d.%m.%Y')) # set minor ticks location every two hours ax.xaxis.set_minor_locator(mdates.HourLocator(interval=2)) # set minor ticks format ax.xaxis.set_minor_formatter(mdates.DateFormatter('%H:%M:%S')) for ax in ax0: ax.legend(loc = 'upper right') ax.figure.patch.set(visible = False) # or just set together date and time for major ticks like # ax.xaxis.set_major_formatter(mdates.DateFormatter('%d.%m.%Y %H:%M:%S')) ax.figure.savefig('Pandasplotting_datetime_xcompat.png', dpi = 300, pad_inches = 0.02) plt.show() Pandas Tseries Plotting • Suppressing Tick Resolution Adjustment by Pandas plotting • x_compat = True to apply matplotlib.dates locator/formatter • Ticks at 2 hour interval Note on Practical Implementation of tick related methods 251
  • 252. Ch Ch Ch Ch Ch Ch Ch DT import matplotlib.dates as mdates import matplotlib.pyplot as plt import datetime as dt fig,ax1 = plt.subplots() ax1.plot(df.index,df.values) ax1.xaxis.set_major_locator(mdates.MonthLocator()) monthyearFmt = mdates.DateFormatter('%Yn%b') ax1.xaxis.set_major_formatter(monthyearFmt) #Underscore and assignment so that tick locs and labels are not shown in output _ = plt.xticks(ha = 'center’) plt.grid(axis = 'x') x1 = dt.datetime(2020,0o5,0o1) x2 = dt.datetime(2020,0o7,0o1) ax1.set_ylim(0, ax1.get_ylim()[1]) ax1.fill_betweenx(y = (ax1.get_ylim()[0],ax1.get_ylim()[1]), x1 = x1, x2=x2, alpha = 0.3, color = 'steelblue' ) x3 = dt.datetime(2020,9,0o1) x4 = dt.datetime(2020,0o10,0o1) ax1.axvspan(xmin = x3, xmax = x4, color = 'salmon', alpha = 0.8) fig.savefig('Simple TimeSeries plot.png', dpi = 300, transparent = True) TimeSeries Plotting • pd.date_range() • dt.datetime • np.random.default_rng(12345) • fill_betweenx() • Axvspan() • MonthLocator() Note on Practical Implementation of tick related methods rng = np.random.default_rng(12345) dtindex=pd.date_range(start='2020-01-01',end='2020-12-31',freq='M') df = pd.DataFrame({'values':rng.integers(0,1000, len(dtindex))}, index= dtindex) 252
  • 253. Ch Ch Ch Ch Ch Ch Ch DT import matplotlib.pyplot as plt import matplotlib.dates as mdates import numpy as np # create reproducible dataframe dates = pd.date_range(start='2014-04-01', end='2021-04-07', freq='10min').tolist() rng = np.random.default_rng(123456) utilization = [rng.integers(10, 50) for _ in range(len(dates))] df = pd.DataFrame({'dates': dates, 'util': utilization}) df.set_index('dates', inplace=True) # resample dataframe to daily df_daily = pd.DataFrame(df['util'].resample('D').sum()) #plot the dataframe plt.style.use('seaborn-whitegrid') fig = plt.figure(figsize=(10, 6)) ax = fig.add_subplot(111) ax.plot(df_daily.index, df_daily['util']) ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m')) fig.savefig('Resampling.png', dpi = 300, transparent = True) plt.show() Datetime index Time series Plotting • Resampling the dataframe to Day frequency Note on Practical Implementation of tick related methods https://pandas.pydata.org/docs/user_guide/timeseries.html#resampling 253
  • 254. Ch Ch Ch Ch Ch Ch Ch DT import pandas as pd import numpy as np import matplotlib.pyplot as plt import matplotlib as mpl import seaborn as sns np.random.seed(123456) perf = np.cumprod(1+np.random.normal(size=250)/100) df = pd.DataFrame({'underwater': perf/np.maximum.accumulate(perf)-1}) sns.set_context('notebook') sns.set_style('ticks', rc = {'axes.grid': True, 'axes.spines.left': True, 'axes.spines.right': False, 'axes.spines.bottom': True, 'axes.spines.top': False}) #Assigning current axes to ax ax = plt.gca() df.plot.area(ax=ax, label = 'underwater') ax.xaxis.set_ticklabels([]) ax.set_ylim(ax.get_ylim()[0],0) #Removing spines at bottom and right sns.despine(left=False, bottom=True, right=True, top = False, ax=ax) #Adding legend with frameon ax.legend(loc = 'upper right', bbox_to_anchor = (0.95,0.95), bbox_transform = ax.transAxes, frameon = True ) # Adding red ticks in x axis pointing outwards ax.tick_params('x', length=8, width=2, color='red', which='major', direction='out') ax.figure.savefig('Major_ticks_Outer_No_AxisLabel.png',dpi=300, format='png', bbox_inches='tight') • sns.despine • Adding red ticks in out direction #https://stackoverflow.com/questions/57097467/ #Check answer by Sheldore #Matplotlib x-axis at the top, no x axis label, no major tick labels, but outer major and minor tick wanted Note on Practical Implementation of tick related methods Pandas Seaborn Matplotlib 254
  • 255. Ch Ch Ch Ch Ch Ch Ch DT #https://stackoverflow.com/questions/41924963/formatting-only-selected-tick-labels import matplotlib.pyplot as plt plt.rcdefaults() fig, ax = plt.subplots() ax.plot([1, 2, 3, 4, 5], [50, 40, 60, 70, 50]) ax.get_xticklabels()[-2].set_color("white") ax.get_xticklabels()[-2].set_fontsize(14) ax.get_xticklabels()[-2].set_weight("bold") ax.get_xticklabels()[-2].set_bbox(dict(facecolor="red", alpha=0.9)) fig.savefig('Highlight selected ticks.png', dpi = 300, format = 'png',bbox_inches='tight’) plt.show() Format select few tick labels • Indexing/slicing axis tick labels • Setting the bbox for text artist matplotlib.patches.FancyBboxPatch FancyBboxPatch Demo Class Name Attrs Circle circle pad=0.3 DArrow darrow pad=0.3 LArrow larrow pad=0.3 RArrow rarrow pad=0.3 Round round pad=0.3, rounding_size=None Round4 round4 pad=0.3, rounding_size=None Roundtooth roundtooth pad=0.3, tooth_size=None Sawtooth sawtooth pad=0.3, tooth_size=None Square square pad=0.3 boxstyle types Note on Practical Implementation of tick related methods 255
  • 256. Ch Ch Ch Ch Ch Ch Ch DT #https://stackoverflow.com/questions/41924963/formatting-only-selected-tick-labels #The above link contains only one tick formatted. The example is extended here for different possible use-cases plt.rcdefaults() fig, ax = plt.subplots() ax.plot([1, 2, 3, 4, 5], [50, 40, 60, 70, 50]) bbox_dict = dict(facecolor='red', alpha=0.9, ec = None, lw = 0, boxstyle = 'circle') fontargs = dict(fontsize = 12, color = 'white', fontweight = 'bold', va = 'top’) for tkl in ax.get_xticklabels(): #Iterating over the xaxis tick labels #Setting the bbox with dict and dict unpacking tkl.set(bbox = bbox_dict, **fontargs) ax.xaxis.set_tick_params(pad = 10) #Increasing the padding between tick and labels fig.savefig('Highlight selected ticks_2.png', dpi = 300,bbox_inches='tight', pad_inches = 0.3) plt.show() Format all tick labels • Iterate over axis tick labels • Setting the bbox for text artist • Set font properties matplotlib.patches.FancyBboxPatch Class Name Attrs Circle circle pad=0.3 DArrow darrow pad=0.3 LArrow larrow pad=0.3 RArrow rarrow pad=0.3 Round round pad=0.3, rounding_size=None Round4 round4 pad=0.3, rounding_size=None Roundtooth roundtooth pad=0.3, tooth_size=None Sawtooth sawtooth pad=0.3, tooth_size=None Square square pad=0.3 boxstyle types Note on Practical Implementation of tick related methods 256
  • 257. Ch Ch Ch Ch Ch Ch Ch DT import matplotlib.pyplot as plt from matplotlib import cm from matplotlib.colors import ListedColormap from matplotlib.colors import LinearSegmentedColormap from itertools import cycle, islice #Using iterator to cycle colors and boxstyle import matplotlib.patches as mpatch plt.rcdefaults() fig, ax = plt.subplots() ax.plot([1, 2, 3, 4, 5], [50, 40, 60, 70, 50]) #If more number of ticks present, we want box style to infinitely loop over style list boxstyle_list = islice(cycle(mpatch.BoxStyle.get_styles()),0,None) #cycle with islice #boxstyle_list = islice(cycle(['circle','darrow', 'rarrow', 'larrow', 'round', 'round4’, 'roundtooth’, 'square', 'sawtooth’]) cmap = 'inferno’ #color map listedcolormap = ListedColormap(cm.get_cmap(cmap,256)(np.linspace(0,0.6,10))) #Using islice to always start from first position color_list = islice(cycle(listedcolormap.colors), 0, None) #create fontargs and bboxargs to pass as arguments in ticklabel properties set fontargs = {'color' :"white",'fontsize' :12, 'fontweight': "bold", 'ha':'center', 'va':'top'} bboxargs = dict( alpha=0.9, ec = None, lw = 0) ax.xaxis.set_tick_params(pad = 10) #Ensuring tick labels have sufficient gap from axis for tkl in ax.get_xticklabels(): tkl.set(bbox = {**bboxargs, 'boxstyle' : next(boxstyle_list), 'facecolor':next(color_list)}, **fontargs ) fig.savefig('Highlight selected ticks_3.png', dpi = 300, format ='png’,bbox_inches='tight’, pad_inches = 0.3) plt.show() • matplotlib.patches.FancyBboxPatch.html • Formatting only selected tick labels • specify-where-to-start-in-an-itertools-cycle-function • fancybox_demo • add-new-keys-to-a-dictionary Format all tick labels • Use cycle iterator object with islice • Set the bbox style with cycle • Create listedcolormap from colormap and use that within cycle object to assign color to ticklabels Note on Practical Implementation of tick related methods 257
  • 258. Ch Ch Ch Ch Ch Ch Ch DT import matplotlib.pyplot as plt import matplotlib.transforms as mtransforms import matplotlib.patches as mpatch from matplotlib.patches import FancyBboxPatch styles = mpatch.BoxStyle.get_styles() spacing = 1.2 figheight = (spacing * len(styles) + .5) fig = plt.figure(figsize=(4 / 1.5, figheight / 1.5)) fontsize = 0.3 * 72 for i, stylename in enumerate(sorted(styles)): fig.text(0.5, (spacing * (len(styles) - i) - 0.5) / figheight, stylename, ha="center", size=fontsize, bbox=dict(boxstyle=stylename, fc="w", ec="k")) • matplotlib.patches.FancyBboxPatch.html • fancybox_demo Patches Class Name Attrs Circle circle pad=0.3 DArrow darrow pad=0.3 LArrow larrow pad=0.3 RArrow rarrow pad=0.3 Round round pad=0.3, rounding_size=None Round4 round4 pad=0.3, rounding_size=None Roundtooth roundtooth pad=0.3, tooth_size=None Sawtooth sawtooth pad=0.3, tooth_size=None Square square pad=0.3 boxstyle types • FancyBboxPatch 258
  • 259. Ch Ch Ch Ch Ch Ch Ch DT import pandas as pd import numpy as np import matplotlib.pyplot as plt np.random.seed(123456) a = np.random.randint(5,15, size=10) b = np.random.randint(5,15, size=10) df = pd.DataFrame({"a":a}) df2 = pd.DataFrame({"b":b}) fig, (ax, ax2) = plt.subplots(ncols=2, sharey=True, figsize = (10,5) ) ax.invert_xaxis() ax.yaxis.tick_right() df["a"].plot(kind='barh', x='LABEL', legend=False, ax=ax) df2["b"].plot(kind='barh', x='LABEL',ax=ax2) fig.patch.set_visible(False) ax.patch.set_visible(False) ax2.patch.set_visible(False) fig.savefig('Two barh adjacent.png', dpi = 300) plt.show() Adjacent Barh charts • https://stackoverflow.com/questions/44049132/python-pandas-plotting-two- barh-side-by-side Context of the Plot • Two adjacent barh plots side-by-side sharing the y-axis • Remove the label in y-axis for the lefthand side chart • Xaxis of the two plots should start from the middle of the two plots • Pandas DataFrame + Matplotlib 259
  • 260. Ch Ch Ch Ch Ch Ch Ch DT #https://sharkcoder.com/data-visualization/mpl- bidirectional import requests import pandas as pd url = 'https://en.wikipedia.org/wiki/List_of_countries_by_gover nment_budget' response = requests.get(url) tables = pd.read_html(response.text) tables[0].to_csv('data.csv’) df = pd.read_csv('data.csv’) data = df[['Country','Revenues','Expenditures']].head(10) data.set_index('Country', inplace=True) data['Revenues'] = data['Revenues'].astype(float) data['Revenues'] = data['Revenues'] / 1000 data['Expenditures'] = data['Expenditures'].astype(float) data['Expenditures'] = data['Expenditures'] / 1000 * (-1) font_color = '#525252' hfont = {'fontname':'Calibri’} # dict for unpacking later facecolor = '#eaeaf2' color_red = '#fd625e' color_blue = '#01b8aa' index = data.index column0 = data['Expenditures'] column1 = data['Revenues'] title0 = 'Expenditures, USD thousand' title1 = 'Revenues, USD thousand’ import matplotlib.pyplot as plt plt.rcdefaults() fig, axes = plt.subplots(figsize=(10,5), facecolor=facecolor, ncols=2, sharey=True) fig.tight_layout() axes[0].barh(index, column0, align='center’, color=color_red, zorder=10) axes[0].set_title(title0, fontsize=18, pad=15, color= color_red, fontweight = 'medium', **hfont) axes[1].barh(index, column1, align='center’, color=color_blue, zorder=10) axes[1].set_title(title1, fontsize=18, pad=15, color= color_blue, fontweight = 'medium', **hfont) # If you have positive numbers and want to invert the x-axis of the left plot #axes[0].invert_xaxis() # To show data from highest to lowest plt.gca().invert_yaxis() axes[0].set(yticks=data.index, yticklabels=data.index) axes[0].yaxis.tick_left() axes[0].tick_params(axis='y', colors='white') # tick color axes[1].set_xticks([1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000]) axes[1].yaxis.set_tick_params(size=0) axes[1].set_xticklabels([1000, 2000, 3000, 4000, 5000, 6000, 7000,8000]) for label in (axes[0].get_xticklabels() +axes[0].get_yticklabels()): label.set(fontsize=13, color=font_color, **hfont) for label in (axes[1].get_xticklabels() +axes[1].get_yticklabels()): label.set(fontsize=13, color=font_color, **hfont) fig.patch.set(facecolor = 'white’) #wspace=0 for no gap between the two axes plt.subplots_adjust(wspace=0, top=0.85, bottom=0.1, left=0.18, right=0.95) [ax.grid(True) for ax in fig.axes] filename = 'mpl-bidirectional2' plt.savefig(filename+'.png', dpi=300, format='png’, bbox_inches='tight') Country Revenues Expenditures United States 5923.829 -9818.53 China 3622.313 -5388.81 Germany 1729.224 -2038.25 Japan 1666.454 -2362.68 France 1334.944 -1609.71 United Kingdom 966.407 -1400.78 Italy 863.785 -1103.72 India 620.739 -940.771 Canada 598.434 -917.271 Spain 481.945 -657.75 Bi Directional Charts The final data frame used for the plot after data aggregation and manipulation is as below (can be used directly) : Back 260
  • 261. Ch Ch Ch Ch Ch Ch Ch DT #https://stackoverflow.com/questions/41296313/stacked-bar-chart-with-centered-labels? import numpy as np import matplotlib.pyplot as plt A = [45, 17, 47] B = [91, 70, 72] #Creating dictionary that contains keyword argument property values for text instance text_kwargs = {'ha':"center", 'va':"center", 'color':"white", 'fontsize':16, 'fontweight':"bold"} fig = plt.figure(facecolor="white") ax = fig.add_subplot(1, 1, 1) bar_width = 0.5 bar_l = np.arange(1, 4) tick_pos = [i + (bar_width / 2) for i in bar_l] ax1 = ax.bar(bar_l, A, width=bar_width, label="A", color="green") ax2 = ax.bar(bar_l, B, bottom=A, width=bar_width, label="B", color="blue") ax.set_ylabel("Count", fontsize=18) ax.set_xlabel("Class", fontsize=18) ax.legend(loc="best") #Adding Legend to “best” location plt.xticks(tick_pos, ["C1", "C2", "C3"], fontsize=16) plt.yticks(fontsize=16) for r1, r2 in zip(ax1, ax2): #Adding data labels at the center of each of the bar patches h1 = r1.get_height() h2 = r2.get_height() ax.text(r1.get_x() + r1.get_width() / 2., h1 / 2., "%d" % h1, **text_kwargs) #unpacking dict ax.text(r2.get_x() + r2.get_width() / 2., h1 + h2 / 2., "%d" % h2, **text_kwargs) fig.savefig('labels_stacked_bar_charts.png', dpi = 300, format = 'png’, bbox_inches='tight') plt.show() Stacked Bar Charts • Adding Labels to the bars 261
  • 262. Ch Ch Ch Ch Ch Ch Ch DT plt.style.use('ggplot') text_kwargs = {'ha':"center", 'va':"center", 'color':"white", 'fontsize':18, 'fontweight':"semibold"} ax = df.plot(stacked=True, kind='bar', figsize=(10, 6), rot='horizontal') fig = ax.figure # .patches gives acess to all the patches (bars) for rect in ax.patches: # Find where everything is located height = rect.get_height() width = rect.get_width() x = rect.get_x() y = rect.get_y() # The height of the bar is the data value and can be used as the label label_text = f'{height}' # f'{height:.2f}' to format decimal values # ax.text(x, y, text) label_x = x + width / 2 label_y = y + height / 2 # plot only when height is greater than specified value if height > 0: ax.text(label_x, label_y, label_text, **text_kwargs) #Dictionary style unpacking ax.legend(bbox_to_anchor=(1.05, 1), loc='upper left', borderaxespad=0) #Adding Legend ax.set_ylabel("Count", fontsize=18, labelpad = 10) #labelpad and fontsize changed ax.set_xlabel("Class", fontsize=18, labelpad = 10) #labelpad and fontsize changed ax.tick_params(labelsize = 16) ax.set_ylim(0, ax.get_ylim()[1]*1.1) #Increasing the upper ylim to create spacing fig.savefig('labels_stacked_bar_charts_ggplot.png', dpi = 300, format = 'png',bbox_inches='tight') plt.show() #https://stackoverflow.com/questions/41296313/ #Check answer by Trenton McKinney Below changes vis-a-vis above link • Dictionary style unpacking of text keyword arguments • Tick labelsize, Axis labelsize and labelpad • Resetting yaxis limits import pandas as pd import matplotlib.pyplot as plt #Data A = [45, 17, 47] B = [91, 70, 72] C = [68, 43, 13] # pandas dataframe df = pd.DataFrame(data={'A': A, 'B': B, 'C': C}) df.index = ['C1', 'C2', 'C3'] Stacked Bar Charts ggplot style 262
  • 263. Ch Ch Ch Ch Ch Ch Ch DT # Pandas Bar Plotting with datetime in x axis #https://stackoverflow.com/questions/30133280/pandas-bar-plot-changes-date- format import pandas as pd import matplotlib.pyplot as plt import numpy as np plt.style.use('default') # generate sample data start = pd.to_datetime("1-1-2012") index = pd.date_range(start, periods= 365) prng = np.random.default_rng(123456) df = pd.DataFrame({'A' : prng.random(365), 'B' : prng.random(365)}, index=index) # resample to any timeframe you need, e.g. months df_months = df.resample("M").sum() # plot fig, ax = plt.subplots() df_months.plot(kind="bar", figsize=(16,5), stacked=True, ax=ax) # Decorations/Embellishments ax.grid(True, alpha = 0.5, ls = 'dotted') ax.set_axisbelow(True) # format xtick-labels with list comprehension ax.set_xticklabels([x.strftime("%Y-%m") for x in df_months.index],ha = 'right', rotation=45) fig.savefig('ticklabels_using timestamp.strftime.png', dpi = 300, bbox_inches = 'tight', transparent = True) plt.show() Pandas Stacked Bar plots • Datetime Index • Using for timestamp.strftime for tick labels • Using Resample to ‘Months’ 263
  • 264. Ch Ch Ch Ch Ch Ch Ch DT #https://stackoverflow.com/questions/55642270/matplotlib-sharex-on-data-with-different-x-values #Check answer by gmds import pandas as pd import numpy as np import matplotlib.pyplot as plt import seaborn as sns fig, axes = plt.subplots(2, sharex=‘all’) ax1, ax2 = axes df = pd.DataFrame({‘x’: [‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’], ‘y1’: np.arrange(6)}) df2 = pd.DataFrame({‘x’: [‘B’, ‘D’, ‘E’], ‘y2’: np.random.rand(3)}) combined = df.merge(df2, how=‘left’, on=‘x’) ax1.bar(combined[‘x’], combined[‘y1’]) ax2.bar(combined[‘x’], combined[‘y2’]) fig.savefig(f’Share_x_across_subplots.png’,dpi=150, format=‘png’, bbox_inches='tight’) plt.show() Share-x on Data with different x values #Alternately, one can also use Seaborn df = pd.DataFrame({'x':['A','B','C','D','E','F'],'y1':np.random.rand(6)}) df2 = pd.DataFrame({'x':['B','D','E'],'y2':np.random.rand(3)}) order = np.unique(list(df.x)+list(df2.x)) fig,axes = plt.subplots(2, sharex='all') sns.barplot(x='x',y='y1',data=df,ax=axes[0], order=order) sns.barplot(x='x',y='y2',data=df2,ax=axes[1], order=order) plt.show() Seaborn Matplotlib 264
  • 265. Ch Ch Ch Ch Ch Ch Ch DT import pandas as pd import numpy as np import matplotlib.pyplot as plt plt.style.use(‘ggplot’) # Preparing Sample data N = 100 industry = ['a','b','c'] city = ['x','y','z'] ind = np.random.choice(industry, N) cty = np.random.choice(city, N) jobs = np.random.randint(low=1,high=250,size=N) df_city =pd.DataFrame({'industry':ind,'city':cty,'jobs':jobs}) # Extracting the number of panels(cities) from df_city cols =df_city.city.value_counts().shape[0] fig, axes = plt.subplots(1, cols, figsize=(8, 8), sharey = True ) for x, city in enumerate(df_city.city.value_counts().index.values): #for loop to create bar plots data = df_city[(df_city['city'] == city)] data = data.groupby(['industry']).jobs.sum() print (data) print (type(data.index)) left= [k[0] for k in enumerate(data)] right= [k[1] for k in enumerate(data)] color = plt.cm.get_cmap('Set1').colors[x] #Access colors from Listed colormaps axes[x].bar(left,right,label="%s" % (city), color = color) axes[x].set_xticks(left, minor=False) axes[x].set_xticklabels(data.index.values) axes[x].legend(loc='best') axes[x].grid(True) fig.suptitle('Employment By Industry By City’, fontsize=20) plt.subplots_adjust(wspace = 0.2) fig.savefig(f'Multiple Subplots_shared_y.png',dpi=300, format='png', bbox_inches='tight') Also check : § https://stackoverflow.com/questions/19613486/pandas -matplotlib-faceting-bar-plots Faceted Bar Charts Pandas + Matplotlib Similar to facet_wrap in ggplot2, Facet_grid in Seaborn 265
  • 266. Ch Ch Ch Ch Ch Ch Ch DT #https://stackoverflow.com/questions/53577630/how-to-make-pareto-chart-in- python A Pareto Chart is a graph that indicates the frequency of defects, as well as their cumulative impact. Pareto Charts are useful to find the defects to prioritize in order to observe the greatest overall improvement. Pareto chart contains both bars and a line graph, where individual values are represented in descending order by bars, and the cumulative total is represented by the line. The chart is named for the Pareto principle, which, in turn, derives its name from Vilfredo Pareto, a noted Italian economist. Pareto Chart import pandas as pd import matplotlib.pyplot as plt from matplotlib.ticker import PercentFormatter df = pd.DataFrame({'country': [177.0, 7.0, 4.0, 2.0, 2.0, 1.0, 1.0, 1.0]}) df.index = ['USA', 'Canada', 'Russia', 'UK', 'Belgium', 'Mexico', 'Germany’, 'Denmark'] df = df.sort_values(by='country',ascending=False) df["cumpercentage"] = df["country"].cumsum()/df["country"].sum()*100 fig, ax = plt.subplots(figsize = (10,5)) ax.tick_params(axis="y", colors="C0",labelsize = 13) ax.tick_params(axis="x", colors="0", rotation = 15) ax2.tick_params(axis="y", colors="C1", labelsize = 13) for labels in ax.get_xticklabels(): labels.set(size = 13,rotation = 0, rotation_mode = 'anchor’, ha = 'center') ax.figure.savefig(f'Pareto Chart.png',dpi=150, format='png’, bbox_inches='tight') ax.bar(df.index, df["country"], color="C0") ax2 = ax.twinx() #Creating twin axes with shared x -axis ax2.plot(df.index, df["cumpercentage"], color="C1", marker="D", ms=7) ax2.yaxis.set_major_formatter(PercentFormatter()) 266
  • 267. Ch Ch Ch Ch Ch Ch Ch DT #https://matplotlib.org/stable/gallery/lines_bars_and_mar kers/fill_between_alpha.html#sphx-glr-gallery-lines-bars- and-markers-fill-between-alpha-py import matplotlib.pyplot as plt import numpy as np import matplotlib.cbook as cbook # Fixing random state for reproducibility np.random.seed(19680801) # load up some sample financial data r = (cbook.get_sample_data('goog.npz’, np_load=True)['price_data'].view(np.recarray)) # create two subplots with the shared x and y axes fig, ax1 = plt.subplots() pricemin = r.close.min() #For entering as argument later #Optional line plotting with thick lines #ax1.plot(r.date, r.close, lw=2) p = ax1.fill_between(r.date, pricemin, r.close, facecolor='blue', alpha=0.5) ax1.grid(True) #Make Gridlines visible ax1.set_ylabel('price’) #Make y ticklabels invisible for label in ax1.get_yticklabels(): label.set_visible(False) fig.suptitle('Google (GOOG) daily closing price') fig.autofmt_xdate() fig.savefig("Fill Area.png",dpi=300, format='png’, bbox_inches='tight’) plt.show() #Above plot by uncommenting the optional line plotting ax1.plot(r.date, r.close, lw=2) Fill Between and Alpha Back 267
  • 268. Ch Ch Ch Ch Ch Ch Ch DT #https://www.machinelearningplus.com/plots/top-50- matplotlib-visualizations-the-master-plots-python/ - 14.- Area-Chart import pandas as pd # Prepare Data df = pd.read_csv("https://github.com/selva86/datasets/raw/m aster/mtcars.csv") x = df.loc[:, ['mpg']] df['mpg_z'] = (x - x.mean())/x.std() df['colors'] = 'black' # color fiat differently df.loc[df.cars == 'Fiat X1-9', 'colors'] = 'darkorange' df.sort_values('mpg_z', inplace=True) df.reset_index(inplace=True) # Draw plot import matplotlib.patches as patches fig, ax = plt.subplots(figsize=(10,12), dpi= 300) plt.hlines(y=df.index, xmin=0, xmax=df.mpg_z, color=df.colors, alpha=0.4, linewidth=1) plt.scatter(df.mpg_z, df.index, color=df.colors, s=[600 if x == 'Fiat X1-9' else 300 for x in df.cars], alpha=0.6) plt.yticks(df.index, df.cars) plt.xticks(fontsize=12) # Annotate plt.annotate('Mercedes Models', xy=(0.0, 11.0), xytext=(1.0, 11), xycoords='data', fontsize=15, ha='center', va='center', bbox=dict(boxstyle='square', fc='firebrick'), arrowprops=dict(arrowstyle='-[, widthB=2.0, lengthB=1.5', lw=2.0, color='steelblue’), color='white') # Add Patches p1 = patches.Rectangle((-2.0, -1), width=.3, height=3, alpha=.2, facecolor='red') p2 = patches.Rectangle((1.5, 27), width=.8, height=5, alpha=.2, facecolor='green’) plt.gca().add_patch(p1) plt.gca().add_patch(p2) # Decorate plt.title('Diverging Bars of Car Mileage’, fontdict={'size':20}) plt.grid(linestyle='--', alpha=0.5) fig.savefig("aesthetic plot.png",dpi=300, format='png’, bbox_inches='tight') plt.show() Interesting Use Case ü Horizontal lines ü Scatter points ü Annotations ü Patches ARTIST Horizontal lines • Starts from the base z score of 0 and move in either direction • Visually appealing and helps mapping the car model label with the scatter point Scatter points The scatter points with size 300 for all cars except Fiat X1-9. x position indicates the z score in terms of mileage. Annotations To markout the Mercedes models Patches To highlight the cars at each of the extremes Raw Data – Next Slide Back 268
  • 269. Ch Ch Ch Ch Ch Ch Ch DT "mpg","cyl","disp","hp","drat","wt","qsec","vs","am","gear","carb","fast","cars","carname" 4.58257569495584,6,160,110,3.9,2.62,16.46,0,1,4,4,1,"Mazda RX4","Mazda RX4" 4.58257569495584,6,160,110,3.9,2.875,17.02,0,1,4,4,1,"Mazda RX4 Wag","Mazda RX4 Wag" 4.77493455452533,4,108,93,3.85,2.32,18.61,1,1,4,1,1,"Datsun 710","Datsun 710" 4.62601340248815,6,258,110,3.08,3.215,19.44,1,0,3,1,1,"Hornet 4 Drive","Hornet 4 Drive" 4.32434966208793,8,360,175,3.15,3.44,17.02,0,0,3,2,1,"Hornet Sportabout","Hornet Sportabout" 4.25440947723653,6,225,105,2.76,3.46,20.22,1,0,3,1,1,"Valiant","Valiant" 3.78153408023781,8,360,245,3.21,3.57,15.84,0,0,3,4,0,"Duster 360","Duster 360" 4.93963561409139,4,146.7,62,3.69,3.19,20,1,0,4,2,1,"Merc 240D","Merc 240D" 4.77493455452533,4,140.8,95,3.92,3.15,22.9,1,0,4,2,1,"Merc 230","Merc 230" 4.38178046004133,6,167.6,123,3.92,3.44,18.3,1,0,4,4,1,"Merc 280","Merc 280" 4.2190046219458,6,167.6,123,3.92,3.44,18.9,1,0,4,4,1,"Merc 280C","Merc 280C" 4.04969134626332,8,275.8,180,3.07,4.07,17.4,0,0,3,3,1,"Merc 450SE","Merc 450SE" 4.15932686861708,8,275.8,180,3.07,3.73,17.6,0,0,3,3,1,"Merc 450SL","Merc 450SL" 3.89871773792359,8,275.8,180,3.07,3.78,18,0,0,3,3,0,"Merc 450SLC","Merc 450SLC" 3.22490309931942,8,472,205,2.93,5.25,17.98,0,0,3,4,0,"Cadillac Fleetwood","Cadillac Fleetwood" 3.22490309931942,8,460,215,3,5.424,17.82,0,0,3,4,0,"Lincoln Continental","Lincoln Continental" 3.83405790253616,8,440,230,3.23,5.345,17.42,0,0,3,4,0,"Chrysler Imperial","Chrysler Imperial" 5.69209978830308,4,78.7,66,4.08,2.2,19.47,1,1,4,1,1,"Fiat 128","Fiat 128" 5.51361950083609,4,75.7,52,4.93,1.615,18.52,1,1,4,2,1,"Honda Civic","Honda Civic" 5.82237065120385,4,71.1,65,4.22,1.835,19.9,1,1,4,1,1,"Toyota Corolla","Toyota Corolla" 4.63680924774785,4,120.1,97,3.7,2.465,20.01,1,0,3,1,1,"Toyota Corona","Toyota Corona" 3.93700393700591,8,318,150,2.76,3.52,16.87,0,0,3,2,0,"Dodge Challenger","Dodge Challenger" 3.89871773792359,8,304,150,3.15,3.435,17.3,0,0,3,2,0,"AMC Javelin","AMC Javelin" 3.64691650576209,8,350,245,3.73,3.84,15.41,0,0,3,4,0,"Camaro Z28","Camaro Z28" 4.38178046004133,8,400,175,3.08,3.845,17.05,0,0,3,2,1,"Pontiac Firebird","Pontiac Firebird" 5.22494019104525,4,79,66,4.08,1.935,18.9,1,1,4,1,1,"Fiat X1-9","Fiat X1-9" 5.09901951359278,4,120.3,91,4.43,2.14,16.7,0,1,5,2,1,"Porsche 914-2","Porsche 914-2" 5.51361950083609,4,95.1,113,3.77,1.513,16.9,1,1,5,2,1,"Lotus Europa","Lotus Europa" 3.97492138287036,8,351,264,4.22,3.17,14.5,0,1,5,4,0,"Ford Pantera L","Ford Pantera L" 4.43846820423443,6,145,175,3.62,2.77,15.5,0,1,5,6,1,"Ferrari Dino","Ferrari Dino" 3.87298334620742,8,301,335,3.54,3.57,14.6,0,1,5,8,0,"Maserati Bora","Maserati Bora" 4.62601340248815,4,121,109,4.11,2.78,18.6,1,1,4,2,1,"Volvo 142E","Volvo 142E" #https://www.machinelearningplus.com/plots/top-50- matplotlib-visualizations-the-master-plots-python/ - 14.-Area- Chart Raw Data Source Link: https://github.com/selva86/datasets/raw/master/mtcars.csv Raw Data : Table on the right • Copy onto excel sheet and save as csv. Use pd.read_csv in jupyter notebook. ` • Alternately, copy onto excel sheet and Use Text to columns in Data Tab. Proceed to R Studio for EDA in R. Raw Data Back 269
  • 270. Ch Ch Ch Ch Ch Ch Ch DT from pandas import DataFrame import matplotlib.pyplot as plt import numpy as np # setting font size to 30 #plt.style.use('ggplot') plt.rcdefaults() plt.rcParams.update({'font.size': 20}) #Preparing the sample data a=np.array([[4,8,5,7,6], [2,3,4,2,6], [4,7,4,7,8], [2,6,4,8,6], [2,4,3,3,2]]) df=DataFrame(a, columns=['a','b','c','d','e’], index=['Jan','Feb','March','April','May']) r = df.plot(kind='bar’) #Using Pandas plotting function #Turn off the minor ticks plt.minorticks_off() # Turn on the grid #plt.grid(which='major', linestyle='-', linewidth='0.5’, color='green') #plt.grid(which='minor', linestyle=':', linewidth='0.5’, color='black’) #Display the y axis grid lines plt.grid(True, axis = 'y', alpha = 0.95, ls = ':’) #Instantiating axes object to use methods ax = plt.gca() #Adding Legend ax.legend(bbox_to_anchor=(1.05, 0.5), loc='center left’, fontsize = 15) #Setting xtick label properties [ticklabel.set(rotation =0, ha = ‘center’, fontsize = 15) for ticklabel in ax.get_xticklabels()] #Setting ytick parameters ax.yaxis.set_tick_params(rotation =0, labelsize = 15 ) [ticks.set(alpha = 0.5) for ticks in ax.get_xticklabels()] [ticks.set(alpha = 0.5) for ticks in ax.get_yticklabels()] #Setting figure properties fig = plt.gcf() fig.set(figheight = 5, figwidth =8) fig.savefig(“Barplot2.png”,dpi=300, format=‘png’, bbox_inches=‘tight’) plt.show() Also check : § https://matplotlib.org/stable/gallery/lines_bars_and_m arkers/barchart.html#sphx-glr-gallery-lines-bars-and- markers-barchart-py § https://www.pythoncharts.com/matplotlib/grouped- bar-charts-matplotlib/ § To get a list of all containers in the axes, use ax.containers https://stackoverflow.com/questions/62189677/how-to- get-a-barcontainer-object-from-an-axessubplot-object- in-matplotllib § To get list of all artists in the axes, use ax.get_children() § To get list of the attributes and methods of axes object, use print(dir(ax)) Back Grouped Bar Chart Pandas plotting 270
  • 271. Ch Ch Ch Ch Ch Ch Ch DT #https://stackoverflow.com/questions/23293011/how-to-plot-a-superimposed-bar-chart-using- matplotlib-in-python a = range(1,10) b = range(4,13) ind = np.arange(len(a)) fig = plt.figure() ax = fig.add_subplot(111) ax.bar(x=ind, height=a, width=0.35,align='center') ax.bar(x=ind, height=b, width=0.35/3, align='center') plt.xticks(ind, a) plt.tight_layout() fig.savefig('Bullet_chart.png', dpi = 300, transparent = True) plt.show() Matplotlib Bar Plots • Bars of different widths 271
  • 272. Ch Ch Ch Ch Ch Ch Ch DT from datetime import datetime import matplotlib.pyplot as plt from matplotlib.dates import ( DateFormatter, AutoDateLocator, AutoDateFormatter, datestr2num ) days = [ '30/01/2019', '31/01/2019', '01/02/2019', '02/02/2019', '03/02/2019', '04/02/2019' ] adata = [1, 9, 10, 3, 7, 6] bdata = [1, 2, 11, 3, 6, 2] #converting/parsing string to python datetime object using strptime #and then converting back to string using strftime x = datestr2num([ datetime.strptime(day, '%d/%m/%Y').strftime('%m/%d/%Y') for day in days ]) #Alternately instead of datestr2num, we can directly use date2num on the datettime object #x = date2num([ datetime.strptime(day, '%d/%m/%Y') for day in days] w = 0.10 fig = plt.figure(figsize=(8, 4)) ax = fig.add_subplot(111) #align:'center' is important and width as 2*w in conjunction with x value ensures adjacent bars ax.bar(x - w, adata, width=2 * w, color='g', align='center', tick_label=days, label = 'adata') ax.bar(x + w, bdata, width=2 * w, color='r', align='center', tick_label=days, label = 'bdata') ax.xaxis.set_major_locator(AutoDateLocator(minticks=3, interval_multiples=False)) ax.xaxis.set_major_formatter(DateFormatter("%d/%m/%y")) ax.legend() #Adding legend - automatic detection of handles, labels fig.savefig('Processing_dates_in_string_as_xaxis.png', dpi = 300) plt.show() #https://stackoverflow.com/questions/59738557/date-format-issues-in- plot-ticks-with-matplotlib-dates-and-datestr2num Context : datestr2num calls (wrapper) dateutil but currently does not forward all keyword arguments. For instance, dayfirst is not forwarded by datestr2num. Instead, month first is always considered. This creates problems incase of ambiguous date formats. The date2strnum is not robust to deal consistently with dates in string form. We therefore first parse date string with Python datetime module functions, convert back to date string with month first string format ('%m/%d/%Y') and then pass it to datestr2num to convert to matplotlib dates. Matplotlib Parsing Date strings • To process strings, use datetime functions • Convert string to datetime object using strptime • Convert datetime object back to string using strftime • Convert the string from previous step to matplotlib.date using datestr2num Code End Code Start 272
  • 273. Ch Ch Ch Ch Ch Ch Ch DT import matplotlib.pyplot as plt from matplotlib.dates import date2num import datetime import matplotlib.dates as mdates x = [ datetime.datetime(2011, 1, 4, 0, 0), datetime.datetime(2011, 1, 5, 0, 0), datetime.datetime(2011, 1, 6, 0, 0) ] x = date2num(x) #Converting datetime objects to matplotlib dates y = [4, 9, 2] z = [1, 2, 3] k = [11, 12, 13] w = 0.05 fig, ax = plt.subplots(figsize = (8,5)) bars1 = ax.bar(x-2*w, y, width=2*w, color='b', align='center', label = 'y') bars2 = ax.bar(x, z, width=2*w, color='g', align='center', label = 'z') bars3 = ax.bar(x+2*w, k, width=2*w, color='r', align='center', label = 'k') ax.xaxis.set_major_locator(mdates.DayLocator()) formatter = mdates.DateFormatter('%d %bn%a') ax.xaxis.set_major_formatter(formatter) ax.legend() def autolabel(rects): for rect in rects: h = rect.get_height() ax.text(rect.get_x()+rect.get_width()/2., 1.01*h, '%d'%int(h), ha='center', va='bottom') autolabel(bars1) autolabel(bars2) autolabel(bars3) plt.show() #Check the above discussion link. I have proposed a solution over here and extended the solution to handle any number of groups. Follow the next few slides. Context : • Three Bar plots plotted for the same datetime index • Each Bar plot corresponds to values for a a single group against date values • Ensure no overlap between the bars • Bars should be symmetrically positioned in either side of date tick value Matplotlib Multiple Group Bar plots • Date Formatting in x axis • No overlapping between bars Code End Code Start 273
  • 274. Ch Ch Ch Ch Ch Ch Ch DT import matplotlib.pyplot as plt import numpy as np from matplotlib import cm from matplotlib.colors import ListedColormap from matplotlib.colors import LinearSegmentedColormap from itertools import cycle, islice #Using iterator to cycle colors and boxstyle def get_colors(y_list, cmap = 'tab20c'): """ Args : ----------- y_list : list containing multiple y series each representing a categorical variable to be plotted cmap = Use next(color_list) to get the next color in each iteration Returns : ----------- color_list : a cycler object using islice to infinitely loop over a list of colors but always start from fixed position """ listedcolormap = ListedColormap(cm.get_cmap(cmap,256)(np.linspace(0,0.6,len(y_list)))) color_list = islice(cycle(listedcolormap.colors),0,None) return color_list #Check the discussion link Context : • Three Bar plots plotted for the same datetime index • Each Bar plot corresponds to values for a a single group against date values • Ensure no overlap between the bars • Bars should be symmetrically positioned in either side of date tick value • Date time formatting in x axis Below is the structure of the code script scalable to any number of groups. Code start Code continue Next slide User defined function 1. Use cycler object to access colors in loop 2. Make bar plots for proper placement of bars and date time formatting of x axis User Defined Functions 3.make_barplots( *args) Call the function Matplotlib Multiple Group Bar plots • Date Formatting in x axis • No overlapping between bars User Defined function for multiple scenarios Slide 1 of 4 Scalable Solution for multiple scenarios 274
  • 275. Ch Ch Ch Ch Ch Ch Ch DT # Creating User defined Function for returning the desired plot def make_barplots(x, y_list, y_labels = [], w = 0.05, cmap = 'tab20c', style = 'seaborn- whitegrid'): """ Args : ----------- x : list of x values of the data type matplotlib dates y_list : list of groupwise values corresponding to the x values y_labels : list of group labels in the format string style : plot style to use cmap : colormap name from the list of matplotlib colormaps w : half of the width of bar (The relationship is Bar width = 2*w) If the input data is a Dataframe df with index of type datetimeindex and we want the columns to be plotted, then below conversion methods helpful : x = date2num(df.index) y_list = df.transpose().to_numpy().tolist() Returns : ----------- fig : Figure containing the axes and barplot ax : Axes contining the barplots bars_list : List containing barcontainer objects Raises : ----------- Warning : Legend not shown if the y_labels argument is empty. "”” Code continues to Next slide Code continues from Prev slide cycler object to access colors in in a loop To Make bar plots User Defined Functions make_barplots (*args) Call the function Slide 2 of 4 Scalable Solution for multiple scenarios 275
  • 276. Ch Ch Ch Ch Ch Ch Ch DT plt.style.use(style) fig, ax = plt.subplots(figsize = (10,6)) bars_list = [] # Creating color_list as a cycler object for cycling through the colors color_list = get_colors(y_list, cmap = cmap) # Creating bar plots iteratively from the list y_list containing the list of values for i,y in enumerate(y_list) : n = len(y_list) # number of groups p = n//2 # p to be used for finding the start location x_offset = 2*w*n//2+w*n%2 # calculating offset from tick location to locate start location # Plotting the bar plot for a single group # The start location will be further offset # by bar widths multiplied by the index position (i) of the group within the y_list bars = ax.bar(x-x_offset+i*2*w, y, width=2*w, color=next(color_list), align='edge’) bars_list.append(bars) # Creating a function to add data labels above bars def autolabel(rects): for rect in rects: h = rect.get_height() ax.text(rect.get_x()+rect.get_width()/2., 1.01*h, '%d'%int(h), ha='center', va='bottom’) # Adding data labels to bars by enumerating over bar containers and for i, bars in enumerate(bars_list) : autolabel(bars) # Setting label property of bars # for automatic detection of handles with labels for legend if len(y_labels) == len(y_list): #Only if labels are provided while calling function bars.set_label(y_labels[i]) Logic for consistent placement of the bars Defining the problem : • A bar belonging to a group should appear at fixed position versus its xtick • Bars of different group should not overlap What we want ? Symmetrical distribution of bar groups across a tick location. The group of bar at each of the x axis value should be symmetrically distributed around the tick. There should be equal no. of bar groups on either side of the x-axis tick location. How will we achieve ? -- It’s a problem of placement of bars We will focus specifically on the x value of a bar plot for a single group at a time. Let’s further narrow it down to a single tick location. Three locations are important : § Tick Location § Start of the first bar patch corresponding to a tick location § Actual x value of a bar patch belonging to a group 1. Tick Location is matplotlib date( a floating point) and is fixed in our context. 2. Start Location will be offset from Tick location depending on number of bars (number of groups) for a single tick 3. For even number of groups lets consider 4, the start location will be offset by the width of 2 bars. 4. For odd number of groups lets consider 5, the start location will be offset by the width of 2.5 bars. 5. Actual x value will be offset from the Start location depending on the group sequence Code continues to Next slide Code continues from Prev slide LOGIC cycler object to access colors in in a loop Make bar plots User Defined Functions make_barplots (*args) Call the function Slide 3 of 4 Scalable Solution for multiple scenarios 276
  • 277. Ch Ch Ch Ch Ch Ch Ch DT locator = mdates.DayLocator() #Creating locator object formatter = mdates.DateFormatter('%d %bn%a’) #Creating locator object ax.xaxis.set_major_locator(locator) ax.xaxis.set_major_formatter(formatter) ax.xaxis.grid(False) #Making the xgrid lines disappear ax.set_ylim(ax.get_ylim()[0]*1.2,ax.get_ylim()[1]*1.1) #Resetting y limits for ample margins if len(y_labels) == len(y_list): #handles, labels = ax.get_legend_handles_labels() #print(handles) #print(labels) ax.legend(loc = 'best') else : print('labels for the bars not entered. Hence, Legend missing.') fig.savefig('Multiple_barsplot_with_xaxis_datetickers.png', dpi = 300) return fig, ax, bars_list #returns figure, axes, list containing bar container objects # Preparing the data and making bar plot by using the user defined function make_barplots() import matplotlib.pyplot as plt from matplotlib.dates import date2num import datetime import matplotlib.dates as mdates x = [ datetime.datetime(2011, 1, 4, 0, 0), datetime.datetime(2011, 1, 5, 0, 0), datetime.datetime(2011, 1, 6, 0, 0)] x = date2num(x) y = [4, 9, 2] z = [1, 2, 3] k = [11, 12, 13] fig, ax, bars_list = make_barplots(x, y_list, y_labels, w = 0.1, style = 'default') Code continues form prev slide Code End cycler object to access colors in in a loop Make bar plots User Defined Functions make_barplots (*args) Call the function Slide 4 of 4 Scalable Solution for multiple scenarios 277
  • 278. Ch Ch Ch Ch Ch Ch Ch DT import pandas as pd import numpy as np #Fixing state for reproducibility np.random.seed(123456) #Creating sample data for plotting using numpy and pandas index = pd.date_range("1/1/2000", periods=8) s = pd.Series(np.random.randn(5), index=["a", "b", "c", "d", "e"]) df = pd.DataFrame(np.random.randint(20,100, (8,4)), index=index, columns=["A", "B", "C", "D"]) #Make bar plots using the userdefined function make_barplots make_barplots(x = date2num(df.index), y_list = df.transpose().to_numpy().tolist() , y_labels = df.columns, w = 0.1, style = ‘default’) Creating DataFrames https://pandas.pydata.org/docs/user_guide/dsintro.html#dataframe Create sample data https://github.com/pandas-dev/pandas/blob/v1.3.1/pandas/core/frame.py#L456-L10748 line no. 1595 https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_numpy.html#pandas.DataFrame.to_numpy Pandas DataFrame columns are Pandas Series when you pull them out, which you can then call x.tolist() on to turn them into a Python list. ( df .filter(['column_name’]) .values .reshape(1, -1) .ravel() .tolist() ) A B C D 2000-01-01 88 82 69 87 2000-01-02 35 47 88 24 2000-01-03 47 93 66 31 2000-01-04 51 56 65 71 2000-01-05 31 41 94 25 2000-01-06 29 84 60 91 2000-01-07 48 66 81 99 2000-01-08 84 96 75 52 2000-01-01 2000-01-02 2000-01-03 2000-01-04 2000-01-05 2000-01-06 2000-01-07 2000-01-08 A 88 35 47 51 31 29 48 84 B 82 47 93 56 41 84 66 96 C 69 88 66 65 94 60 81 75 D 87 24 31 71 25 91 99 52 DataFrame DataFrame - Transpose Alternate way from DataFrame to List Matplotlib Multiple Group Bar plots • Date Formatting in x axis • No overlapping between bars • Number of bar groups = 4 Demo using the User defined function make_barplots Code End Code Start 278
  • 279. Ch Ch Ch Ch Ch Ch Ch DT import matplotlib.pyplot as plt import numpy as np import matplotlib.ticker as ticker plt.rcdefaults() fig = plt.figure() # generate sample data for this example count = 12 xs = list(range(1,count+1)) prng = np.random.default_rng(123456) ys_bars = prng.normal(loc=3.0,size=len(xs)) ys_lines = prng.normal(loc=5.0,size=len(xs),scale=0.5) # this is the axes on the top ax1= plt.subplot(211) # plot the same numbers but multiplied by 20 ax1.plot(xs,ys_lines*20,color='red') # order is important when setting ticks. # Ticks must be set after the plot has been drawn #ax1.set_yticks(np.arange(0,101,10)) ax1.yaxis.set_major_locator(ticker.MultipleLocator(10)) # set ticks for the common x axis (bottom) ax1.xaxis.set_ticks(xs) ax1.grid(True, alpha = 0.5) ax1.fill_between(xs,y1 = ys_lines*20, alpha = 0.5, color = 'xkcd:carolina blue') ax1.vlines(x=xs, ymin = 0, ymax = ys_lines*20, alpha = 0.5, lw = 0.3 ) ax2= plt.subplot(212) ax2.bar(xs,ys_bars,color='red') ax2.yaxis.grid(True, alpha = 0.5) ax2.set_axisbelow(True) ax2.set_xticks(xs) ax2.yaxis.set_major_locator(ticker.MultipleLocator(1)) plt.subplots_adjust(hspace = 0.4) fig.savefig('Simple LinePlots_BarPlots_sharedxaxis.png', dpi = 300, transparent = True) Multiple Subplots Line plot + Bar plot Shared Axis • Categorical Bar and Line plot across multiple subplots • Ticker.MultipleLocator(10) • Axes.fill_between() • Axes.vlines() Code End Code Start 279
  • 280. Ch Ch Ch Ch Ch Ch Ch DT ax1.bar(xs,ys_bars,color = 'xkcd:lichen', alpha = 0.8) # order is important when setting ticks. # Ticks must be set after the plot has been drawn ax1.set_yticks(np.arange(0,11,1)) # define the number of ticks NUM_TICKS=11 # change the tick locator for this axis and set the desired number of ticks ax1.yaxis.set_major_locator(plt.LinearLocator(numticks=NUM_TICKS)) # create the 'twin’ axes with the y axis on right ax2=ax1.twinx() # plot the same numbers but multiplied by 20 ax2.plot(xs,ys_lines*20,color='red') # set the limits for the twin axis ymax = (ax2.get_ylim()[1]//100+0.5)*100 ax2.set_ylim(0, ymax) # change the tick locator for this axis and set the desired number of ticks ax2.yaxis.set_major_locator(plt.LinearLocator(numticks=NUM_TICKS)) # set ticks for the common x axis (bottom) ax2.xaxis.set_ticks(xs) #Show yaxis gridlines ax1.yaxis.grid(True) ax1.set_axisbelow(True) fig.savefig('Barplot_Lineplot.png', dpi = 300, transparent = False) import matplotlib.pyplot as plt import numpy as np fig, ax1 = plt.subplots() # generate sample data for this example xs = list(range(1,13)) prng = np.random.default_rng(123456) ys_bars = prng.normal(loc=3.0,size=12) ys_lines = prng.normal(loc=5.0,size=12,scale=0.5) Overlaying Charts • Categorical Bar and Line plot on same Axes • X axis is numeric but non datetime • Twin Axes with shared x axis • Aligning yaxis of the twin axes Check https://queirozf.com/entries/matplotlib-pyplot-by-example Note : Changes have been done visavis the code in above link. Code End Code Start 280
  • 281. Ch Ch Ch Ch Ch Ch Ch DT Check https://queirozf.com/entries/matplotlib-pyplot-by-example Note : Changes have been done visavis the code in above link. ax1.bar(xs,ys_bars,color = 'xkcd:lichen') # order is important when setting ticks. # Ticks must be set after the plot has been drawn ax1.set_yticks(np.arange(0,11,1)) # define the number of ticks NUM_TICKS=11 # change the tick locator for this axis and set the desired number of ticks ax1.yaxis.set_major_locator(plt.LinearLocator(numticks=NUM_TICKS)) # plot the same numbers but multiplied by 20 ax2.plot(xs,ys_lines*20,color='red') # set the ticks for the twin axis #ax2.set_yticks(np.arange(0,101,10)) ymax = (ax2.get_ylim()[1]//100+0.5)*100 ax2.set_ylim(0, ymax) # change the tick locator for this axis and set the desired number of ticks ax2.yaxis.set_major_locator(plt.LinearLocator(numticks=NUM_TICKS)) # set ticks for the common x axis (bottom) ax2.xaxis.set_ticks(xs) ax1.yaxis.grid(True) ax1.set_axisbelow(True) ax2.vlines(x = xs,ymin = 0, ymax = ys_lines*20) #Axes.vlines from line plot fig.savefig('Barplot_Lineplot_2.png', dpi = 300, transparent = False) import matplotlib.pyplot as plt import numpy as np fig, ax1 = plt.subplots() # generate sample data for this example xs = list(range(1,13)) prng = np.random.default_rng(123456) ys_bars = prng.normal(loc=3.0,size=12) ys_lines = prng.normal(loc=5.0,size=12,scale=0.5) Overlaying Charts • Categorical Bar and Line plot on same Axes • X axis is numeric but non datetime • Twin Axes with a shared x axis • Ax.vlines from the line plot Code End Code Start # create the 'twin’ axes with the y axis on the right ax2=ax1.twinx() # Setting zorder of the twin axes below original axes. Check link1, link2 on zorder ax2.set_zorder(ax2.get_zorder()-1) ax1.patch.set(visible = False) 281
  • 284. Ch Ch Ch Ch Ch Ch Ch DT The plot and data is inspired from the below link : https://pythondata.com/visualizing-data-overlaying-charts/ Most of the code and comments of original link is as it is considering it is very informative. However, below changes have been done : • Locator/formatter pairs created for placement of x-axis tick labels • For loop to create the bar plots iteratively • Cycler object with islice from itertools to assign colors to bars • Addition of legend by mix of automatic detection and proxy artists For the revised code, go to the link : https://github.com/Bhaskar-JR/Matplotlib_Overlaying_Charts/blob/main/Matplotlib_Overlaying_Charts.ipynb 284
  • 285. Ch Ch Ch Ch Ch Ch Ch DT plt.rcParams['figure.figsize']=(20,10) # set the figure size plt.style.use('fivethirtyeight') # using the fivethirtyeight matplotlib theme path1 = "https://raw.githubusercontent.com/Bhaskar-JR/Matplotlib_Overlaying_Charts/main/Sales_Data.csv" #path = '/Users/bhaskarroy/Files/Data Science/PYTHON/Visualisation/Matplotlib/Overlaying charts/sales.csv' sales = pd.read_csv(path1) # Read the data in sales.Date = pd.to_datetime(sales.Date) #set the date column to datetime sales.set_index('Date', inplace=True) #set the index to the date column # now the hack for the multi-colored bar chart: # create fiscal year dataframes covering the timeframes you are looking for. In this case, # the fiscal year covered October - September. # -------------------------------------------------------------------------------- # Note: This should be set up as a function, but for this small amount of data, # I just manually built each fiscal year. This is not very pythonic and would # suck to do if you have many years of data, but it isn't bad for a few years of data. # -------------------------------------------------------------------------------- fy10_all = sales[(sales.index >= '2009-10-01') & (sales.index < '2010-10-01')] fy11_all = sales[(sales.index >= '2010-10-01') & (sales.index < '2011-10-01')] fy12_all = sales[(sales.index >= '2011-10-01') & (sales.index < '2012-10-01')] fy13_all = sales[(sales.index >= '2012-10-01') & (sales.index < '2013-10-01')] fy14_all = sales[(sales.index >= '2013-10-01') & (sales.index < '2014-10-01')] fy15_all = sales[(sales.index >= '2014-10-01') & (sales.index < '2015-10-01')] The plot and data is inspired from the link. Below changes have been done in the code : • Locator/formatter pairs created for placement of x-axis tick labels • For loop to create the bar plots iteratively • Cycler object with islice from itertools to assign colors to bars • Addition of legend by mix of automatic detection and proxy artists For the revised code, go to the link Code continues to Next slide Code Start 285
  • 286. Ch Ch Ch Ch Ch Ch Ch DT # Let's build our plot fig, ax1 = plt.subplots() ax2 = ax1.twinx() # set up the 2nd axis df = sales.copy() df.index = mdates.date2num(df.index) ax1.plot(df.Sales_Dollars, label = 'Monthly Revenue') #plot the Revenue on axis #1 # Creating cycler object with islice to set bar plot colors to grey, orange alternately from itertools import cycle, islice from datetime import datetime import matplotlib.patches as mpatches color_list = islice(cycle(['orange','grey']), 0, None) # Using for loop to plot the fiscal year data sequentially as bar plots # Assign color from cycler object kwargs = dict(width=20, alpha=0.2) #kwargs dictionary for bar plot arguments for fy in [fy10_all, fy11_all, fy12_all, fy13_all, fy14_all, fy15_all]: fyr = fy.copy() fyr.index = mdates.date2num(fyr.index) ax2.bar(fyr.index, fyr.Quantity, **kwargs, color = next(color_list), label = 'Monthly Quantity') ax2.grid(b=False) # turn off grid #2 ax1.set_title('Monthly Sales Revenue vs Number of Items Sold Per Month', fontsize = 25, y = 1.05) ax1.set_ylabel('Monthly Sales Revenue') ax2.set_ylabel('Number of Items Sold’) The plot and data is inspired from the link. Below changes have been done in the code : • Locator/formatter pairs created for placement of x-axis tick labels • For loop to create the bar plots iteratively • Cycler object with islice from itertools to assign colors to bars • Addition of legend by mix of automatic detection and proxy artists For the revised code, go to the link Code continues to Next slide Code continues from Prev slide 286
  • 287. Ch Ch Ch Ch Ch Ch Ch DT #Creating locator/formatter from dateutil.rrule import rrule, MONTHLY,YEARLY from datetime import datetime, timedelta #timedelta holds days, seconds, microseconds internally import matplotlib.dates as mdates import matplotlib.ticker as ticker start_date = datetime(2009,10,1) #Let's do ceiling division to get the relevant number of financial years count = -(-(sales.index[-1]-start_date)//timedelta(days = 365.25)) #Division of timedelta by timedelta of 365.25days date_list = list(rrule(freq=YEARLY, count=count, dtstart=start_date)) date_list = mdates.date2num(date_list) #Creating locator and formatter locator = ticker.FixedLocator(date_list) date_labels = [datetime.strftime(x, "FY %Y") for x in mdates.num2date(date_list)] formatter = ticker.FixedFormatter(date_labels) # Set the x-axis labels to be more meaningful than just some random dates. ax1.xaxis.set_major_locator(locator) ax1.xaxis.set_major_formatter(formatter) ax1.xaxis.grid(b = True, color = 'grey', lw = 1,alpha = 0.5) ax1.xaxis.set_tick_params(pad = 15) [tkl.set(ha = 'left',rotation = 0, rotation_mode = "anchor") for tkl in ax1.xaxis.get_ticklabels()] # Creating Legend with a mix of automatic detection and adding proxy artists handle, label = ax1.get_legend_handles_labels() patch1 = mpatches.Patch(color='orange',alpha = 0.5, label='Monthly Qty',zorder = 100) patch2 = mpatches.Patch(color='grey',alpha = 0.5, label='Monthly Qty', zorder = 100) handle.extend([patch1, patch2]) ax1.legend(handles = handle,bbox_to_anchor = (1,1), loc = 'upper right', bbox_transform = fig.transFigure) #ax2.set_ylim(0,ax2.get_ylim()[1]*10//3) # To restrict the bar plots to only 30% of the height of line plots plt.show() The plot and data is inspired from the link. Below changes have been done in the code : • Locator/formatter pairs created for placement of x-axis tick labels • For loop to create the bar plots iteratively • Cycler object with islice from itertools to assign colors to bars • Addition of legend by mix of automatic detection and proxy artists For the revised code, go to the link Code End Code continues from Prev slide 287
  • 288. Ch Ch Ch Ch Ch Ch Ch DT https://datasciencelab.wordpress.com/2013/12/21/ beautiful-plots-with-pandas-and-matplotlib/ Data Source : demographic data from countries in the European Union obtained from Wolfram|Alpha. Data Set : Contains information on population, extension and life expectancy in 24 European countries. Visualization : • Our Main objective is exploration on usage of hues, transparencies and simple layouts to create informative, rich and appealing visualisation. • Specifically • bar length (along horizontal direction) is population indicator • bar length (along vertical direction) is country size indicator • Color of the bar is mapped to Age expectancy Go to the above link for detailed run-through of the context and the code for creating the plot. 288
  • 289. Ch Ch Ch Ch Ch Ch Ch DT https://datasciencelab.wordpress.com/2013/12/21/ beautiful-plots-with-pandas-and-matplotlib/ import matplotlib.pyplot as plt import pandas as pd import numpy as np import matplotlib as mpl from matplotlib.colors import LinearSegmentedColormap from matplotlib.lines import Line2D from matplotlib import cm # Preparing the Data countries = ['France','Spain','Sweden','Germany','Finland','Poland','Italy', 'United Kingdom','Romania','Greece','Bulgaria','Hungary', 'Portugal','Austria','Czech Republic','Ireland','Lithuania','Latvia', 'Croatia','Slovakia','Estonia','Denmark','Netherlands','Belgium'] extensions = [547030,504782,450295,357022,338145,312685,301340,243610,238391, 131940,110879,93028,92090,83871,78867,70273,65300,64589,56594, 49035,45228,43094,41543,30528] populations = [63.8,47,9.55,81.8,5.42,38.3,61.1,63.2,21.3,11.4,7.35, 9.93,10.7,8.44,10.6,4.63,3.28,2.23,4.38,5.49,1.34,5.61, 16.8,10.8] life_expectancies = [81.8,82.1,81.8,80.7,80.5,76.4,82.4,80.5,73.8,80.8,73.5, 74.6,79.9,81.1,77.7,80.7,72.1,72.2,77,75.4,74.4,79.4,81,80.5] data = {'extension' : pd.Series(extensions, index=countries), 'population' : pd.Series(populations, index=countries), 'life expectancy' : pd.Series(life_expectancies, index=countries)} df = pd.DataFrame(data) df = df.sort_values(by = 'life expectancy') Code Start Code continues to Next slide Go to the above link for comprehensive run- through of the context and the code for creating the plot. The code has been reproduced from the link with all the comments intact. There is only a minor change owing to API changes as the link is dated. The change is : mpl.colors.Normalize has been used for instantiation of norm as required for Scalar Mappable. See also Colormap reference for a list of builtin colormaps. Creating Colormaps in Matplotlib for examples of how to make colormaps. Choosing Colormaps in Matplotlib an in-depth discussion of choosing colormaps. Colormap Normalization for more details about data normalization. Customized Colorbars tutorials 289
  • 290. Ch Ch Ch Ch Ch Ch Ch DT # Create a figure of given size fig = plt.figure(figsize=(16,12)) # Add a subplot ax = fig.add_subplot(111) # Set title ttl = 'Population, size and age expectancy in the European Union' # Set color transparency (0: transparent; 1: solid) a = 0.7 # Create a colormap customcmap = [(x/24.0, x/48.0, 0.05) for x in range(len(df))] # Plot the 'population' column as horizontal bar plot df['population'].plot(kind='barh', ax=ax, alpha=a, legend=False, color=customcmap, edgecolor='w', xlim=(0,max(df['population'])), title=ttl) # Remove grid lines (dotted lines inside plot) ax.grid(False) # Remove plot frame ax.set_frame_on(False) # Pandas trick: remove weird dotted line on axis #ax.lines[0].set_visible(False) # Customize title, set position, allow space on top of plot for title ax.set_title(ax.get_title(), fontsize=26, alpha=a, ha='left') plt.subplots_adjust(top=0.9) ax.title.set_position((0,1.08)) # Set x axis label on top of plot, set label text ax.xaxis.set_label_position('top') xlab = 'Population (in millions)' ax.set_xlabel(xlab, fontsize=20, alpha=a, ha='left') ax.xaxis.set_label_coords(0, 1.04) Code continues to Next slide Code continues from Prev slide https://datasciencelab.wordpress.com/2013/12/21/ beautiful-plots-with-pandas-and-matplotlib/ Go to the above link for comprehensive run- through of the context and the code for creating the plot. The code has been reproduced from the link with all the comments intact. There is only a minor change owing to API changes as the link is dated. The change is : mpl.colors.Normalize has been used for instantiation of norm as required for Scalar Mappable. See also Colormap reference for a list of builtin colormaps. Creating Colormaps in Matplotlib for examples of how to make colormaps. Choosing Colormaps in Matplotlib an in-depth discussion of choosing colormaps. Colormap Normalization for more details about data normalization. Customized Colorbars tutorials 290
  • 291. Ch Ch Ch Ch Ch Ch Ch DT # Position x tick labels on top ax.xaxis.tick_top() # Remove tick lines in x and y axes ax.yaxis.set_ticks_position('none') ax.xaxis.set_ticks_position('none') # Customize x tick labels xticks = [5,10,20,50,80] ax.xaxis.set_ticks(xticks) ax.set_xticklabels(xticks, fontsize=16, alpha=a) # Customize y tick labels yticks = [item.get_text() for item in ax.get_yticklabels()] ax.set_yticklabels(yticks, fontsize=16, alpha=a) ax.yaxis.set_tick_params(pad=12) # Set bar height dependent on country extension # Set min and max bar thickness (from 0 to 1) hmin, hmax = 0.3, 0.9 xmin, xmax = min(df['extension']), max(df['extension']) # Function that interpolates linearly between hmin and hmax f = lambda x: hmin + (hmax-hmin)*(x-xmin)/(xmax-xmin) # Make array of heights hs = [f(x) for x in df['extension']] # Iterate over bars for container in ax.containers: # Each bar has a Rectangle element as child for i,child in enumerate(container.get_children()): # Reset the lower left point of each bar so that bar is centered child.set_y(child.get_y()- 0.125 + 0.5-hs[i]/2) # Attribute height to each Recatangle according to country's size plt.setp(child, height=hs[i]) Code continues to Next slide Code continues from Prev slide https://datasciencelab.wordpress.com/2013/12/21/ beautiful-plots-with-pandas-and-matplotlib/ Go to the above link for comprehensive run- through of the context and the code for creating the plot. The code has been reproduced from the link with all the comments intact. There is only a minor change owing to API changes as the link is dated. The change is : mpl.colors.Normalize has been used for instantiation of norm as required for Scalar Mappable. See also Colormap reference for a list of builtin colormaps. Creating Colormaps in Matplotlib for examples of how to make colormaps. Choosing Colormaps in Matplotlib an in-depth discussion of choosing colormaps. Colormap Normalization for more details about data normalization. Customized Colorbars tutorials 291
  • 292. Ch Ch Ch Ch Ch Ch Ch DT # Legend # Create fake labels for legend l1 = Line2D([], [], linewidth=6, color='k', alpha=a) l2 = Line2D([], [], linewidth=12, color='k', alpha=a) l3 = Line2D([], [], linewidth=22, color='k', alpha=a) # Set three legend labels to be min, mean and max of countries extensions # (rounded up to 10k km2) rnd = 10000 rnd = 10000 labels = [str(int(round(l/rnd,1 )*rnd)) for l in [min(df['extension']), np.mean(df['extension']), max(df['extension'])]] # Position legend in lower right part # Set ncol=3 for horizontally expanding legend leg = ax.legend([l1, l2, l3], labels, ncol=3, frameon=False, fontsize=16, bbox_to_anchor=[1.1, 0.11], handlelength=2, handletextpad=1, columnspacing=2, title='Size (in km2)') # Customize legend title # Set position to increase space between legend and labels plt.setp(leg.get_title(), fontsize=20, alpha=a) leg.get_title().set_position((0, 10)) # Customize transparency for legend labels [plt.setp(label, alpha=a) for label in leg.get_texts()] Code continues to Next slide Code continues from Prev slide https://datasciencelab.wordpress.com/2013/12/21/ beautiful-plots-with-pandas-and-matplotlib/ Go to the above link for comprehensive run- through of the context and the code for creating the plot. The code has been reproduced from the link with all the comments intact. There is only a minor change owing to API changes as the link is dated. The change is : mpl.colors.Normalize has been used for instantiation of norm as required for Scalar Mappable. See also Colormap reference for a list of builtin colormaps. Creating Colormaps in Matplotlib for examples of how to make colormaps. Choosing Colormaps in Matplotlib an in-depth discussion of choosing colormaps. Colormap Normalization for more details about data normalization. Customized Colorbars tutorials 292
  • 293. Ch Ch Ch Ch Ch Ch Ch DT # Create a fake colorbar ctb = LinearSegmentedColormap.from_list('custombar', customcmap, N=2048) # Trick from http://stackoverflow.com/questions/8342549/ # matplotlib-add-colorbar-to-a-sequence-of-line-plots # Used mpl.colors.Normalize for Scalar Mappable sm = plt.cm.ScalarMappable(cmap=ctb, norm=mpl.colors.Normalize(vmin=72, vmax=84)) # Fake up the array of the scalar mappable sm._A = [] # Set colorbar, aspect ratio cbar = plt.colorbar(sm, alpha=0.05, aspect=16, shrink=0.4) cbar.solids.set_edgecolor("face") # Remove colorbar container frame cbar.outline.set_visible(False) # Fontsize for colorbar ticklabels cbar.ax.tick_params(labelsize=16) # Customize colorbar tick labels mytks = range(72,86,2) cbar.set_ticks(mytks) cbar.ax.set_yticklabels([str(a) for a in mytks], alpha=a) # Colorbar label, customize fontsize and distance to colorbar cbar.set_label('Age expectancy (in years)', alpha=a, rotation=270, fontsize=20, labelpad=20) # Remove color bar tick lines, while keeping the tick labels cbarytks = plt.getp(cbar.ax.axes, 'yticklines') plt.setp(cbarytks, visible=False) plt.savefig("beautiful plot.png", bbox_inches='tight') Code End Code continues from Prev slide https://datasciencelab.wordpress.com/2013/12/21/ beautiful-plots-with-pandas-and-matplotlib/ Go to the above link for comprehensive run- through of the context and the code for creating the plot. The code has been reproduced from the link with all the comments intact. There is only a minor change owing to API changes as the link is dated. The change is : mpl.colors.Normalize has been used for instantiation of norm as required for Scalar Mappable. See also Colormap reference for a list of builtin colormaps. Creating Colormaps in Matplotlib for examples of how to make colormaps. Choosing Colormaps in Matplotlib an in-depth discussion of choosing colormaps. Colormap Normalization for more details about data normalization. Customized Colorbars tutorials 293
  • 294. Ch Ch Ch Ch Ch Ch Ch DT Data Source : https://uidai.gov.in/images/state-wise-aadhaar-saturation.pdf Check out the github repository for the shape files and notebook: https://github.com/Bhaskar-JR/India-Statewise-Population-2020 GeoSpatial Data - Chloropeth Map Using Geopandas, Matplotlib 294
  • 295. Ch Ch Ch Ch Ch Ch Ch DT Data Source : https://uidai.gov.in/images/state-wise-aadhaar-saturation.pdf Check out the github repository for the shape files and notebook: https://github.com/Bhaskar-JR/India-Statewise-Population-2020 GeoSpatial Data - Chloropeth Map Using Geopandas, Matplotlib 295
  • 297. Ch Ch Ch Ch Ch Ch Ch DT Category Description Link Matplotlib overview, tutorials Succinct overview of matplotlib https://www.webpages.uidaho.edu/~stevel/504/Matplotlib%20Notes.pdf What does it mean when they say 'stateful’? https://stackoverflow.com/questions/24764918/what-does-it-mean-when-they-say-stateful/24768434#24768434 Summary of Matplotlib for python developers https://radhakrishna.typepad.com/rks_musings/2012/04/matplotlib-for-python-developers-summary.html Very Interesting Article ”Matplotlib and the Future of Visualization in Python”by Jake VanderPlas https://jakevdp.github.io/blog/2013/03/23/matplotlib-and-the-future-of-visualization-in-python/ Excellent resource with examples using mix of Pyplot and OO style - “Python Data Cleaning Cookbook- Modern techniques and Python tools to detect and remove dirty data and extract key insights” by Michael Walker [] Advance plotting Tutorial https://python4astronomers.github.io/plotting/advanced.html Nicola P Rougier https://github.com/rougier/matplotlib-tutorial Beautiful tutorial using Pandas and Matplotlib in conjunction : http://jonathansoma.com/lede/algorithms-2017/classes/fuzziness-matplotlib/how-pandas-uses-matplotlib-plus-figures- axes-and-subplots/ Blog Post - Python, Machine Learning, and Language Wars https://sebastianraschka.com/blog/2015/why-python.html Python related Understanding df.plot in Pandas : http://jonathansoma.com/lede/algorithms-2017/classes/fuzziness-matplotlib/understand-df-plot-in-pandas/ Discussion to wrap all of the matplotlib getter and setter methods with python properties, allowing them to be read and written like class attributes https://matplotlib.org/stable/devel/MEP/MEP13.html?highlight=artist%20attributes#id1 On *args and **kwargs https://stackoverflow.com/questions/36901/what-does-double-star-asterisk-and-star-asterisk-do-for-parameters?rq=1 On purpose of kwargs in https://stackoverflow.com/questions/1769403/what-is-the-purpose-and-use-of-kwargs? Decorator basics https://stackoverflow.com/questions/739654/how-to-make-function-decorators-and-chain-them-together?rq=1 Sorting a dictionary by value https://stackoverflow.com/questions/613183/how-do-i-sort-a-dictionary-by-value/613218#613218 Using ax parameter https://stackoverflow.com/questions/43085427/using-dataframe-plot-to-make-a-chart-with-subplots-how-to-use-ax- parameter? Infinite Iterating always from a fixed position : https://stackoverflow.com/questions/42459582/how-to-specify-where-to-start-in-an-itertools-cycle-function Difference between property and attribute : https://stackoverflow.com/questions/7374748/whats-the-difference-between-a-python-property-and- attribute/7377013#7377013 Introduction to Probability Using Python : https://nbviewer.jupyter.org/url/norvig.com/ipython/Probability.ipynb Discussion on Array Programming with Numpy : https://www.repository.cam.ac.uk/bitstream/handle/1810/315595/41586_2020_2649_MOESM1_ESM.pdf?sequence=2 On Random Number https://people.astro.umass.edu/~schloerb/ph281/Lectures/RandomNumbers/RandomNumbers.pdf Fluent Python by Luciano Ramalho. Chapter - ‘Iterables, Iterators, and Generators’ is relevant for many operations in matplotlib, pandas, numpy ‘The NumPy array: a structure for efficient numerical computation’ by Stefan van der Walt provides indepth understanding of Numpy Arrays https://hal.inria.fr/inria-00564007/document References Start Also check https://scipy-lectures.org/advanced/advanced_python/index.html#id2 297
  • 298. Ch Ch Ch Ch Ch Ch Ch DT Category Description Link Figures, subplots, gridspec Relationship between DPI and figure size https://stackoverflow.com/questions/47633546/relationship-between-dpi-and-figure-size/47639545#47639545 Change the figure size https://stackoverflow.com/questions/332289/how-do-you-change-the-size-of-figures-drawn-with- matplotlib/638443#638443 Using bbox_inches = ‘tight’ in plt.savefig() to ensure all artists are in saved output | behaviour of jupyter "inline" backend (%matplotlib inline) https://stackoverflow.com/questions/44642082/text-or-legend-cut-from-matplotlib-figure-on-savefig On Multiple Subplots https://actruce.com/en/object-oriented-way-of-using-matplotlib-4-multiple-subplots/ Combining different figures and putting in single subplot https://stackoverflow.com/questions/16748577/matplotlib-combine-different-figures-and-put-them-in-a-single-subplot- sharing-a?noredirect=1&lq=1 Brilliant Link on adding subplots - https://stackoverflow.com/questions/3584805/in-matplotlib-what-does-the-argument-mean-in-fig-add-subplot111?rq=1 Excellent link on using GridSpec https://www.python-course.eu/matplotlib_gridspec.php Usecase Scenarios of plt.cla, plt.clf, plt.close : https://stackoverflow.com/questions/8213522/when-to-use-cla-clf-or-close-for-clearing-a-plot-in-matplotlib? Fig patch, axes patch, zorder Superb Example on setting fc of fig patch and axes patch https://stackoverflow.com/questions/4581504/how-to-set-opacity-of-background-colour-of-graph-with-matplotlib Zorder demo : https://matplotlib.org/stable/gallery/misc/zorder_demo.html?highlight=zorder Ticks, tick labels On removing ticks https://stackoverflow.com/questions/12998430/remove-xticks-in-a-matplotlib-plot Rotating Axis Labels in Matplotlib https://www.pythoncharts.com/2019/05/17/rotating-axis-labels/ Tick Label Rotation and spacing https://stackoverflow.com/questions/43152502/how-can-i-rotate-xticklabels-in-matplotlib-so-that-the-spacing-between- each-xtic?noredirect=1&lq=1 Making tick label font size smaller https://stackoverflow.com/questions/6390393/matplotlib-make-tick-labels-font-size-smaller/11386056#11386056 Formatter for large tick values - https://dfrieds.com/data-visualizations/how-format-large-tick-values.html Discussion on tick labels zorder being above axes and patches whereas the grid lines below the axes : https://stackoverflow.com/questions/48327099/is-it-possible-to-set-higher-z-index-of-the-axis-labels Tick locator and formatter for timeseries without using matplotlib.dates https://stackoverflow.com/questions/33743394/ Time Series AR, MA and ARIMA Models in Python https://www.ritchievink.com/blog/2018/09/26/algorithm-breakdown-ar-ma-and-arima-models/ Practical Time Series Analysis - Prediction with Statistics & Machine Learning by Aileen Nielson Machine Learning for Time Series Forecasting with Python by Francesca Lazzeri Advanced Data Science and Analytics with Python by Jesus Rogel-Salazar Analysis of Financial Time Series, 3d Edition, by Ruey S. Tsay Hands-on Time Series Analysis with Python by B V Vishwas and A. Patel Data Science Revealed by Tshepo Chris Nokeri Time Series Related Books 298
  • 299. Ch Ch Ch Ch Ch Ch Ch DT Category Description Link Legend Go to reference for understanding the purport of Legend arguments https://stackoverflow.com/questions/4700614/how-to-put-the-legend-out-of-the-plot/4701285#4701285 On adding text artists in Legend https://stackoverflow.com/questions/27174425/how-to-add-a-string-as-the-artist-in-matplotlib-legend Drawing and Animating Shapes with Matplotlib https://nickcharlton.net/posts/drawing-animating-shapes-matplotlib.html Tuple Arguments in bbox_to_anchor https://stackoverflow.com/questions/39803385/what-does-a-4-element-tuple-argument-for-bbox-to-anchor-mean-in- matplotlib/39806180#39806180 Annotation resources - from matplotlib v3.4.2 • Adding value labels on a matplotlib bar chart • How to annotate each segment of a stacked bar chart • Stacked Bar Chart with Centered Labels • How to plot and annotate multiple data columns in a seaborn barplot • stack bar plot in matplotlib and add label to each section • How to add multiple annotations to a barplot • How to plot and annotate a grouped bar chart Font properties Using font dictionary : https://matplotlib.org/stable/gallery/text_labels_and_annotations/text_fontdict.html?highlight=font%20dictionary Fonts demo object oriented style : https://matplotlib.org/stable/gallery/text_labels_and_annotations/fonts_demo.html List of font families or name : https://stackoverflow.com/questions/18821795/ set([f.name for f in matplotlib.font_manager.fontManager.afmlist]) and set([f.name for f in matplotlib.font_manager.fontManager.ttflist]) [] Data Visualizations Ten Simple Rules for Better Figures https://journals.plos.org/ploscompbiol/article?id=10.1371/journal.pcbi.1003833 Excellent link on beautiful data viz http://www.randalolson.com/2014/06/28/how-to-make-beautiful-data-visualizations-in-python-with-matplotlib/ Superb data viz https://www.dunderdata.com/blog/create-a-bar-chart-race-animation-in-python-with-matplotlib A Dramatic tour through Python’s Visualisation landscape including ggplot2 and Altair https://dsaber.com/2016/10/02/a-dramatic-tour-through-pythons-data-visualization-landscape-including-ggplot-and- altair/ Superb link covering different kind of plots. Can serve as the base code which can be modified as per requirement https://www.machinelearningplus.com/plots/top-50-matplotlib-visualizations-the-master-plots-python/#14.-Area-Chart Superb blog on wide ranging topics related to data wrangling/viz/programming with Pandas, matplotlib and python - https://dfrieds.com/#dataviz Pie vs dot plots https://pierreh.eu/pie-vs-dots-powersystem/ Visualizing Data by William Cleveland [] Cleveland dot plots in excel https://mbounthavong.com/blog/2018/7/16/communicating-data-effectively-with-data-visualizations-part-9-cleveland- plots Styling the plots https://dfrieds.com/data-visualizations/style-plots-python-matplotlib.html Extensive coverage of code for multiple aspects of plotting https://queirozf.com/entries/matplotlib-pyplot-by-example 299
  • 300. Ch Ch Ch Ch Ch Ch Ch DT Category Description Link Pandas Plotting, Seaborn Legend in plotting with Pandas https://stackoverflow.com/questions/21988196/legend-only-shows-one-label-when-plotting-with-pandas Seaborn : https://michaelwaskom.medium.com/three-common-seaborn-difficulties-10fdd0cc2a8b Superb Article on using Matplotlib, pandas and seaborn together : https://towardsdatascience.com/matplotlib-seaborn-pandas-an-ideal-amalgamation-for-statistical-data-visualisation- f619c8e8baa3 Time Series in Seaborn : https://www.geeksforgeeks.org/creating-a-time-series-plot-with-seaborn-and-pandas/ On limitation of plotting time series data in Seaborn : https://stackoverflow.com/questions/22795348/plotting-time-series-data-with-seaborn Plotting time in python: https://stackoverflow.com/questions/1574088/plotting-time-in-python-with-matplotlib?rq=1 Time Series in Matplotlib : https://www.earthdatascience.org/courses/use-data-open-source-python/use-time-series-data-in-python/date-time- types-in-pandas-python/customize-dates-matplotlib-plots-python/ Comprehensive coverage of datetime in Pandas Time series : https://developers.arcgis.com/python/guide/part5-time-series-analysis-with-pandas/ Converting between datetime, timestamp and datetime64 https://stackoverflow.com/questions/13703720/converting-between-datetime-timestamp-and-datetime64? R ggplot2 An Introduction to ggplot2 by Joey Stanley https://joeystanley.com/downloads/171012-ggplot2_handout.pdf “ggplot2 Elegant Graphics for Data Analysis” by Hadley Wickham - Highly recommended for ggplot2 https://ggplot2-book.org/introduction.html “R Graphics E ssentials for Great Data Visualization” by Alboukadel KASSAMBAR Highly recommended for creating great graphics for the right data using either the ggplot2 package and extensions or the traditional R graphics. Books on Data Science with R Modern Data Science with R by Benjamin S. Baumer Daniel T. Kaplan Nicholas J. Horton Machine Learning Essentials by Alboukadel KASSAMBAR Application oriented Code Reference and R handbook for experienced and new users https://epirhandbook.com/ggplot-basics.html#labels Bar charts in R ggplot : http://bradleyboehmke.github.io/tutorials/barchart Manipulation of tabular data in R http://barryrowlingson.github.io/hadleyverse/ Comparison of viz tools in Python and R : https://shankarmsy.github.io/posts/exploringviz1.html Covering baseline R vocabulary and knowledge for the primary data wrangling processes. “Data Wrangling with R” by Bradley C. Boehmke “R for Data Science” by Hadley Wickham and Garrett Grolemund – Highly recommended https://r4ds.had.co.nz/introduction.html “Building a plot layer by layer” by Hadley Wickham - https://rpubs.com/hadley/ggplot2-layers geopandas Shapely documentation – Page No. 27 - https://geopandas.org/getting_started/introduction.html Creating another layer to take care of Nan values https://buildmedia.readthedocs.org/media/pdf/shapely/latest/shapely.pdf Labeling polygons in geopandas plotting https://stackoverflow.com/questions/38473257/how-can-i-set-a-special-colour-for-nans-in-my-plot Placement of colorbars using cax https://stackoverflow.com/questions/38899190/geopandas-label-polygons Fixing colorbars https://matplotlib.org/stable/gallery/subplots_axes_and_figures/colorbar_placement.html Setting single common colorbar for two subplots https://joseph-long.com/writing/colorbars/ Delhi Map Geopandas discussion https://stackoverflow.com/questions/13784201/matplotlib-2-subplots-1-colorbar Interesting Case study Airbnb Paris analysis https://stackoverflow.com/questions/63644131/how-to-use-geopandas-to-plot-latitude-and-longitude-on-a-more- detailed-map-with/63646936#63646936 References End 300