0% found this document useful (0 votes)
275 views352 pages

Anatomy of Matplotlib Architecture

The document provides an index and overview of topics to be covered in a review of the Matplotlib library. It lists the three layers of the Matplotlib architecture - the backend layer, artist layer, and scripting layer. It also lists several frequently used plotting functions and concepts that will be discussed, such as the Pyplot and OO interfaces, color maps, legends, and transformations.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
275 views352 pages

Anatomy of Matplotlib Architecture

The document provides an index and overview of topics to be covered in a review of the Matplotlib library. It lists the three layers of the Matplotlib architecture - the backend layer, artist layer, and scripting layer. It also lists several frequently used plotting functions and concepts that will be discussed, such as the Pyplot and OO interfaces, color maps, legends, and transformations.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

A COMPLETE REVIEW

Ø MATPLOTLIB ARCHITECTURE
Ø ANATOMY OF A MATPLOTLIB FIGURE
Ø PYPLOT INTERFACE
Ø OBJECT ORIENTED INTERFACE

PREPARED BY :
BHASKAR JYOTI ROY
bhaskarjroy1605@[Link]
REFERENCES :
• Matplotlib Release 3.4.2, May 08, 2021
• Mastering Matplotlib by Duncan M. McGreggor
• StackOverflow and others

1 DT Ch ChCh Ch Ch Ch Ch
INDEX – MATPLOTLIB API
• 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 DT Ch ChCh Ch Ch Ch Ch
INDEX – OO INTERFACE

• OO interface
• Create a Figure instance
• Use Figures to create one or more axes or subplot
• Demo of [Link]() 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 DT Ch ChCh Ch Ch Ch Ch
INDEX – OO INTERFACE
• 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

4 DT Ch ChCh Ch Ch Ch Ch
INDEX – OO INTERFACE

• 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

5 DT Ch ChCh Ch Ch Ch Ch
INDEX – OO INTERFACE

• 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

6 DT Ch ChCh Ch Ch Ch Ch
INDEX – END

• Epilogue
• Data Structures Overview
• Pseudo Random Number Generation (in Python, Numpy)
• Handling Date and Time
• Statistics with Python
• Appendix
• References

7 DT Ch ChCh Ch Ch Ch Ch
VISUAL INDEX – 01

8 DT Ch ChCh Ch Ch Ch Ch
VISUAL INDEX – 02

9 DT Ch ChCh Ch Ch Ch Ch
MATPLOTLIB INTRODUCTION A comprehensive library for creating static, animated, and
interactive visualizations in Python.

Widely used plotting library for the Python programming language STRUCTURE OF THE IMPORTANT PYTHON PACKAGES
FOR DATA SCIENCE Code
A core component of the scientific Python stack – a group of
scientific computing tools, alongwith Numpy, Scipy and IPython

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

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.

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 • ipython : : : For interactive work.
control • numpy : : : For working with vectors and arrays.
• scipy : : : All the essential scientific algorithms, including those for basic statistics.
libraries like pandas and Seaborn are “wrappers” over matplotlib - • matplotlib : : : The de-facto standard module for plotting and visualization.
providing high level API’s that allow access to a number of • pandas : : : Adds DataFrames (imagine powerful spreadsheets) to Python.
matplotlib's methods with less code. For instance, pandas’ • statsmodels : : : For statistical modeling and advanced analysis.
.plot()combines multiple matplotlib methods into a single method, • seaborn : : : For visualization of statistical data.
thus plotting in a few lines. • 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

10 DT Ch ChCh Ch Ch Ch Ch
TOPICS WE WILL COVER
• Overview of Data Structures
• Backend layer • Pseudo Random Number Generation
Three Layers of matplotlib
• Artist layer • Handling Date and Time
architecture • Statistics with Python
• Scripting layer
• Appendix – Matplotlib plot examples
• References
Anatomy of a figure

Artist Hierarchy

Pyplot API

Object oriented API - for control


and Customisation

11 DT Ch ChCh Ch Ch Ch Ch
MATPLOTLIB ARCHITECTURE

Logical Separation into Three Layers ü 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.
Matplotlib Hierarchy

ISOLATION OF THE COMPLEXITIES IN EACH LAYER

Scripting

Artist Artist

Backend Backend Backend


ü Backend layer is not ü Artist Layer knows ü Scripting Layer knows
aware of the Artist how to talk with the how to talk to both
and the Scripting Backend Layer. the Artist Layer and
layer. It knows its own the Backend Layer.
interfaces only.

*[Link]
*Matplotlib Release 3.4.2, May 08, 2021, Page No. 460
*Mastering Matplotlib by Duncan M. McGreggor, Pg no.15

12 DT Ch ChCh Ch Ch Ch Ch
RELATIONSHIP OF THE LAYERS TO
DATA AND THE FIGURE OBJECT FOR The user creates either the data that he/she wants
A GIVEN PLOT to plot or the functions that generate this data

This data is either created or loaded in the


scripting layer
Matplotlib Hierarchy

The data is transformed into various objects in


the artist layer; it is adjusted as scripted

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

*[Link]
*Matplotlib Release 3.4.2, May 08, 2021, Page No. 460
*Mastering Matplotlib by Duncan M. McGreggor, Pg no.15

13 DT Ch ChCh Ch Ch Ch Ch
DEEP DIVE INTO BACKEND LAYER
Backend Layer refers to matplotlib capabilities to render plots for
different use cases and output formats.

DIFFERENT USE CASES

Use Matplotlib interactively Some people run Jupyter


Matplotlib Hierarchy

from the python shell and have notebooks and draw inline
plotting windows pop up when plots for quick data analysis.
they type commands.

Others embed Matplotlib into Some people use Matplotlib in


graphical user interfaces like batch scripts to generate
PyQt or PyGObject to build postscript images from
rich applications. numerical simulations

Others run web application


servers to dynamically serve
up graphs.

*[Link]
*Matplotlib Release 3.4.2, May 08, 2021, Page No. 460
*Mastering Matplotlib by Duncan M. McGreggor, Pg no.15

14 DT Ch ChCh Ch Ch Ch Ch
DEEP DIVE INTO BACKEND LAYER
Backend Layer refers to matplotlib capabilities to render plots for
different use cases and output formats.

DIFFERENT OUTPUT FORMATS


VECTOR OR RASTER BASED
Matplotlib Hierarchy

Vector Graphics use clean Raster Graphics use pixels


lines and shapes that can be arranged in grid formation to
scaled to any size represent an image
*[Link]
*Matplotlib Release 3.4.2, May 08, 2021, Page No. 460
*Mastering Matplotlib by Duncan M. McGreggor, Pg no.15

15 DT Ch ChCh Ch Ch Ch Ch
DEEP DIVE INTO BACKEND LAYER
Backend Layer refers to matplotlib capabilities to render plots for
different use cases and output formats.

DIFFERENT OUTPUT FORMATS


VECTOR OR RASTER BASED
Renderer Output File Types Description
Matplotlib Hierarchy

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

*[Link]
*Matplotlib Release 3.4.2, May 08, 2021, Page No. 460
*Mastering Matplotlib by Duncan M. McGreggor, Pg no.15

16 DT Ch ChCh Ch Ch Ch Ch
DEEP DIVE INTO BACKEND LAYER
Backend Layer refers to matplotlib capabilities to render plots for
different use cases and output formats.

User Interface Backends


For use in
Matplotlib Hierarchy

• GTK 2.x and GTK 3.x


• wxWidgets
• Tk
• Qt4 and Qt5
• Mac OS X Cocoa

Hardcopy backends to
make static image files (PNG,
SVG, PDF, PS)
*[Link]
*Matplotlib Release 3.4.2, May 08, 2021, Page No. 460
*Mastering Matplotlib by Duncan M. McGreggor, Pg no.15

17 DT Ch ChCh Ch Ch Ch Ch
DEEP DIVE INTO ARTIST LAYER
The Artist layer constitutes the bulk of what matplotlib actually
does — the generation of the plots.
Matplotlib Hierarchy

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…

*[Link]
*Matplotlib Release 3.4.2, May 08, 2021, Page No. 460
*Mastering Matplotlib by Duncan M. McGreggor, Pg no.15

18 DT Ch ChCh Ch Ch Ch Ch
DEEP DIVE INTO ARTIST LAYER
The matplotlib artist Primitives
primitives are classes of
standard graphical
objects that are
supposed to be painted
on a figure's canvas. Circle
Line 2D
Matplotlib Hierarchy

Wedge

Fancybox Patch Polygon

text
Arrow
Path Patch

Rectangle Ellipse

*[Link]
*Matplotlib Release 3.4.2, May 08, 2021, Page No. 460 *Matplotlib Release 3.4.2,
*Mastering Matplotlib by Duncan M. McGreggor, Pg no.15 Chapter 18.36 [Link], Pg No. 2326

19 DT Ch ChCh Ch Ch Ch Ch
DEEP DIVE INTO ARTIST LAYER
The matplotlib artist Primitives – Patches Classes
primitives are classes of
standard graphical
objects that are
supposed to be painted
on a figure's canvas. Circle
Matplotlib Hierarchy

Wedge

Fancybox Patch Polygon

Arrow
Path Patch

Rectangle Ellipse

*[Link]
*Matplotlib Release 3.4.2, May 08, 2021, Page No. 460 *Matplotlib Release 3.4.2,
*Mastering Matplotlib by Duncan M. McGreggor, Pg no.15 Chapter 18.36 [Link], Pg No. 2326

20 DT Ch ChCh Ch Ch Ch Ch
DEEP DIVE INTO ARTIST LAYER
The matplotlib artist
primitives are classes of
standard graphical
objects that are Figure - Top level Artist, which
supposed to be painted holds all plot elements
on a figure's canvas.
Matplotlib Hierarchy

Containers offer a useful


abstraction to gather
primitives.

*[Link]
*Matplotlib Release 3.4.2, May 08, 2021, Page No. 460
*Mastering Matplotlib by Duncan M. McGreggor, Pg no.15

21 DT Ch ChCh Ch Ch Ch Ch
DEEP DIVE INTO ARTIST LAYER
The matplotlib artist Ellipse Collection
primitives are classes of
standard graphical
objects that are
supposed to be painted
on a figure's canvas.
Matplotlib Hierarchy

Containers offer a useful


abstraction to gather
primitives.

Collections are the classes


that provide for the
efficient drawing of large
*[Link] numbers of similar objects.
*Matplotlib Release 3.4.2, May 08, 2021, Page No. 460
*Mastering Matplotlib by Duncan M. McGreggor, Pg no.15

22 DT Ch ChCh Ch Ch Ch Ch
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

Containers offer a useful


abstraction to gather
primitives.

Collections are the classes


that provide for the
efficient drawing of large
*[Link] numbers of similar objects.
*Matplotlib Release 3.4.2, May 08, 2021, Page No. 460
*Mastering Matplotlib by Duncan M. McGreggor, Pg no.15

23 DT Ch ChCh Ch Ch Ch Ch
DEEP DIVE INTO ARTIST LAYER

The Standard Approach to Plotting


using Artists
Matplotlib Hierarchy

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

*[Link]
*Matplotlib Release 3.4.2, May 08, 2021, Page No. 460
*Mastering Matplotlib by Duncan M. McGreggor, Pg no.15

24 DT Ch ChCh Ch Ch Ch Ch
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 DT Ch ChCh Ch Ch Ch Ch
ANATOMY OF A FIGURE

Major tick Title


Legend
Minor tick

Major tick label Grid


Line
(line plot)

Y axis label
The Axes contains most of the
figure elements: Axis, Tick, Line2D, Markers
(scatter plot)
Text, Polygon, etc., and sets the Y axis label
coordinate system.

Figure can contain axes or subplots.


Spines
Figure is top level container for all Line
Figure Axes (line plot)
plot elements. It means the whole
window in user interface.
Minor tick label X axis label
• Matplotlib Release 3.4.2, May 08, 2021, Pg No. 7-11
X axis label
• [Link]
26 DT Ch ChCh Ch Ch Ch Ch
ANATOMY OF A FIGURE

Major tick Title


Legend
Minor tick

Major tick label Grid


Line
(line plot)

The Axes contains most of the


figure elements: Axis, Tick, Line2D, Markers
(scatter plot)
Text, Polygon, etc., and sets the Y axis label
coordinate system.

Figure can contain axes or subplots.


Spines
Figure is top level container for all Line
Figure Axes (line plot)
plot elements. It means the whole
window in user interface.
Minor tick label
• Matplotlib Release 3.4.2, May 08, 2021, Pg No. 7-11
X axis label
• [Link]
27 DT Ch ChCh Ch Ch Ch Ch
ANATOMY OF A FIGURE

Figure
Axes

X axis label

Y axis label

Major tick
Major tick label

Minor tick
The Axes contains most of the
figure elements: Axis, Tick, Line2D, Minor tick label
Text, Polygon, etc., and sets the Grid
coordinate system.
Spines
Figure can contain axes or subplots. Line Line
(line plot) (line plot)
Figure is top level container for all
Markers
plot elements. It means the whole (scatter plot)
window in user interface. Legend
Title
• Matplotlib Release 3.4.2, May 08, 2021, Pg No. 7-11
• [Link]
28 DT Ch ChCh Ch Ch Ch Ch
ANATOMY OF A FIGURE

Figure
Basically, everything you can see on the figure is an
artist (even the Figure, Axes, and Axis objects). Axes

X axis label
This includes Text objects, Line2D objects,
Y axis label
collections objects, Patch objects..
Major tick
When the figure is rendered, all of the artists are
Major tick label
drawn to the canvas.
Minor tick
Most Artists are tied to an Axes; such an Artist Minor tick label
cannot be shared by multiple Axes, or moved from
Grid
one to another.
Spines

Line Line
(line plot) (line plot)
Markers
(scatter plot)
Legend
Title
• Matplotlib Release 3.4.2, May 08, 2021, Pg No. 7-11
• [Link]
29 DT Ch ChCh Ch Ch Ch Ch
Let’s run down through the hierarchy
of Artists…

Representing Containers by yellow Containers


colored boxes.

Representing Primitives by Salmon


Primitives
colored boxes.

• Matplotlib Release 3.4.2, May 08, 2021, Pg No. 7-11


• [Link]
30 DT Ch ChCh Ch Ch Ch Ch
Figure

Figure is the Top Level container The figure keeps track of all the child Axes, a
smattering of ‘special’ artists (titles, figure legends,
etc), and the canvas
Figure can contain other Containers and Primitives.

Figure can contain Axes and subplots.

Artist Type Color Code

Containers

Primitives

• Matplotlib Release 3.4.2, May 08, 2021, Pg No. 7-11


• [Link]
31 DT Ch ChCh Ch Ch Ch Ch
Figure Axes

Rectangle

The figure keeps track of all the child Axes, a


Line2D smattering of ‘special’ artists (titles, figure legends,
etc), and the canvas

Text

Artist Type Color Code Image

Containers
Legend
Primitives

• Matplotlib Release 3.4.2, May 08, 2021, Pg No. 7-11


• [Link]
32 DT Ch ChCh Ch Ch Ch Ch
Figure Axes

Rectangle

Let us focus on Axes.


Line2D
Note the term ‘Axes‘ is not related to term ‘axis’.

Text
The Axes class is one of the most important.
It is the primary mover and shaker in the artist layer.

Artist Type Color Code Image The reason for this is simple—the Axes instances
are where most of the matplotlib objects go (both
Containers primitives and other containers)
Legend
Primitives

• Matplotlib Release 3.4.2, May 08, 2021, Pg No. 7-11


• [Link]
33 DT Ch ChCh Ch Ch Ch Ch
Figure Axes XAxis

YAxis

“the Axes instances are where most of


Line2D
the matplotlib objects go (both
primitives and other containers)“
Rectangle

PolyCollection

Artist Type Color Code Text

Containers
Legend
Primitives

• Matplotlib Release 3.4.2, May 08, 2021, Pg No. 7-11


• [Link]
34 DT Ch ChCh Ch Ch Ch Ch
Figure Axes XAxis

YAxis

In addition to the creation of primitives,


the methods of axes class can prepare Line2D
the supplied data to :
§ create primitives such as lines and
shapes Rectangle
§ add primitives to the appropriate
containers, and
§ draw artists when called by some PolyCollection
other objects.
Artist Type Color Code Text

Containers
Legend
Primitives

• Matplotlib Release 3.4.2, May 08, 2021, Pg No. 7-11


• [Link]
35 DT Ch ChCh Ch Ch Ch Ch
Figure Axes XAxis

YAxis

In addition to the creation of primitives,


the methods of axes class can prepare Line2D
the supplied data to :
§ create primitives such as lines and
shapes Rectangle
§ add primitives to the appropriate
containers, and
§ draw artists when called by some PolyCollection
other objects.
Artist Type Color Code Text

Containers
Legend
Primitives

• Matplotlib Release 3.4.2, May 08, 2021, Pg No. 7-11


• [Link]
36 DT Ch ChCh Ch Ch Ch Ch
XAxis Label
Figure Axes XAxis

XAxis Tick
YAxis

Let’s focus on the two important containers YAxis Label Tick Major Line
inside Axes : the XAxis and YAxis
YAxis Tick
The Axis instances handle the drawing of : Tick Minor Line
• tick lines Tick Major Line
• grid lines
• tick labels Tick Major Label
• axis label Tick Minor Line

Tick Minor Label


Artist Type Color Code
Tick Major Label

Containers Tick Minor Label Grid Line

Primitives
Grid Line
• Matplotlib Release 3.4.2, May 08, 2021, Pg No. 7-11
• [Link]
37 DT Ch ChCh Ch Ch Ch Ch
XAxis Label
Figure Axes XAxis

XAxis Tick
YAxis

The Axis instances also store Locator and YAxis Label Tick Major Line
Formatter instances which control :
• where the ticks are placed and YAxis Tick
• how they are represented as strings. Tick Minor Line
Tick Major Line

Tick Major Label


Tick Minor Line

Tick Minor Label


Artist Type Color Code
Tick Major Label

Containers Tick Minor Label Grid Line

Primitives
Grid Line
• Matplotlib Release 3.4.2, May 08, 2021, Pg No. 7-11
• [Link]
38 DT Ch ChCh Ch Ch Ch Ch
Having reviewed the Artists Hierarchy…

We shall move on to the “Scripting Layer”

39 DT Ch ChCh Ch Ch Ch Ch
SCRIPTING LAYER – DUAL INTERFACE Scripting Layer is the User Facing Interface from where User
TO MATPLOTLIB API can make calls to the Artist layer and backend layer.

Two ways to use Matplotlib:


Matplotlib Hierarchy

• 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")

40 DT Ch ChCh Ch Ch Ch Ch
PYPLOT INTERFACE TO MATPLOTLIB Pyplot Interface is a Stateful Interface
API styled on MATLAB

import [Link] as plt


[Link] is a collection of functions that
[Link]([1, 2, 3, 4])
make matplotlib work like MATLAB.
[Link]('some numbers’)
[Link]()
• Each pyplot function makes some change to a figure.

• Various states are preserved across function calls, so Plot y versus x as lines and/or markers
that pyplot keeps track of things like the current Set the label for the y-axis
figure and plotting area.
Display all open figures
• All plotting functions apply to the current axes.

41 DT Ch ChCh Ch Ch Ch Ch
FREQUENTLY USED [Link] FUNCTIONS For the Figure, Axes, Axis

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.
[Link]

The [Link]() method allows you to set the x and y limits with a single call,
by passing a list that specifies [xmin, xmax, ymin, ymax].

42 DT Ch ChCh Ch Ch Ch Ch
FREQUENTLY USED [Link] 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
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.

[Link]

43 DT Ch ChCh Ch Ch Ch Ch
FREQUENTLY USED [Link] FUNCTIONS For the Figure, Axes, Axis

Functions Description
axhline([y, xmin, xmax]) Add a horizontal line across the axis.
Line,
axvline([x, ymin, ymax]) Add a vertical line across the Axes.
Span
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.
[Link]

44 DT Ch ChCh Ch Ch Ch Ch
FREQUENTLY USED [Link] FUNCTIONS For Charting

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.
[Link]

45 DT Ch ChCh Ch Ch Ch Ch
FREQUENTLY USED [Link] FUNCTIONS For Charting

Functions Description
hlines(y, xmin, xmax[, colors, linestyles, ...]) Plot horizontal lines at each y from xmin to xmax.
Line, vlines(x, ymin, ymax[, colors, linestyles, ...]) Plot vertical lines at each x from ymin to ymax.
Area 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.

[Link]

46 DT Ch ChCh Ch Ch Ch Ch
FREQUENTLY USED [Link] FUNCTIONS Helper Methods

Functions Description
gcf() Get the current figure.
gca(\*\*kwargs) Get the current Axes, creating one if necessary.
Current gci() Get the current colorable artist.
Axes, sca() Set the current Axes to ax and the current Figure to the parent of ax.
Figure 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.
Inspecting, get(obj, \*args, \*\*kwargs) Return the value of an Artist's property, or print all of them.
Changing setp(obj, \*args, \*\*kwargs) Set one or more properties on an Artist, or list allowed values.
Properties
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
[Link]

47 DT Ch ChCh Ch Ch Ch Ch
FREQUENTLY USED [Link] FUNCTIONS
For Colormaps,
Pseudo Colorplots

Functions Description Functions Description


pcolor(\*args[, shading, alpha, Create a pseudocolor plot with a non- autumn() Set the colormap to 'autumn'.
norm, cmap, ...]) regular rect- angular grid. bone() Set the colormap to 'bone'.
cool() Set the colormap to 'cool'.
copper() Set the colormap to 'copper'.
pcolormesh(\*args[, alpha, Create a pseudocolor plot with a non-
flag() Set the colormap to 'flag'.
norm, cmap, ...]) regular rect- angular grid.
gray() Set the colormap to 'gray'.
hot() Set the colormap to 'hot'.
imread(fname[, format]) Read an image from a file into an array. hsv() Set the colormap to 'hsv'.
inferno() Set the colormap to 'inferno'.
imsave(fname, arr, \*\*kwargs) Save an array as an image file. jet() Set the colormap to 'jet'.
magma() Set the colormap to 'magma'.
imshow(X[, cmap, norm, aspect, Display data as an image, i.e., on a 2D nipy_spectral() Set the colormap to 'nipy_spectral'.
...]) regular raster. pink() Set the colormap to 'pink'.

[Link]

48 DT Ch ChCh Ch Ch Ch Ch
FREQUENTLY USED [Link] FUNCTIONS For Multiple plots, Layout Customization

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, Create a subplot at a specific location inside a reg- ular
fig]) 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.
[Link]

49 DT Ch ChCh Ch Ch Ch Ch
Subplot function to add/change current axes
Code Snippet
Pyplot function on current axes
names = ['group_a', 'group_b', 'group_c']
values = [1, 10, 100]
[Link] adds axes to current figure.

[Link](figsize=(9, 3)) Everytime it is called, it resets the current axes.


Subsequent pyplot functions will be applicable to current axes.
[Link](131)
[Link](names, values) 131 means in a grid of one row by three columns,
the subplot at index position 1 is set as the current axes.

A Bar chart is plotted on the current axes.

[Link](132) The current axes is reset.


[Link](names, values) 132 means in a grid of one row by three columns,
the subplot at index position 2 is set as the current axes.

A Scatter chart is plotted on the current axes.


[Link](133)
[Link](names, values) Current Axes is reset and a Line plot is drawn.
[Link]('Categorical Plotting’)
Centered suptitle is set in the Figure.
[Link]('Categorical [Link]’, \
bbox_inches = "tight", \ Figure is saved as png file.
pad_inches = 0.15, dpi=300) Figure is displayed in user screen
[Link]()
50 DT Ch ChCh Ch Ch Ch Ch
Subplot function to add/change current axes
Code Snippet
names = ['group_a', 'group_b', 'group_c']
Pyplot function on current axes
values = [1, 10, 100]

[Link](figsize=(9, 3))

[Link](131)
[Link](names, values)

[Link](132)
[Link](names, values)

[Link](133)
[Link](names, values)
[Link]('Categorical Plotting’)

[Link]('Categorical [Link]’, \
bbox_inches = "tight", \
pad_inches = 0.15, dpi=300)
[Link]()

51 DT Ch ChCh Ch Ch Ch Ch
FREQUENTLY USED [Link] FUNCTIONS For Fine Tuning your Plot Appearance
Customizing Matplotlib Configurations

CONFIGURATIONS
Functions Description
rc(group, \*\*kwargs) Set the current rcParams. group is the grouping for the rc, e.g., for
Change [Link] the group is lines, for [Link], the group is axes, and
rcParams so on. Group may also be a list or tuple of group names, e.g., (xtick, ytick).
settings 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.

*Matplotlib Release 3.4.2, May 08, 2021, Customizing Matplotlib with style sheets and rcParams, Pg No.84, 25

*rc refers to Matplotlib runtime configurations

import matplotlib as mpl


Inspect
[Link]['[Link]']

import matplotlib as mpl


Change
[Link]['[Link]’] = 2

52 DT Ch ChCh Ch Ch Ch Ch
FREQUENTLY USED [Link] FUNCTIONS “rcParams” – dict like Global variable
Stores settings

Dictionary Like Variable that stores rc settings and Global to Matplotlib package
Functions Description
Settings
Stored
[Link] Stores the default rcParams.
[Link] Stores the rcParams in original matplotlibrc file
[Link] Stores the current rcParams

*Matplotlib Release 3.4.2, May 08, 2021, Customizing Matplotlib with style sheets and rcParams, Pg No.86

53 DT Ch ChCh Ch Ch Ch Ch
rcParams function for global changes

Code Snippet for customizing configurations Before Customizing


Import [Link] as plt
from matplotlib import cycler
colors = cycler('color’, ['#EE6666', '#3388BB’,
'#9988DD', '#EECC55’, '#88BB44’,
'#FFBBBB’])

[Link]('axes', facecolor='#E6E6E6’,
edgecolor='none', axisbelow=True,
grid=True,
prop_cycle=colors) After Customizing

[Link]('grid', color='w', linestyle='solid’)

[Link]('xtick', direction='out', color='gray’)


[Link]('ytick', direction='out', color='gray’)

[Link]('patch', edgecolor='#E6E6E6’)
[Link]('lines', linewidth=2)

*Python Data Science Handbook by Jake VanderPlas, Page No.284

54 DT Ch ChCh Ch Ch Ch Ch
rcParams dictionary variables to revert
back settings
Code Snippet
Before Reverting Back
[Link]([Link])
OR
[Link]([Link])

After restoring defaults

*[Link]

55 DT Ch ChCh Ch Ch Ch Ch
rcParams dictionary variables to revert
back settings
Code Snippet

[Link]([Link])
OR
[Link]([Link])

Best Practices
import matplotlib as mpl
my_params = [Link] Store current rcParams as a variable prior to any change.

# apply some change to the rcparams here Do some changes in rcParams, plot using new settings

[Link](my_params)
Restore the rcParams with the earlier stored variable.

*[Link]

56 DT Ch ChCh Ch Ch Ch Ch
FREQUENTLY USED [Link] FUNCTIONS Using your
For Fine Tuning StyleSheets
Plot Appearance

STYLE SHEETS
[Link]('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 [Link] to get the available style names list
Use [Link] to apply style temporarily.
*Matplotlib Release 3.4.2, May 08, 2021, Customizing Matplotlib with style sheets and rcParams, Pg No.84

[Link]

57 DT Ch ChCh Ch Ch Ch Ch
All Available Stylesheets
Code

Code Snippet for showcasing all styles


fig = [Link](dpi=100, figsize=(24, 20),
tight_layout=True)

available = ['default'] + [Link]


x = ['Product A','Product B', 'Product C']
y = [100,130,200]

for i, style in enumerate(available):


with [Link](style):
ax = fig.add_subplot(6,5 , i + 1)
[Link](x,y)
ax.set_title(style)

[Link]('All [Link]’,
bbox_inches = "tight",
pad_inches = 0.05, dpi=300)

[Link]()

58 DT Ch ChCh Ch Ch Ch Ch
Styles in this slide
'default’
Zooming in - First set of 6# Stylesheets
'Solarize_Light2'
Styles on '_classic_test_patch'
Right '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 '

59 DT Ch ChCh Ch Ch Ch Ch
Styles in this slide
'default’
Zooming in - Second set of 6# Stylesheets
'Solarize_Light2'
'_classic_test_patch'
'bmh'
'classic'
'dark_background '
'fast'
'fivethirtyeight'
Styles on 'ggplot'
Right '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 '

60 DT Ch ChCh Ch Ch Ch Ch
Styles in this slide
'default’
Zooming in - Third set of 6# Stylesheets
'Solarize_Light2'
'_classic_test_patch'
'bmh'
'classic'
'dark_background '
'fast'
'fivethirtyeight'
'ggplot'
'grayscale'
'seaborn'
'seaborn- bright'
'seaborn-colorblind'
'seaborn-dark'
Styles on 'seaborn-dark-palette'
Right 'seaborn-darkgrid'
'seaborn-deep'
'seaborn-muted'
'seaborn-notebook'
'seaborn-paper'
'seaborn-pastel'
'seaborn-poster'
'seaborn-talk'
'seaborn-ticks'
'seaborn-white'
'seaborn-whitegrid'
'tableau-colorblind10 '

61 DT Ch ChCh Ch Ch Ch Ch
Styles in this slide
'default’
Zooming in - Fourth set of 6# Stylesheets
'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'
Styles on 'seaborn-pastel'
Right 'seaborn-poster'
'seaborn-talk'
'seaborn-ticks'
'seaborn-white'
'seaborn-whitegrid'
'tableau-colorblind10 '

62 DT Ch ChCh Ch Ch Ch Ch
Styles in this slide
'default’
Zooming in - Fifth set of 3# Stylesheets
'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'
Styles on
'seaborn-whitegrid'
Right
'tableau-colorblind10 '

63 DT Ch ChCh Ch Ch Ch Ch
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
ENTRY POINT INFLECTION POINT ACTIONABLES

ü Consider Pyplot Interface as ü But to gain finely grained ü We will take it step by step
a brief interlude control over every aspect ü Review matplotlib landscape
ü It’s a good starting point of plot ü Build on our learnings
ü Hands-on and convenient ü OO is the way ü Partly incremental and partly
undertake few leaps
ü This is an attempt to bring a structure, build intuition ü Understand Matplotlib API
BROADER to approach the plotting into breakable tasks ü Understand OO interface and settle on
SPECIFIC
GOAL ü The goal is also to be the go-to reference guide for mix of OO and pyplot
OUTCOMES
the major part of business analytics related EDA ü Eventually use OO interface seamlessly
with Seaborn, Pandas, GeoPandas

64 DT Ch ChCh Ch Ch Ch Ch
OO APPROACH – A COMPLETE RUNDOWN PREPARING THE DATA
data = {'Water':465,
'Alchoholic Beverages':271,
Create a Figure instance
1
'Carbonated soft drinks': 224,
'Milk and Dairy products': 260,
'New Drinks': 75, CODE SCRIPTING
'Fruits and Vegetable Juices': 86}
import [Link] as plt
x = list([Link]())
%matplotlib inline
y = list([Link]())
Use Figure to create one or more Axes or
2 #Creating figure and axes instances
fig, ax = [Link](figsize = (10,5), frameon = True) PLOTTING OUTPUT
Subplot #Creating barplot using [Link]() method
colors = [ [Link].Dark2(x) for x in [Link](0, 1, len(x))]
[Link](x,y, color = colors)

Use Axes Helper Methods to add artists to #Setting y limits

3 yupperlimit = ax.get_ylim()[1]*1.1

respective containers within the Axes ax.set_ylim(0,yupperlimit)

#Making x-tick lines invisible


object. [ ticks.set_visible(False] for ticks in ax.get_xticklines() ]

#Rotating and aligning the xtick labels


for labels in ax.get_xticklabels():
[Link](size = 12, rotation = 15, rotation_mode = 'anchor’, ha = 'right')
Use Axes Helper Methods to access x-axis
4 # Adding Annotations to place data labels

and y-axis tick, tick labels and axis labels. for p in [Link]:
[Link](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)

Use Axes Helper Methods (Accessors) to


5 #Setting the properties of rectangular patch in axes and figure
[Link](facecolor = 'white')
access artists through attributes. [Link](facecolor = 'white’, edgecolor = 'grey', lw = 4)
[Link](True)

#Setting axes title


StrTitle = 'Consumption of Packed beverages in billion liters,\n by Beverage Type, Global, 2019’
Set/Modify the Artist properties after ax.set_title(StrTitle, size = 16)

6
accessing the artists
#Adding Legend labels and handles
[p.set_label(x[i]) for i,p in zip(range(len(x)), [Link])]
[Link](bbox_to_anchor = (1.04,.5), loc="upper left",borderaxespad=0)

#Saving the figure locally


[Link]('Consumption_2.png', dpi=300, format='png', bbox_inches='tight’)
Add Legend and Set Title to the Axes
7

65 DT Ch ChCh Ch Ch Ch Ch
OO APPROACH – PREPARING THE DATA PREPARING THE DATA
data = {'Water':465,
Create a Figure instance 'Alchoholic Beverages':271,
1 'Carbonated soft drinks': 224,
'Milk and Dairy products': 260,
'New Drinks': 75,
'Fruits and Vegetable Juices': 86}
Use Figure to create one or more Axes or x = list([Link]())
2 y = list([Link]())
Subplot

Use Axes Helper Methods to add artists to


3
respective containers within the Axes
object.

Use Axes Helper Methods to access x-axis


4
and y-axis tick, tick labels and axis labels.

Use Axes Helper Methods (Accessors) to


5
access artists through attributes.

Set/Modify the Artist properties after


6
accessing the artists

Add Legend and Set Title to the Axes


7

66 DT Ch ChCh Ch Ch Ch Ch
Check slide
OO APPROACH – CODE SCRIPTING import [Link] as plt
%matplotlib inline

1 Create a Figure instance #Creating figure and axes instances


fig, ax = [Link](figsize = (10,5), frameon = True)

#Creating barplot using [Link]() method


colors = [ [Link].Dark2(x) for x in [Link](0, 1, len(x))] #Accessing colors from colormap
Use Figure to create one or more Axes or
2 [Link](x,y, color = colors)
Subplot
#Setting y limits
yupperlimit = ax.get_ylim()[1]*1.1
ax.set_ylim(0,yupperlimit)
Use Axes Helper Methods to add artists to
3
respective containers within the Axes #Making x-tick lines invisible
[ ticks.set_visible(False] for ticks in ax.get_xticklines() ]
object.
#Rotating and aligning the xtick labels
for labels in ax.get_xticklabels():
Use Axes Helper Methods to access and [Link](size = 12, rotation = 15, rotation_mode = 'anchor’, ha = 'right')
4
set x-axis and y-axis tick, tick labels and # Adding Annotations to place data labels
axis labels. for p in [Link]:
[Link](x = p.get_x()+p.get_width()/2,
y = ax.get_ylim()[1]*0.01+ p.get_height(),
s = p.get_height(),
Use Axes Helper Methods (Accessors) to
5 ha = 'center', size = 12)
access artists through attributes. #Setting the properties of rectangular patch in axes and figure
[Link](facecolor = 'white’) #OO style to set properties
[Link]([Link], facecolor = 'white’, edgecolor = 'grey', lw = 4). #Pyplot style to set properties
Set/Modify the Artist properties after
6 [Link](True)
accessing the artists. # Setting axes title
StrTitle = 'Consumption of Packed beverages in billion liters,\n by Beverage Type, Global, 2019’
ax.set_title(StrTitle, size = 16)
Add Legend and Set Title to the Axes
7 # Adding Legend labels and handles
[p.set_label(x[i]) for i,p in zip(range(len(x)), [Link])]
[Link](bbox_to_anchor = (1.04,.5), loc="upper left",borderaxespad=0)

[Link]('Consumption_2.png', dpi=300, format='png', bbox_inches='tight’)


67 DT Ch ChCh Ch Ch Ch Ch
OO APPROACH – PLOTTING OUTPUT

1 Create a Figure instance

PLOTTING OUTPUT Check slide


Use Figure to create one or more Axes or
2
Subplot

Use Axes Helper Methods to add artists to


3
respective containers within the Axes
object.

Use Axes Helper Methods to access and


4
set x-axis and y-axis tick, tick labels and
axis labels.

Use Axes Helper Methods (Accessors) to


5
access artists through attributes.

Set/Modify the Artist properties after


6
accessing the artists.

Add Legend and Set Title to the Axes


7

68 DT Ch ChCh Ch Ch Ch Ch
OO APPROACH – A DEEP DIVE

Create a Figure instance CREATING FIGURE AND AXES


1 Method Description Page No.
fig, ax = [Link]() Instantiate a figure and axes 2650
Use Figure to create one or more Axes fig = [Link]() Instantiate a figure 2528
2
or Subplot
METHODS TO CREATE NEW SUBPLOTS
Use Axes Helper Methods to add [Link](nrows, ncols) Primary function to create and place all axes on the figure 2117
3 at once by dividing figure into grids
artists to respective containers within fig.add_subplot( (nrows, ncols, index) Figure method to add axes by specifying the index in a grid 2077,
the Axes object. layout pattern 2117
fig.add_gridspec( nrows, ncols) Figure method to return gridspec that specifies the 2076,
geometry of the grid that a subplot will be placed. 2174
Use Axes/Axis Helper Methods to
4 fig.add_subplot(SubplotSpec) Figure method to add axes by specifying the location of 2076
access and set x-axis and y-axis tick, subplot in a GridSpec 2174
tick labels and axis labels. fig.add_axes(rect) Figure method to add axes at any location within the Figure 2074
[Link](axes object) Figure method to delete an axes
[Link]() Axes method to remove an axes
Use Axes Methods (Accessors) to
5
access artists. CUSTOMIZE SUBPLOT
fig.subplots_adjust(left, bottom, right, Adjust the subplot layout parameters. 2119
top, wspace, hspace )
Set/Modify the Artist properties after
6 tight_layout(pad, h_pad, w_pad, rect) Adjust the padding between and around subplots. 2124
accessing the artists 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 2080-
Add Legend, Annotations if any and
7 fig.align_xlabels() label alignment is being done automatically 2082
Set Title to the Axes fig.align_ylabels()

69 DT Ch ChCh Ch Ch Ch Ch
DEMO OF [Link]()

Create a Figure instance #Using [Link] to place multiple


1 axes in the figure
import [Link] as plt
[Link]('seaborn’)
Use Figure to create one or more Axes
2 %matplotlib inline
or Subplot
fig = [Link]()
Use Axes Helper Methods to add ax = [Link](2,2)
3
artists to respective containers within
the Axes object.
DEMO OF [Link]() AND ENUMERATE
#Using [Link]
Use Axes/Axis Helper Methods to
4 import [Link] as plt
access and set x-axis and y-axis tick, [Link]('seaborn')
tick labels and axis labels. %matplotlib inline

rows, cols = 2,2


Use Axes Methods (Accessors) to fig = [Link]()
5 ax = [Link](rows,cols,
access artists. sharex = True,sharey = True)

Set/Modify the Artist properties after #Adding text by looping through all the
6 subplots/axes
accessing the artists ax_iter = [Link]()
for i,axes in enumerate(ax_iter):
[Link](0.5, 0.5,
Add Legend, Annotations if any and
7 f'subplot{rows,cols,i}',
Set Title to the Axes ha='center', va='center',
size=20, color="steelblue")
[Link](False)
70 DT Ch ChCh Ch Ch Ch Ch
Create a Figure instance #Using add_subplots to add axes
1 import [Link] as plt
ax1 [Link]('seaborn’)
%matplotlib inline
Use Figure to create one or more Axes
2
or Subplot ax2 fig = [Link]()
ax1 = fig.add_subplot(2,3,1)
Use Axes Helper Methods to add ax2 = fig.add_subplot(2, 3, (3,6))
3
artists to respective containers within Note : Indexing is 1 based.
the Axes object.

Use Axes/Axis Helper Methods to


4 #Using Gridspec with add_subplots
access and set x-axis and y-axis tick,
import [Link] as gridspec
ax1
tick labels and axis labels.
import [Link] as plt

Use Axes Methods (Accessors) to


5
ax3
fig = [Link](figsize=(12, 8))
access artists. spec = [Link](nrows = 2,
ncols = 2,

6 Set/Modify the Artist properties after


accessing the artists
ax2 wspace = 0.2,
hspace=0.1)

ax1 = fig.add_subplot(spec[0, 0])


Add Legend, Annotations if any and ax2 = fig.add_subplot(spec[1, 0])
7
Set Title to the Axes ax3 = fig.add_subplot(spec[:, 1])
Note : Indexing is 0 based.

71 DT Ch ChCh Ch Ch Ch Ch
Create a Figure instance #Using fig.add_axes() to add subplots
1 import [Link] as plt
[Link]()
fig = [Link]()
Use Figure to create one or more Axes
2
or Subplot ax1 = [Link]()
ax2 = fig.add_axes([0.3,0.7,0.4,0.1])
Use Axes Helper Methods to add ax2.tick_params(labelsize=8, length=0)
3
artists to respective containers within
*Note : This method used to Add an axes at
the Axes object.
position rect[left, bottom,width, height]
where all quantities are in fractions of figure
Use Axes/Axis Helper Methods to width and height.
4
access and set x-axis and y-axis tick,
tick labels and axis labels.

Use Axes Methods (Accessors) to


5
access artists.

Set/Modify the Artist properties after


6
accessing the artists

Add Legend, Annotations if any and


7
Set Title to the Axes

72 DT Ch ChCh Ch Ch Ch Ch
Create a Figure instance AXES HELPER METHODs TO CREATE PRIMITIVES
1
Axes helper method Artist Container
Use Figure to create one or more Axes
2 annotate - text annotations Annotation [Link]
or Subplot bar - bar charts Rectangle [Link] and Barcontainer
errorbar - error bar plots Line2D and Rectangle [Link] and [Link]
Use Axes Helper Methods to add fill - shared area Polygon [Link]
3 hist - histograms Rectangle [Link]
artists to respective containers within
the Axes object. imshow - image data AxesImage [Link]
legend - Axes legends Legend [Link]()
plot - xy plots Line2D [Link]
Use Axes/Axis Helper Methods to scatter - scatter charts PolyCollection [Link]
4
access and set x-axis and y-axis tick, text - text Text [Link]
tick labels and axis labels. ü To get a list of all containers in the axes, use [Link]
ü To get list of all artists in the axes, use ax.get_children()
Use Axes Methods (Accessors) to
5
access artists.
CONTAINER
PRIMITIVES
Set/Modify the Artist properties after
6 AXES HELPER
accessing the artists METHOD
PRIMITIVES
Add Legend, Annotations if any and CONTAINER
7
Set Title to the Axes

*Check Artist Tutorial


73 DT Ch ChCh Ch Ch Ch Ch
Create a Figure instance AXES METHODS TO CREATE DIFFERENT PLOTS AND TEXT (1/3)
1
Page
Category Method Description
No.
Use Figure to create one or more Axes
2 [Link] Plot y versus x as lines and/or markers. 1241
or Subplot [Link] Plot y versus x as lines and/or markers with attached errorbars. 1241
[Link] A scatter plot of y vs. 1241
Use Axes Helper Methods to add Axes.plot_date Plot coercing the axis to treat floats as dates. 1241
3
artists to respective containers within [Link] Make a step plot. 1241
the Axes object. [Link] Make a plot with log scaling on both the x and y axis. 1241
[Link] Make a plot with log scaling on the x axis. 1241
[Link] Make a plot with log scaling on the y axis. 1241
Use Axes/Axis Helper Methods to
4 Axes.fill_between Fill the area between two horizontal curves. 1241
access and set x-axis and y-axis tick, Axes.fill_betweenx Fill the area between two vertical curves. 1241
tick labels and axis labels. Basic [Link] Make a bar plot. 1241
[Link] Make a horizontal bar plot. 1241
Use Axes Methods (Accessors) to Axes.bar_label Label a bar plot. 1241
5
access artists. [Link] Create a stem plot. 1242
[Link] Plot identical parallel lines at the given positions. 1242
[Link] Plot a pie chart. 1242
Set/Modify the Artist properties after
6 [Link] Draw a stacked area plot. 1242
accessing the artists Axes.broken_barh Plot a horizontal sequence of rectangles. 1242
[Link] Plot vertical lines at each x from ymin to ymax. 1242
Add Legend, Annotations if any and [Link] Plot horizontal lines at each y from xmin to xmax. 1242
7
Set Title to the Axes [Link] Plot filled polygons. 1242

74 DT Ch ChCh Ch Ch Ch Ch
Create a Figure instance AXES METHODS TO CREATE DIFFERENT PLOTS AND TEXT (2/3)
1
Page
Category Method Description
No.
Use Figure to create one or more Axes [Link] Add a horizontal line across the axis. 1296
2 [Link] Add a horizontal span (rectangle) across the Axes. 1296
or Subplot Spans [Link] Add a vertical line across the Axes. 1296
[Link] Add a vertical span (rectangle) across the Axes. 1296
Use Axes Helper Methods to add [Link] Add an infinitely long straight line. 1296
3 Spectral [Link] Plot the cross correlation between x and y. 1307
artists to respective containers within [Link] Make a box and whisker plot. 1334
the Axes object. Statistics
[Link] Make a violin plot. 1334
[Link] Drawing function for violin plots. 1334
[Link] Drawing function for box and whisker plots. 1334
Use Axes/Axis Helper Methods to [Link] Make a 2D hexagonal binning plot of points x, y. 1344
4
access and set x-axis and y-axis tick, [Link] Plot a histogram. 1344
Binned Axes.hist2d Make a 2D histogram plot. 1344
tick labels and axis labels. [Link] A stepwise constant function as a line with bounding edges 1344
or a filled plot.
Use Axes Methods (Accessors) to [Link] [Link](self, text, xy, *args, **kwargs) Annotate the 1399
5 point xy with text text.
access artists. [Link] Annotate the point xy with text text. 1399
[Link] Add text to the Axes. 1399
[Link] Add a table to an Axes. 1399
Set/Modify the Artist properties after
6 Text and
Axes.inset_axes Add an arrow to the Axes. 1399
accessing the artists annotations
Axes.indicate_inset Add a child inset Axes to this existing Axes. 1399
Axes.indicate_inset_z Add an inset indicator to the Axes. 1399
oom
Add Legend, Annotations if any and
7 Axes.secondary_xaxis Add an inset indicator rectangle to the Axes based on the 1399
Set Title to the Axes axis limits for an inset_ax and draw connectors between
inset_ax and the rectangle.
Axes.secondary_yaxis Add a second x-axis to this Axes. 1399

75 DT Ch ChCh Ch Ch Ch Ch
Create a Figure instance AXES METHODS TO CREATE DIFFERENT PLOTS AND TEXT (3/3)
1
Page
Category Method Description
No.
Use Figure to create one or more Axes
2 [Link] Display data as an image, i.e., on a 2D regular raster. 1369
or Subplot [Link] Plot the values of a 2D matrix or array as color-coded image. 1369
[Link] Create a pseudocolor plot with a non-regular rectangular grid. 1369
2D Arrays
[Link] Create a pseudocolor plot with a non-regular rectangular grid. 1369
Use Axes Helper Methods to add
3 [Link] Create a pseudocolor plot with a non-regular rectangular grid. 1369
artists to respective containers within [Link] Plot the sparsity pattern of a 2D array. 1369
the Axes object. [Link]

Use Axes/Axis Helper Methods to


4
access and set x-axis and y-axis tick,
tick labels and axis labels.

Use Axes Methods (Accessors) to


5
access artists.

Set/Modify the Artist properties after


6
accessing the artists

Add Legend, Annotations if any and


7
Set Title to the Axes

76 DT Ch ChCh Ch Ch Ch Ch
[Link] [Link]
[Link] [Link]
Create a Figure instance
1 Code Code

Use Figure to create one or more Axes


2
or Subplot

Use Axes Helper Methods to add


3
artists to respective containers within
the Axes object.

Use Axes/Axis Helper Methods to


4 [Link]
access and set x-axis and y-axis tick, Code

tick labels and axis labels. Code

Use Axes Methods (Accessors) to


5
access artists.

Set/Modify the Artist properties after


6
accessing the artists

Add Legend, Annotations if any and


7
Set Title to the Axes [Link]
[Link] [Link]
[Link] Barcontainer
77 DT Ch ChCh Ch Ch Ch Ch
Code

Create a Figure instance


1

Use Figure to create one or more Axes


2
or Subplot

Use Axes Helper Methods to add


3
artists to respective containers within
the Axes object.

Use Axes/Axis Helper Methods to


4
access and set x-axis and y-axis tick,
tick labels and axis labels. Code

Use Axes Methods (Accessors) to


5
access artists.

Set/Modify the Artist properties after


6
accessing the artists

Add Legend, Annotations if any and


7
Set Title to the Axes

78 DT Ch ChCh Ch Ch Ch Ch
Code
VISUALIZING MULTI DIMENSIONAL DISTRIBUTIONS
Create a Figure instance
1

Use Figure to create one or more Axes


2
or Subplot

Use Axes Helper Methods to add


3
artists to respective containers within
the Axes object.

Use Axes/Axis Helper Methods to


4
access and set x-axis and y-axis tick,
tick labels and axis labels.

Use Axes Methods (Accessors) to


5
access artists.

Set/Modify the Artist properties after


6
accessing the artists

Add Legend, Annotations if any and


7
Set Title to the Axes

79 DT Ch ChCh Ch Ch Ch Ch
Code

Create a Figure instance


1

Use Figure to create one or more Axes


2
or Subplot

Use Axes Helper Methods to add


3
artists to respective containers within
the Axes object.

Use Axes/Axis Helper Methods to


4
access and set x-axis and y-axis tick,
tick labels and axis labels.

Use Axes Methods (Accessors) to


5
access artists.

Set/Modify the Artist properties after


6
accessing the artists

Add Legend, Annotations if any and


7
Set Title to the Axes
Source : [Link]
master-plots-python/#13.-Diverging-Lollipop-Chart-with-Markers

80 DT Ch ChCh Ch Ch Ch Ch
Create a Figure instance
1

Use Figure to create one or more Axes


2
or Subplot

Use Axes Helper Methods to add


3
artists to respective containers within
the Axes object.
TICK
4
Use Axes/Axis Helper Methods to TICK LABELS
access and set x-axis and y-axis tick,
tick labels and axis labels. GRIDLINES
Use Axes Methods (Accessors) to
5
access artists.

Set/Modify the Artist properties after


6
accessing the artists

Add Legend, Annotations if any and


7
Set Title to the Axes

81 DT Ch ChCh Ch Ch Ch Ch
Create a Figure instance AXES HELPER METHODS FOR APPEARANCE, AXIS LIMITS
1
Page
Category Method Description
No.
Use Figure to create one or more Axes ✓ [Link] Convenience method to get or set some axis properties. 1432
2 ✓ Axes.set_axis_off Turn the x- and y-axis off. 1432
or Subplot ✓ 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
Use Axes Helper Methods to add Axes.get_frame_on Get whether the axes rectangle patch is drawn. 1432
3 ✓ Axes.set_axisbelow Set whether axis ticks and gridlines are above or below 1432
artists to respective containers within Appearance
most artists.
the Axes object. Axes.get_axisbelow Get whether axis ticks and gridlines are above or below 1432
most artists.
✓ [Link] Configure the grid lines. 1432
Use Axes/Axis Helper Methods to
4 Axes.get_facecolor Get the facecolor of the Axes. 1432
access and set x-axis and y-axis tick, ✓ Axes.set_facecolor Set the facecolor of the Axes. 1432
tick labels and axis labels. Property Cycle ✓✓ Axes.set_prop_cycle Set the property cycle of the Axes. 1440
✓ Axes.get_xaxis Return the XAxis instance. 1442
Axis / limits
✓ Axes.get_yaxis Return the YAxis instance. 1442
Use Axes Methods (Accessors) to Axes.invert_xaxis Invert the x-axis. 1442
5 Axes.xaxis_inverted Return whether the xaxis is oriented in the "inverse" 1442
access artists.
direction.
✓ Axes.invert_yaxis Invert the y-axis. 1442
Set/Modify the Artist properties after Axis limits and Axes.yaxis_inverted Return whether the yaxis is oriented in the "inverse" 1442
6 direction direction.
accessing the artists
✓✓ Axes.set_xlim Set the x-axis view limits. 1442
✓ Axes.get_xlim Return the x-axis view limits. 1442
Add Legend, Annotations if any and ✓✓ Axes.set_ylim Set the y-axis view limits. 1442
7 ✓ Axes.get_ylim Return the y-axis view limits. 1442
Set Title to the Axes
✓ indicates frequency and importance of usage of the method

82 DT Ch ChCh Ch Ch Ch Ch
AXES HELPER METHODS FOR TICKS AND TICK LABELS, GRID LINES
Create a Figure instance
1
Page
Category Method Description
No.
Axes.set_xticks Set the xaxis' tick locations. 1491
Use Figure to create one or more Axes
2 Axes.get_xticks Return the xaxis' tick locations in data coordinates. 1491
or Subplot ✓ 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
Use Axes Helper Methods to add
3 ✓ Axes.get_xminorticklabels Return the xaxis' minor tick labels, as a list of Text. 1491
artists to respective containers within ✓ Axes.get_xgridlines Return the xaxis' grid lines as a list of Line2Ds. 1491
the Axes object. ✓ 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 1491
as dates.
Use Axes/Axis Helper Methods to
4 Axes.set_yticks Set the yaxis' tick locations. 1491
access and set x-axis and y-axis tick, Ticks
Axes.get_yticks Return the yaxis' tick locations in data coordinates. 1491
tick labels and axis labels. ✓ Axes.set_yticklabels Set the yaxis' labels with list of string labels. 1491
and
Tick Labels ✓✓ Axes.get_yticklabels Get the yaxis' tick labels. 1491
✓ Axes.get_ymajorticklabels Return the yaxis' major tick labels, as a list of Text. 1491
Use Axes Methods (Accessors) to ✓ Axes.get_yminorticklabels Return the yaxis' minor tick labels, as a list of Text. 1491
5 ✓ Axes.get_ygridlines Return the yaxis' grid lines as a list of Line2Ds. 1491
access artists.
✓ 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 1491
Set/Modify the Artist properties after as dates.
6 ✓ Axes.minorticks_off Remove minor ticks from the axes. 1491
accessing the artists
✓ Axes.minorticks_on Display minor ticks on the axes. 1491
Axes.ticklabel_format Configure the ScalarFormatter used by default for linear 1491
Add Legend, Annotations if any and axes.
7 ✓✓ Axes.tick_params Change the appearance of ticks, tick labels, and gridlines. 1491
Set Title to the Axes
✓ Axes.locator_params Control behavior of major tick locators. 1491
✓ indicates frequency and importance of usage of the method

DTlabels
Also, can use [Link]([ticks, labels]), [Link]([ticks,labels]) to set ticks and Ch Chin one Ch Ch
Ch call. Ch Ch
Create a Figure instance AXES HELPER METHODS FOR AXIS LABELS, TITLE AND LEGEND
1
Category Method Description Page
No.
Use Figure to create one or more Axes ✓ Axes.set_xlabel Set the label for the x-axis. 1445
2
or Subplot ✓ 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
Use Axes Helper Methods to add
3 ✓ Axes.set_title Set a title for the Axes. 1445
artists to respective containers within Axes.get_title Get an Axes title. 1445
the Axes object. Axis labels, ✓ [Link] Place a legend on the Axes. 1445
title, Axes.get_legend Return the Legend instance, or None if no legend is defined. 1445
and legend
Use Axes/Axis Helper Methods to Axes.get_legend_handl Return handles and labels for legend 1445
4 es_labels
access and set x-axis and y-axis tick, Axes.set_xlabel Set the label for the x-axis. 1445

tick labels and axis labels. Axes.get_xlabel Get the xlabel text string. 1445
✓ Axes.set_ylabel Set the label for the y-axis. 1445
Use Axes Methods (Accessors) to Axes.get_ylabel Get the ylabel text string. 1445
5 Axes.set_title Set a title for the Axes. 1445
access artists. ✓
[Link] Create a twin Axes sharing the xaxis. 1513
[Link] Create a twin Axes sharing the yaxis. Check link. 1513
Set/Modify the Artist properties after
6 Twinning
[Link] Share the x-axis with other. 1513
accessing the artists [Link] Share the y-axis with other. 1513
and
sharing Axes.get_shared_x_axes Return a reference to the shared axes Grouper object for x 1513
Add Legend, Annotations if any and axes.
7 Axes.get_shared_y_axes Return a reference to the shared axes Grouper object for y 1513
Set Title to the Axes axes.
✓ indicates frequency and importance of usage of the method

84 DT Ch ChCh Ch Ch Ch Ch
AXIS HELPER METHODS (1/3)
Create a Figure instance
1
Page
Category Method Description
No.
Formatters Axis.get_major_formatter Get the formatter of the major ticker. 1540
Use Figure to create one or more Axes
2 and Locators Axis.get_major_locator Get the locator of the major ticker. 1540
or Subplot Axis.get_minor_formatter Get the formatter of the minor ticker. 1540
Axis.get_minor_locator Get the locator of the minor ticker. 1540
Use Axes Helper Methods to add ✓ Axis.set_major_formatter Set the formatter of the major ticker. 1540
3 ✓ Axis.set_major_locator Set the locator of the major ticker. 1540
artists to respective containers within ✓ Axis.set_minor_formatter Set the formatter of the minor ticker. 1540
the Axes object. ✓ Axis.set_minor_locator Set the locator of the minor ticker. 1540
Axis.remove_overlapping_locs If minor ticker locations that overlap with 1540
Use Axes/Axis Helper Methods to major ticker locations should be trimmed.
4 Axis Label Axis.set_label_coords Set the coordinates of the label. 1545
access and set x-axis and y-axis tick,
✓ Axis.set_label_position Set the label position (top or bottom) 1545
tick labels and axis labels.
Axis.set_label_text Set the text value of the axis label. 1545

Use Axes Methods (Accessors) to ✓ indicates frequency and importance of usage of the method
5
access artists.

Set/Modify the Artist properties after


6
accessing the artists

Add Legend, Annotations if any and


7
Set Title to the Axes

85 DT Ch ChCh Ch Ch Ch Ch
AXIS HELPER METHODS (2/3)
Create a Figure instance
1
Page
Category Method Description
No.
Use Figure to create one or more Axes ✓ Axis.get_major_ticks Return the list of major Ticks. 1547
2 ✓ Axis.get_majorticklabels Return this Axis' major tick labels, as a list of Text. 1547
or Subplot ✓ 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
Use Axes Helper Methods to add Axis.get_minor_ticks Return the list of minor Ticks. 1547
3 Axis.get_minorticklabels Return this Axis' minor tick labels, as a list of Text. 1547
artists to respective containers within
Axis.get_minorticklines Return this Axis' minor tick lines as a list of Line2Ds. 1547
the Axes object. Axis.get_minorticklocs Return this Axis' minor tick locations in data coordinates. 1547
Ticks, Axis.get_offset_text Return the axis offsetText as a Text instance. 1547
Use Axes/Axis Helper Methods to tick labels,
4 Offset text,
Axis.get_tick_padding 1547
access and set x-axis and y-axis tick, Axis.get_ticklabels Get this Axis' tick labels. 1547
GridLines ✓
tick labels and axis labels. ✓ 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
Use Axes Methods (Accessors) to ✓ Axis.get_gridlines Return this Axis' grid lines as a list of Line2Ds.
5 1547
access artists. ✓ [Link] Configure the grid lines. 1547
✓✓ Axis.set_tick_params Set appearance parameters for ticks, ticklabels, and 1547
gridlines.
Set/Modify the Artist properties after
6 Axis.axis_date Set up axis ticks and labels to treat data along this Axis 1547
accessing the artists as dates.
✓ indicates frequency and importance of usage of the method
Add Legend, Annotations if any and
7
Set Title to the Axes

86 DT Ch ChCh Ch Ch Ch Ch
AXIS HELPER METHODS (3/3)
Create a Figure instance
1
Page
Category Method Description
No.
XAxis.axis_name Read-only name identifying the axis. 1559
Use Figure to create one or more Axes
2 XAxis.get_text_heights
Return how much space should be reserved for text 1559
or Subplot above and below the axes, as a pair of floats.
XAxis.get_ticks_position Return the ticks position ("top", "bottom", "default", or 1559
"unknown").
Use Axes Helper Methods to add XAxis Specific
3 ✓ XAxis.set_ticks_position Set the ticks position. 1559
artists to respective containers within ✓ XAxis.tick_bottom Move ticks and ticklabels (if present) to the bottom of the 1559
the Axes object. axes.
✓ XAxis.tick_top Move ticks and ticklabels (if present) to the top of the 1559
axes.
Use Axes/Axis Helper Methods to YAxis.axis_name Read-only name identifying the axis. 1563
4
access and set x-axis and y-axis tick, YAxis.get_text_widths 1563
tick labels and axis labels. YAxis.get_ticks_position Return the ticks position ("left", "right", "default", or 1563
"unknown").
YAxis.set_offset_position 1563
Use Axes Methods (Accessors) to YAxis Specific
5 ✓ YAxis.set_ticks_position Set the ticks position. 1563
access artists. ✓ YAxis.tick_left Move ticks and ticklabels (if present) to the left of the 1563
axes.
Set/Modify the Artist properties after ✓ YAxis.tick_right Move ticks and ticklabels (if present) to the right of the 1563
6 axes.
accessing the artists Other ✓ [Link] The Axes instance the artist resides in, or None. 1563

Use with Axis.set_ticks Set this Axis' tick locations. 1564


Add Legend, Annotations if any and
7 Caution* Axis.set_ticklabels Set the text values of the tick labels. 1565
Set Title to the Axes *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.
✓ indicates frequency and importance of usage of the method

87 DT Ch ChCh Ch Ch Ch Ch
TICK PARAMS METHOD – BULK PROPERTY SETTER
Create a Figure instance
1
Tick Params to change the appearance of ticks, tick labels, and gridlines.
Call signature : Axes.tick_params(), axis.set_tick_params()
Use Figure to create one or more Axes
2 Parameters Description
or Subplot axis : {'x', 'y', 'both'}, default: 'both' The axis to which the parameters are applied.

which : {'major', 'minor', 'both'}, The group of ticks to which the parameters are applied.
Use Axes Helper Methods to add default: 'major'
3
artists to respective containers within reset : bool, default: False Whether to reset the ticks to defaults before updating
them.
the Axes object. 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.
Use Axes/Axis Helper Methods to
4 color : color Tick color.
access and set x-axis and y-axis tick, pad : float Distance in points between tick and label.
tick labels and axis labels. 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.
Use Axes Methods (Accessors) to zorder: float Tick and label zorder.
5 bottom, top, left, right : bool Whether to draw the respective ticks.
access artists.
labelbottom, labeltop, labelleft, Whether to draw the respective tick labels.
labelright :bool
Set/Modify the Artist properties after labelrotation : float Tick label rotation
6 grid_color : color Gridline color.
accessing the artists 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.
Add Legend, Annotations if any and
7 Matplotlib Release 3.4.2 document, Pg No. 1491, 1504-1506
Set Title to the Axes
Enter length = 0 if tick lines are not desired

88 DT Ch ChCh Ch Ch Ch Ch
A Note on Practical implementation
Create a Figure instance
1 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
Check
Use Figure to create one or more Axes • Placing Tick labels inside the axes
2 • Adjusting the padding between the tick and labels
Examples
or Subplot • Setting the label color and label size
3. However, note that Tick_params does not have horizontal alignment or vertical alignment argument.
4. Use [Link].get_ticklabels() to access tick labels as text instances within a list.
Use Axes Helper Methods to add
3
artists to respective containers within [In]: type([Link].get_ticklabels()[0])
[Out]: [Link]
the Axes object.
5. We can Iterate/Loop over all the tick labels with or without conditions and then use [Link] or
[Link]() method to modify text instance properties.
Use Axes/Axis Helper Methods to
4
access and set x-axis and y-axis tick, for labels in [Link].get_ticklabels():
[Link](labels, size = 14, rotation_mode = 'anchor’,
tick labels and axis labels. ha = 'right’)

[Link].set_tick_params(rotation =0, labelsize = 15 )


Use Axes Methods (Accessors) to
5 Check
access artists. [[Link](alpha = 0.5) for ticks in ax.get_xticklabels()] Examples
[[Link](alpha = 0.5) for ticks in ax.get_yticklabels()]

Set/Modify the Artist properties after 6. Grid lines can be set by using [Link](b = None, which = ‘major’, axis = ‘both’, **kwargs) or
6 [Link](b = None, which = ‘major’,**kwargs).
accessing the artists 7. Use Axes.set_axisbelow(True) to place gridlines/tick lines behind or below all the artists.
8. Use [Link]([ticks, labels]), [Link]([ticks, labels]) to place ticks and labels in one call. This is helpful
for simple use-cases.
Add Legend, Annotations if any and
7 9. Beyond simple usecases, it is preferable to use Tick Locator/Formatter pairs to customize the tick
Set Title to the Axes positions and the string formatting of the ticks. For general tick locator/formatter, check
[Link] module. For datetime tick locator/formatter, check [Link] module.

Matplotlib Release 3.4.2 document, Pg No. 1491, 1504-1506 | Tick_Params | GridLines | Example_Demo |

89 DT Ch ChCh Ch Ch Ch Ch
Create a Figure instance
1

Use Figure to create one or more Axes


2
or Subplot

Use Axes Helper Methods to add


3
artists to respective containers within
the Axes object.
TICK
4
Use Axes/Axis Helper Methods to LOCATORS
access and set x-axis and y-axis tick,
tick labels and axis labels. FORMATTERS
Use Axes Methods (Accessors) to
5
access artists.

Set/Modify the Artist properties after


6
accessing the artists

Add Legend, Annotations if any and


7
Set Title to the Axes

90 DT Ch ChCh Ch Ch Ch Ch
Create a Figure instance SPECIAL NOTE ON TICKERS – LOCATOR CLASS
1
Tick locators define the position of the ticks.
Use Figure to create one or more Axes
2
or Subplot Locator class Description
NullLocator No ticks
Use Axes Helper Methods to add FixedLocator Tick locations are fixed
3
artists to respective containers within IndexLocator Locator for index plots (e.g., where x = range(len(y)))
the Axes object. LinearLocator Evenly spaced ticks from min to max
LogLocator Logarithmically ticks from min to max
Use Axes/Axis Helper Methods to
4 MultipleLocator Ticks and range are a multiple of base
access and set x-axis and y-axis tick, MaxNLocator Finds up to a max number of ticks at nice locations
tick labels and axis labels.
AutoLocator (Default.) MaxNLocator with simple defaults.
AutoMinorLocator Locator for minor ticks
Use Axes Methods (Accessors) to
5 *Matplotlib Release, 3.4.2, Page No. 2812
access artists.
*[Link]

Set/Modify the Artist properties after


6 Use tick locator/formatter pairs for controlling tick position and string representation
accessing the artists

Add Legend, Annotations if any and


7
Set Title to the Axes

91 DT Ch ChCh Ch Ch Ch Ch
Create a Figure instance SPECIAL NOTE ON TICKERS – LOCATORs - DEMO 1
1
code
Use Figure to create one or more Axes
2
or Subplot

Use Axes Helper Methods to add


3
artists to respective containers within
the Axes object.

Use Axes/Axis Helper Methods to


4
access and set x-axis and y-axis tick,
tick labels and axis labels.
import [Link] as plt
import [Link] as ticker
Use Axes Methods (Accessors) to
5
access artists. fig, ax = [Link](2,2,figsize= (10,5), sharey = True)
ax1,ax2,ax3,ax4 = [Link]() #Unpacking numpy array of axes
Set/Modify the Artist properties after [ax.set_ylim(0,20) for ax in [Link]] #Setting ylim as (0,20)
6
accessing the artists [Link].set_major_locator([Link]())
[Link].set_major_locator([Link](2))
Add Legend, Annotations if any and [Link].set_major_locator([Link]([0,1,2,8,9]))
7 [Link].set_major_locator([Link](5))
Set Title to the Axes

92 DT Ch ChCh Ch Ch Ch Ch
Create a Figure instance SPECIAL NOTE ON TICKERS – LOCATORs - DEMO 2
1

Use Figure to create one or more Axes


2
or Subplot

Use Axes Helper Methods to add


3
artists to respective containers within
the Axes object.

Use Axes/Axis Helper Methods to


4
access and set x-axis and y-axis tick,
tick labels and axis labels.

Use Axes Methods (Accessors) to


5
access artists.

Set/Modify the Artist properties after


6
accessing the artists

Add Legend, Annotations if any and


7
Set Title to the Axes

93 DT Ch ChCh Ch Ch Ch Ch
Create a Figure instance SPECIAL NOTE ON TICKERS – FORMATTER CLASS
1
Tick formatters define how the numeric value associated with a
Use Figure to create one or more Axes tick on an axis is formatted as a string.
2
or Subplot
Formatter Class Description
Use Axes Helper Methods to add NullFormatter No labels on the ticks
3
artists to respective containers within IndexFormatter Set the strings from a list of labels
the Axes object. FixedFormatter Set the strings manually for the labels
FuncFormatter User-defined function sets the labels
Use Axes/Axis Helper Methods to
4 FormatStrFormatter Use a format string for each value
access and set x-axis and y-axis tick,
ScalarFormatter (Default.) Formatter for scalar values
tick labels and axis labels.
LogFormatter Default formatter for log axes
*Matplotlib Release, 3.4.2, Page No. 2814
Use Axes Methods (Accessors) to
5 *[Link]
access artists.
Use tick locator/formatter pairs for controlling tick position and string representation
Set/Modify the Artist properties after
6
accessing the artists

Add Legend, Annotations if any and


7
Set Title to the Axes

94 DT Ch ChCh Ch Ch Ch Ch
Create a Figure instance SPECIAL NOTE ON TICKERS – USING SIMPLE FORMATTING
1
Code

Use Figure to create one or more Axes


2
or Subplot

Use Axes Helper Methods to add


3
artists to respective containers within
the Axes object.

Use Axes/Axis Helper Methods to


4
access and set x-axis and y-axis tick,
tick labels and axis labels.

Use Axes Methods (Accessors) to


5
access artists.

Set/Modify the Artist properties after


6
accessing the artists

Add Legend, Annotations if any and


7
Set Title to the Axes

95 DT Ch ChCh Ch Ch Ch Ch
Create a Figure instance SPECIAL NOTE ON TICKERS – USING FORMATTER CLASS
1
Code

Use Figure to create one or more Axes


2
or Subplot

Use Axes Helper Methods to add


3
artists to respective containers within
the Axes object.

Use Axes/Axis Helper Methods to


4
access and set x-axis and y-axis tick,
tick labels and axis labels.

Use Axes Methods (Accessors) to


5
access artists.

Set/Modify the Artist properties after


6
accessing the artists

Add Legend, Annotations if any and


7
Set Title to the Axes

96 DT Ch ChCh Ch Ch Ch Ch
1
Create a Figure instance TICKERS FOR TIME SERIES PLOTTING (1/3)

Use Figure to create one or more Axes Ø Matplotlib handles dates in its own format.
2 Ø Helper Methods are available to convert datetime objects and timedelta
or Subplot
objects to matplotlib dates and vice-versa.

Use Axes Helper Methods to add


3
artists to respective containers within datetime Python’s built-in module to handle date/ time
the Axes object.
The datetime module is python’s built-in module that
supplies classes for manipulating dates and times.
Use Axes/Axis Helper Methods to
4 Four Main Classes TZ Awareness
access and set x-axis and y-axis tick,
tick labels and axis labels. time date datetime timedelta tzinfo
only time in hours, only year, month All components of An amount of time
minutes, seconds time and date with maximum unit
Use Axes Methods (Accessors) to and microseconds
and day
of days timezone
5
access artists.

Set/Modify the Artist properties after dateutil powerful extensions to datetime


6
accessing the artists
The dateutil module provides additional code to
Add Legend, Annotations if any and handle date ticking, making it easy to place ticks on
7 any kinds of dates.
Set Title to the Axes
Check Link

97 DT Ch ChCh Ch Ch Ch Ch
1
Create a Figure instance TICKERS FOR TIME SERIES PLOTTING (2/3)
Helper methods in [Link] for conversion between datetime objects and
Use Figure to create one or more Axes Matplotlib dates
2
or Subplot
METHOD DESCRIPTION
datestr2num Convert a date string to a datenum using [Link]
Use Axes Helper Methods to add
3 date2num Convert datetime objects to Matplotlib dates.
artists to respective containers within
the Axes object. num2date Convert Matplotlib dates to datetime objects.
num2timedelta Convert number of days to a timedelta object.
Use Axes/Axis Helper Methods to drange Return a sequence of equally spaced Matplotlib dates.
4
access and set x-axis and y-axis tick, set_epoch Set the epoch (origin for dates) for datetime calculations.
tick labels and axis labels. get_epoch Get the epoch used by dates.
Go to
Use Axes Methods (Accessors) to datetime
Section
5
access artists. object
num2date
Set/Modify the Artist properties after
6
accessing the artists date2num

Add Legend, Annotations if any and


7 matplotlib timedelta
Set Title to the Axes date object

98 DT Ch ChCh Ch Ch Ch Ch
Create a Figure instance TICKERS FOR TIME SERIES PLOTTING (3/3)
1
Tick locators and formatters from [Link] module
Use Figure to create one or more Axes
2 DATE LOCATORS
or Subplot
MicrosecondLocator Locate microseconds.
SecondLocator Locate seconds.
Use Axes Helper Methods to add MinuteLocator Locate minutes.
3 HourLocator Locate hours.
artists to respective containers within
DayLocator Locate specified days of the month.
the Axes object. WeekdayLocator Locate days of the week, e.g., MO, TU.
MonthLocator Locate months, e.g., 7 for July.
Use Axes/Axis Helper Methods to YearLocator Locate years that are multiples of base.
4 RRuleLocator Locate using a [Link]. rrulewrapper is a
access and set x-axis and y-axis tick, simple wrapper around dateutil's [Link] which allow
tick labels and axis labels. 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.
Use Axes Methods (Accessors) to
5 DATE FORMATTERS
access artists. 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
Set/Modify the Artist properties after
6 the format as compact as possible while still having complete
accessing the artists date information. This is most useful when used with the
AutoDateLocator.
DateFormatter use strftime format strings.
Add Legend, Annotations if any and
7 IndexDateFormatter date plots with implicit x indexing.
Set Title to the Axes

99 DT Ch ChCh Ch Ch Ch Ch
import datetime
import [Link] as plt CONCISE DATE FORMATTER EXAMPLE
import [Link] as mdates # For Date locators and formatters
import numpy as np

base = [Link](2005, 2, 1)
dates = [base + [Link](hours=(2 * i)) for i in range(732)]
N = len(dates)

#Fixing random state for reproducibility


[Link](19680801)
y = [Link]([Link](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 = [Link](3, 1, constrained_layout=True, figsize=(6, 6))


for nn, ax in enumerate(axs):
locator = [Link](minticks=3, maxticks=7)
formatter = [Link](locator)

[Link].set_major_locator(locator)
[Link].set_major_formatter(formatter)

[Link](dates, y)
ax.set_xlim(lims[nn])

axs[0].set_title('Concise Date Formatter’)


[Link]
[Link]() [Link]?highlight=date

100 DT Ch ChCh Ch Ch Ch Ch
Create a Figure instance
1

Use Figure to create one or more Axes


2
or Subplot

Use Axes Helper Methods to add


3
artists to respective containers within
the Axes object.

4
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick, OO STYLE ESSENCE
tick labels and axis labels.

Use Axes Methods (Accessors) to


5
access artists.

Set/Modify the Artist properties after


6
accessing the artists

Add Legend, Annotations if any and


7
Set Title to the Axes

101 DT Ch ChCh Ch Ch Ch Ch
Create a Figure instance
1
• In OO style, we want to access the artists first.
Use Figure to create one or more Axes • This would allow us to access the attributes and
2 properties of the artist.
or Subplot
• We can then modify the properties using setter
methods.
Use Axes Helper Methods to add
3 • We can also inspect into the attributes, add
artists to respective containers within
primitives into them and modify using appropriate
the Axes object.
methods.
Use Axes/Axis Helper Methods to
4
access and set x-axis and y-axis tick,
tick labels and axis labels.

Use Axes Methods (Accessors) to


5
access artists.

Set/Modify the Artist properties after


6
accessing the artists

Add Legend, Annotations if any and


7
Set Title to the Axes

102 DT Ch ChCh Ch Ch Ch Ch
ACCESSING ARTISTS
Create a Figure instance Ø BY ASSIGNING TO A VARIABLE WHEN CREATING
1
One way to gain access to the primitives is to assign it to a
variable when it is created using Axes helper method.
Use Figure to create one or more Axes
2 Instead of :
or Subplot
[Link](x,y) #returns rectangular patch objects
we should assign it to a variable as below :
Use Axes Helper Methods to add bars = [Link](x,y)
3
artists to respective containers within
the Axes object. Now, bars variable contain the rectangular patch objects that
were returned by the [Link]() method.
Use Axes/Axis Helper Methods to
4 We can use below methods to inspect the children of the
access and set x-axis and y-axis tick, bars variable:
tick labels and axis labels. str(bars)

Use Axes Methods (Accessors) to After finding the index location of Barcontainer object, we
5 will access the rectangular patches :
access artists.
bars[0].get_children()

Set/Modify the Artist properties after


6
accessing the artists

Add Legend, Annotations if any and


7
Set Title to the Axes

103 DT Ch ChCh Ch Ch Ch Ch
ACCESSING ARTISTS
Create a Figure instance Ø THROUGH ATTRIBUTES (1/2)
1
The Second way is to access the attributes.
In the current example, we will access the patches attribute
Use Figure to create one or more Axes of ax object.
2 [Link](x,y)
or Subplot
[Link]

Use Axes Helper Methods to add We can then iterate through the patches in [Link].
3
artists to respective containers within
the Axes object. In a similar manner, we have access to other axes attributes
[Link]
[Link]
Use Axes/Axis Helper Methods to
4 [Link]
access and set x-axis and y-axis tick, [Link]
tick labels and axis labels. [Link]
[Link]()
Use Axes Methods (Accessors) to [Link]
5 [Link]
access artists.

Set/Modify the Artist properties after


6
accessing the artists

Add Legend, Annotations if any and


7
Set Title to the Axes

104 DT Ch ChCh Ch Ch Ch Ch
ACCESSING ARTISTS
Create a Figure instance Ø THROUGH ATTRIBUTES (2/2)
1
ITERATING OVER THE ARTIST ATTRIBUTES CONTAINERS

Use Figure to create one or more Axes bars = [rect for rect in ax.get_children() if
2 isinstance(rect, [Link])]
or Subplot
[p.set_label(x[i]) for i,p in zip(range(len(x)), [Link])]
Use Axes Helper Methods to add
3
artists to respective containers within [p.set_label(x[i]) for i,p in enumerate([Link])]
the Axes object.

Use Axes/Axis Helper Methods to


4
access and set x-axis and y-axis tick,
tick labels and axis labels.

Use Axes Methods (Accessors) to


5
access artists.

Set/Modify the Artist properties after


6
accessing the artists

Add Legend, Annotations if any and


7
Set Title to the Axes

105 DT Ch ChCh Ch Ch Ch Ch
Create a Figure instance USEFUL METHODS AND PYTHON BUILT IN METHODS Check
1
ARTIST OBJECT METHODS
• [Link](artistobject)
Use Figure to create one or more Axes • [Link](artistobject)
2 • artist.get_children()
or Subplot
• [Link]()

Use Axes Helper Methods to add FOR INSPECTION


3 • print(dir(object))
artists to respective containers within
the Axes object. • vars(ax) or ax.__dict__
• str(ax)
• type(object)
Use Axes/Axis Helper Methods to
4 • len(object)
access and set x-axis and y-axis tick, • isinstance(obj, class)
tick labels and axis labels. • get_text()
• hasattr(), getattr(), setattr()
Use Axes Methods (Accessors) to FOR LOOPING/ ITERATION
5
access artists. • enumerate()
• zip()
Set/Modify the Artist properties after • flatten()
6 • ravel()
accessing the artists
• list comprehension
• [Link]()
Add Legend, Annotations if any and • [Link]()
7
Set Title to the Axes • list()
• Lambda function

106 DT Ch ChCh Ch Ch Ch Ch
ACCESSING ARTISTS THROUGH ATTRIBUTES
Create a Figure instance Figure attribute Description
1 axes A list of Axes instances (includes Subplot)
patch The Rectangle background
images A list of FigureImage patches - useful for raw pixel display
Use Figure to create one or more Axes FIGURE legends A list of Figure Legend instances (different
2 from [Link])
or Subplot
lines A list of Figure Line2D instances (rarely used, see [Link])
patches A list of Figure Patchs (rarely used, see [Link])
Use Axes Helper Methods to add texts A list Figure Text instances
3
artists to respective containers within Axes attribute Description
the Axes object. artists A list of Artist instances
patch Rectangle instance for Axes background
collections A list of Collection instances
Use Axes/Axis Helper Methods to images A list of AxesImage
4
access and set x-axis and y-axis tick, AXES legends A list of Legend instances
lines A list of Line2D instances
tick labels and axis labels. patches A list of Patch instances
texts A list of Text instances
xaxis A [Link] instance
Use Axes Methods (Accessors) to
5 yaxis A [Link] instance
access artists.
Use Axis Accessor/Helper Methods to access axis labels, tick lines, tick
AXIS labels, tick locators
Set/Modify the Artist properties after
6 Tick attribute Description
accessing the artists tick1line A Line2D instance
tick2line A Line2D instance
Add Legend, Annotations if any and TICK gridline A Line2D instance
7 label1 A Text instance
Set Title to the Axes 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
107 DT Ch ChCh Ch Ch Ch Ch
MAPPING THE JOURNEY
Create a Figure instance FROM AXES INSTANCE TO ARTIST INSTANCE PROPERTIES
1
AXES LABEL GUIDE
Use Figure to create one or more Axes
2 CLASS INSTANCES
or Subplot
ATTRIBUTES
ATTRIBUTES
Use Axes Helper Methods to add Use dot operator to
3 PROPERTIES
artists to respective containers within access attribute
the Axes object.
Attributes are list of ARTISTS
Artists objects.
Use Axes/Axis Helper Methods to
4 Use setter and
access and set x-axis and y-axis tick, getter methods
tick labels and axis labels. PROPERTIES
Change Multiple
Use Axes Methods (Accessors) to properties COMMON
5 • [Link]()
access artists. PROPERTIES
• [Link]()

6 Set/Modify the Artist properties after ARTIST SPECIFIC


accessing the artists PROPERTIES

Add Legend, Annotations if any and


7 RECTANGLE
Set Title to the Axes TEXT LINE2D PATCH

108 DT Ch ChCh Ch Ch Ch Ch
PROPERTIES COMMON TO ALL ARTISTS (1/2)
Create a Figure instance
1 PROPERTY DESCRIPTION
alpha The transparency - a scalar from 0-1
animated A boolean that is used to facilitate animated drawing
Use Figure to create one or more Axes axes The Axes that the Artist lives in, possibly None
2 clip_box The bounding box that clips the Artist
or Subplot clip_on Whether clipping is enabled
clip_path The path the artist is clipped to
Use Axes Helper Methods to add contains A picking function to test whether the artist contains the
3 pick point
artists to respective containers within figure The figure instance the artist lives in, possibly None
the Axes object. label A text label (e.g., for auto-labeling)
picker A python object that controls object picking
transform The transformation
Use Axes/Axis Helper Methods to
4 visible A boolean whether the artist should be drawn
access and set x-axis and y-axis tick, zorder A number which determines the drawing order
tick labels and axis labels. rasterized Boolean; Turns vectors into raster graphics (for
compression & EPS transparency)
*Matplotlib Release, 3.4.2, Page No. 113-120
Use Axes Methods (Accessors) to
5
access artists
[Link] attributes. Ø “Each of the properties is accessed with setter or getter“
Ø (The exception being axes property which can be accessed by
Set/Modify the Artist properties after dot operator)
6
accessing the artists
[Link].get_alpha( )
Add Legend
Legend,andAnnotations
Set Title to
if any
theand
Axes [Link].set_alpha( [Link].get_apha()*1.2 )
7
Set Title to the Axes [Link].set_alpha( 0.5 )

109 DT Ch ChCh Ch Ch Ch Ch
PROPERTIES COMMON TO ALL ARTISTS (2/2)
Create a Figure instance
1 USE CASE SCENARIO ARTIST PROPERTY
For User-defined function figure, axes in conjunction with [Link](),
[Link](), [Link](), [Link]()
Use Figure to create one or more Axes
2 For Adding artist to Legend label
or Subplot
Setting ticks, axis, spines or visible
even axes(for multiple
Use Axes Helper Methods to add subplots) invisible
3
artists to respective containers within If multiple artists overlap such zorder
as axhspan, axvspan,
the Axes object.
fillbetween
If text, patch, arrows, transform : [Link] | [Link] |
Use Axes/Axis Helper Methods to
4 annotation etc. are to be [Link] | fig. dpi_scale_trans
access and set x-axis and y-axis tick, added either in data
coordinates or in figure where ax is an axes instance, fig is a figure
tick labels and axis labels. coordinates instance. Refer to official docs for full list.

Use Axes Methods (Accessors) to


5
access artists
[Link] attributes.

Set/Modify the Artist properties after


6
accessing the artists

Add Legend
Legend,andAnnotations
Set Title to
if any
theand
Axes
7
Set Title to the Axes

110 DT Ch ChCh Ch Ch Ch Ch
SETTING ARTIST PROPERTIES
• Use setp function of Pyplot Interface
• Use [Link]() method to set properties of this Axes object.

# Preparing the Data


group_names = ['Nexus', 'Chiffron', 'Pendos', 'Forresters']
group_data = [105000, 125000, 70500, 90000]

[Link]()
fig, ax = [Link](figsize=(8, 4)) #Instantiating fig and axes
[Link](group_names, group_data, height = 0.8)

# Accessing the artist using axes accessor method


labels = ax.get_xticklabels()

# Setting the artist property using [Link] method


[Link](labels, rotation=15, ha = 'right', fontsize = 10)

# Setting the artist property using [Link]() method


[Link](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 [Link]]

[Link]("Setting [Link]",dpi=300, format='png', bbox_inches='tight')

111 DT Ch ChCh Ch Ch Ch Ch
INSPECTING ARTIST PROPERTIES
import [Link] as plt • [Link](artistobject)
lines = [Link]([1, 2, 3])
[Link](lines) # list of settable line properties
• [Link](artistobject)

agg_filter: a filter function, which takes a (m, n, 3) float array and a marker: marker style string, `~.[Link]` or `~.[Link]`
markeredgecolor or mec: color
dpi value, and returns a (m, n, 3) array
markeredgewidth or mew: float
alpha: float or None
markerfacecolor or mfc: color
animated: bool markerfacecoloralt or mfcalt: color
antialiased or aa: bool markersize or ms: float
clip_box: `.Bbox` markevery: None or int or (int, int) or slice or List[int] or float or (float, float) or List[bool]
clip_on: bool path_effects: `.AbstractPathEffect`
clip_path: Patch or (Path, Transform) or None picker: unknown pickradius: float
color or c: color rasterized: bool or None
contains: unknown sketch_params: (scale: float, length: float, randomness: float)
dash_capstyle: {'butt', 'round', 'projecting’} snap: bool or None
dash_joinstyle: {'miter', 'round', 'bevel’} solid_capstyle: {'butt', 'round', 'projecting’}
solid_joinstyle: {'miter', 'round', 'bevel’}
dashes: sequence of floats (on/off ink in points) or (None, None)
transform: `[Link]`
data: (2, N) array or two 1D arrays url: str
drawstyle or ds: {'default', 'steps', 'steps-pre', 'steps-mid', 'steps- visible: bool
post'}, default: 'default’ xdata: 1D array
figure: `.Figure` ydata: 1D array
fillstyle: {'full', 'left', 'right', 'bottom', 'top', 'none’} zorder: float
gid: str
in_layout: bool #[Link] also returns a listing of properties for a given object.
label: object linestyle or ls: {'-', '--', '-.', ':', '', (offset, on-off-seq), import [Link] as plt
...} import [Link] as mpa
linewidth or lw: float lines = [Link]([1, 2, 3])
[Link](lines)

*Matplotlib, Release 3.4.2, Pg No. 33,76,111

112 DT Ch ChCh Ch Ch Ch Ch
ADDITIONAL PROPERTIES SPECIFIC TO LINE2D
Create a Figure instance PROPERTY DESCRIPTION
1 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]
Use Figure to create one or more Axes color or c any matplotlib color
2 dash_capstyle [‘butt’ | ‘round’ | ‘projecting’]
or Subplot
dash_joinstyle [‘miter’ | ‘round’ | ‘bevel’]
dashes sequence of on/off ink in points
Use Axes Helper Methods to add drawstyle [‘default’ | ‘steps’ | ‘steps-pre’ | ‘steps-mid’ | ‘steps-post’]
3 fillstyle [‘full’ | ‘left’ | ‘right’ | ‘bottom’ | ‘top’ | ‘none’]
artists to respective containers within
gid an id string
the Axes object. linestyle or ls [‘solid’ | ‘dashed’, ‘dashdot’, ‘dotted’ | (offset, on-off-dash-
seq) | '-' | '--' | '-.' | ':' | 'None' | ' ' | '']
Use Axes/Axis Helper Methods to linewidth or lw float value in points
4 marker A valid marker style
access and set x-axis and y-axis tick, markeredgecolor or mec any matplotlib color
tick labels and axis labels. markeredgewidth or mew float value in points
markerfacecolor or mfc any matplotlib color
markerfacecoloralt or any matplotlib color
Use Axes Methods (Accessors) to
5 mfcalt
access artists. markersize or ms float
markevery [None | int | length-2 tuple of int | slice | list/array of int |
float | length-2 tuple of float]
Set/Modify the Artist properties after
6 path_effects AbstractPathEffect
accessing the artists pickradius float distance in points
solid_capstyle [‘butt’ | ‘round’ | ‘projecting’]
solid_joinstyle [‘miter’ | ‘round’ | ‘bevel’]
Add Legend, Annotations if any and
7 url a url string
Set Title to the Axes xdata 1D array
ydata 1D array
*Matplotlib Release, 3.4.2, Page No. 2260-61
113 DT Ch ChCh Ch Ch Ch Ch
ADDITIONAL PROPERTIES SPECIFIC TO TEXT (1/2)
Create a Figure instance PROPERTY DESCRIPTION
1 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
Use Figure to create one or more Axes bbox dict with properties for [Link]
2 color or c color
or Subplot
fontfamily or family {FONTNAME, 'serif', 'sans-serif', 'cursive', 'fantasy', 'monospace'}
fontproperties or font or font_manager.FontProperties or str or [Link]
Use Axes Helper Methods to add font_properties
3 fontsize or size float or {'xx-small', 'x-small', 'small', 'medium', 'large', 'x-large', 'xx-
artists to respective containers within
large'}
the Axes object. fontstretch or stretch horizontal condensation or expansion
{a numeric value in range 0-1000, 'ultra-condensed', 'extra-
condensed', 'condensed', 'semi-condensed', 'normal', 'semi-
Use Axes/Axis Helper Methods to
4 expanded', 'expanded', 'extra-expanded', 'ultra-expanded'}
access and set x-axis and y-axis tick, fontstyle or style {'normal', 'italic', 'oblique'}
tick labels and axis labels. 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',
Use Axes Methods (Accessors) to 'bold', 'heavy', 'extra bold', 'black'}
5
access artists. gid str
*Matplotlib Release, 3.4.2, Page No. 2795-2796, 326-329

Set/Modify the Artist properties after


6
accessing the artists

Add Legend, Annotations if any and


7
Set Title to the Axes

114 DT Ch ChCh Ch Ch Ch Ch
ADDITIONAL PROPERTIES SPECIFIC TO TEXT (2/2)
Create a Figure instance PROPERTY DESCRIPTION
1 horizontalalignment or ha {'center', 'right', 'left'}
in_layout bool
linespacing float (multiple of font size)
Use Figure to create one or more Axes math_fontfamily str
2 multialignment or ma {'left', 'right', 'center'}
or Subplot
path_effects AbstractPathEffect
position (float, float)
Use Axes Helper Methods to add rotation float or {'vertical', 'horizontal'}
3 rotation_mode {None, 'default', 'anchor'}
artists to respective containers within
text object
the Axes object. transform_rotates_text bool
url str
Use Axes/Axis Helper Methods to usetex bool or None
4 verticalalignment or va {'center', 'top', 'bottom', 'baseline', 'center_baseline'}
access and set x-axis and y-axis tick,
visible bool
tick labels and axis labels. wrap bool
x float
Use Axes Methods (Accessors) to y float
5 zorder float
access artists.
url str
usetex bool or None
Set/Modify the Artist properties after *Matplotlib Release, 3.4.2, Page No. 2795-2796
6
accessing the artists

Add Legend, Annotations if any and


7
Set Title to the Axes

115 DT Ch ChCh Ch Ch Ch Ch
TEXT INSTANCE – HORIZONTAL ALIGNMENT DEMO
Create a Figure instance
1
Code

Use Figure to create one or more Axes


2
or Subplot

Use Axes Helper Methods to add


3
artists to respective containers within
the Axes object.

Use Axes/Axis Helper Methods to


4
access and set x-axis and y-axis tick,
tick labels and axis labels.

Use Axes Methods (Accessors) to


5
access artists.

Set/Modify the Artist properties after


6
accessing the artists

Add Legend, Annotations if any and


7
Set Title to the Axes

116 DT Ch ChCh Ch Ch Ch Ch
SPECIAL LOOK INTO PATCH RECTANGLE
Create a Figure instance
1 RECTANGLE PARAMETERS

Use Figure to create one or more Axes


2
or Subplot
height
Use Axes Helper Methods to add
3
artists to respective containers within
the Axes object.
(x,y)
width
Use Axes/Axis Helper Methods to
4
access and set x-axis and y-axis tick,
tick labels and axis labels. (x,y) [(float, float)] The anchor point
width [float] Rectangle width
Use Axes Methods (Accessors) to height [float] Rectangle height
5
access artists. angle [float, default: 0] Rotation in degrees anti-
clockwise about xy
Set/Modify the Artist properties after *Matplotlib Release, 3.4.2, Page No. 2409
6
accessing the artists
Note: the float values are in the coordinate system for the data,
controlled by xlim and ylim.
Add Legend, Annotations if any and
7
Set Title to the Axes

117 DT Ch ChCh Ch Ch Ch Ch
ADDITIONAL PROPERTIES PATCH RECTANGLE SPECIFIC
Create a Figure instance PROPERTY DESCRIPTION
1
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
Use Figure to create one or more Axes
2 capstyle CapStyle or {'butt', 'projecting', 'round'}
or Subplot color color
edgecolor or ec color or None or 'auto'
facecolor or fc color or None
Use Axes Helper Methods to add
3 fill bool
artists to respective containers within gid str
the Axes object. hatch {'/', '\', '|', '-', '+', 'x', 'o', 'O', '.', '*'}
in_layout bool
joinstyle JoinStyle or {'miter', 'round', 'bevel'}
Use Axes/Axis Helper Methods to linestyle or ls {'-', '--', '-.', ':', '', (offset, on-off-seq), ...}
4
access and set x-axis and y-axis tick, linewidth or lw float or None
path_effects AbstractPathEffect
tick labels and axis labels.
sketch_params (scale: float, length: float, randomness: float)
snap bool or None
Use Axes Methods (Accessors) to url str
5
access artists.

Set/Modify the Artist properties after


6
accessing the artists

Add Legend, Annotations if any and


7
Set Title to the Axes

118 DT Ch ChCh Ch Ch Ch Ch
USE CASES – MODIFYING THE RECTANGLE PATCH PROPERTIES
Create a Figure instance Patch instance Use Case Scenario
1
[Link] Changing color of figure background
[Link] Changing color of axes background
Use Figure to create one or more Axes [Link] Adding data labels on top of the bars in a bar plot
2 [Link] Assigning label values to each of the patches of bar plot
or Subplot
for adding to legend
Use Axes Helper Methods to add
3 import [Link] as plt
artists to respective containers within
import numpy as np
the Axes object.
fig, (ax1, ax2) = [Link](1,2, sharey = True)
Use Axes/Axis Helper Methods to
4 x = [Link](1,20,20)
access and set x-axis and y-axis tick,
tick labels and axis labels. #Set the face color of figure background
rect = [Link]
[Link](facecolor = 'aliceblue’)
Use Axes Methods (Accessors) to
5 #Set the face color of the axes backgrounds
access artists. [Link](facecolor = 'salmon')
[Link](facecolor = 'aquamarine')
Set/Modify the Artist properties after [Link](x)
6
accessing the artists [Link]()

Add Legend, Annotations if any and


7
Set Title to the Axes

119 DT Ch ChCh Ch Ch Ch Ch
METHODS OF RECTANGLE PATCH
Create a Figure instance Rectangle Patch Method Description
1 __str__(self ) Return str(self).
get_bbox(self ) Return the Bbox.
get_height(self ) Return the height of the rectangle.
Use Figure to create one or more Axes
2 get_patch_transform(self ) Return the Transform instance mapping patch
or Subplot coordinates to data coordinates.
get_path(self ) Return the vertices of the rectangle.
get_width(self ) Return the width of the rectangle.
Use Axes Helper Methods to add
3 get_x(self ) Return the left coordinate of the rectangle.
artists to respective containers within get_xy(self ) Return the left and bottom coords of the
rectangle as a tuple.
the Axes object.
get_y(self ) Return the bottom coordinate of the rectangle.
set_bounds(self, *args) Set the bounds of the rectangle as left, bottom,
Use Axes/Axis Helper Methods to width, height. The values may be passed as
4 separate parameters or as a tuple:
access and set x-axis and y-axis tick, set_height(self, h) Set the height of the rectangle.
tick labels and axis labels. 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 Axes Methods (Accessors) to
5
access artists. USE CASE SCENARIO LABEL PROPERTY
For Adding data labels on top of get_x(), get_y(), get_height(), get_width()
Set/Modify the Artist properties after the bars in bar plot
6
accessing the artists For User defined function axes property, figure property in user defined
function to retrieve the axes and figure
respectively.
Add Legend, Annotations if any and
7
Set Title to the Axes

120 DT Ch ChCh Ch Ch Ch Ch
DEMO EXAMPLE – APPLYING LEARNINGS TILL NOW

PREPARING SAMPLE DATA CODE SCRIPT PLOT OUTPUT


data = {'Water':465, import [Link] as plt
'Alchoholic Beverages':271, import numpy as np
'Carbonated soft drinks': 224, import [Link] as spines
'Milk and Dairy products': 260, colors = [ [Link].Dark2(x) for x in [Link](0, 1,
'New Drinks': 75, len(x))]
'Fruits and Vegetable Juices': 86}
x = list([Link]()) [Link]()
y = list([Link]()) %matplotlib inline
fig, ax = [Link](figsize = (10,6), frameon = True)
bars = [Link](x,y, color = colors)

• User defined function to StrTitle = 'Consumption of Packed beverages in


add data labels billion liters,\n by Beverage Type, Global, 2019'
• Uses Axes helper ax.set_title(StrTitle, size = 16)
methods to access the
individual bars and use Add_data_labels([Link])
its properties to add stylize_axes(ax)
texts
[Link]('[Link]', dpi=300, format='png',
• User defined function to bbox_inches='tight')
stylize the axes.
• Uses Axes helper Check slide
methods to access and
modify artist properties

121 DT Ch ChCh Ch Ch Ch Ch
CREATING USER DEFINED FUNCTION CREATING USER DEFINED FUNCTION TO ADD DATA LABELS IN BAR
TO STYLIZE AXES PLOTS
def stylize_axes(ax): # Adding Annotations to place data labels
def Add_data_labels(rect): #rect is [Link] object
#Making the axes background light gray for p in rect:
ax.set_facecolor('.85’) #Retrieving the Axes container of patch object
ax = rect[0].axes
# Setting the y limits
ax.set_ylim(0,ax.get_ylim()[1]*1.1) #Adding text iteratively on top of each bar
#Using get and set on x,y parameters of rectangle patch
#Making the tick lines disppear [Link]( x = p.get_x()+p.get_width()/2,
ax.tick_params(length=0) y = ax.get_ylim()[1]*0.01+ p.get_height(),
s = p.get_height(),
#Making the xtick labels rotated ha = 'center', size = 12)
for labels in ax.get_xticklabels() :
[Link](size = 13,rotation = 15,
rotation_mode = 'anchor’,
ha = 'right’)

#Setting grid lines to white color


[Link](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 [Link]()]

122 DT Ch ChCh Ch Ch Ch Ch
DEMO - USERDEFINED FUNCTIONS TO STYLIZE PLOTS AND ADD LABELS
# Doing the necessary imports
import [Link] as plt
import numpy as np
import [Link] as spines

[Link]()
%matplotlib inline

#create a list of rgba colors from a [Link] colormap


colors = [ [Link].Dark2(x) for x in [Link](0, 1, len(x))]

#Instantiating figure, axes


fig, ax = [Link](figsize = (10,6), frameon = True)

#Creating a bar plot by [Link] method


# Note the colors passed as the color argument
bars = [Link](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([Link])
stylize_axes(ax)

[Link]('[Link]', dpi=300, format='png', bbox_inches='tight')

123 DT Ch ChCh Ch Ch Ch Ch
Create a Figure instance
1

Use Figure to create one or more Axes


2
or Subplot

Use Axes Helper Methods to add


3
artists to respective containers within
the Axes object.

4
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick, TRANSFORMATIONS
tick labels and axis labels.

Use Axes Methods (Accessors) to


5
access artists.

Set/Modify the Artist properties after


6
accessing the artists

Add Legend, Annotations if any and


7
Set Title to the Axes

124 DT Ch ChCh Ch Ch Ch Ch
TRANSFORMATION FRAMEWORK
Create a Figure instance
1 Use Transformation Objects to easily move between coordinate systems
Coordinates Transformation object Description
Use Figure to create one or more Axes "data" [Link] The coordinate system for the data, controlled by xlim and ylim.
2 "axes" [Link] The coordinate system of the Axes; (0, 0) is bottom left of the axes,
or Subplot and (1, 1) is top right of the axes.
"subfigure" [Link] The coordinate system of the SubFigure; (0, 0) is bottom left of the
Use Axes Helper Methods to add subfigure, and (1, 1) is top right of the subfigure. If a figure has no
3 subfigures, this is the same as transFigure.
artists to respective containers within "figure" [Link] The coordinate system of the Figure; (0, 0) is bottom left of the figure,
the Axes object. and (1, 1) is top right of the figure.
"figure- fig.dpi_scale_trans The coordinate system of the Figure in inches; (0, 0) is bottom left of
inches" the figure, and (width, height) is the top right of the figure in inches.
Use Axes/Axis Helper Methods to
4 "display" None, The pixel coordinate system of the display window; (0, 0) is bottom
access and set x-axis and y-axis tick, or IdentityTransform() left of the window, and (width, height) is top right of the display
window in pixels.
tick labels and axis labels.
"xaxis", ax.get_xaxis_transform(), Blended coordinate systems; use data coordinates on one of the axis
"yaxis" ax.get_yaxis_transform() and axes coordinates on the other.
Use Axes Methods (Accessors) to *Matplotlib Release, 3.4.2, Page No. 227
5 *[Link]
access artists.

Set/Modify the Artist properties after


6
accessing the artists

Add Legend, Annotations if any and


7
Set Title to the Axes

125 DT Ch ChCh Ch Ch Ch Ch
TRANSFORMATION FRAMEWORK – THE COORDINATE SYSTEMS
Create a Figure instance
1 The commonly used coordinate systems are :
• Data
• Axes
Use Figure to create one or more Axes • Figure
2
or Subplot
All the above will be converted to Display coordinates by appropriate transformation
Use Axes Helper Methods to add objects.
3 Code
artists to respective containers within
the Axes object.

Use Axes/Axis Helper Methods to


4
access and set x-axis and y-axis tick,
tick labels and axis labels.

Use Axes Methods (Accessors) to


5
access artists.

Set/Modify the Artist properties after


6
accessing the artists

Add Legend, Annotations if any and


7
Set Title to the Axes

126 DT Ch ChCh Ch Ch Ch Ch
TRANSFORMATION FRAMEWORK – TRANSFORMATION OBJECTS
Create a Figure instance
1 Transformation Objects to easily move between coordinate systems

Userland data
Use Figure to create one or more Axes
2 coordinate system
or Subplot [Link]
axes coordinate
Use Axes Helper Methods to add System
3 [Link]
artists to respective containers within display coordinate
the Axes object. figure coordinate
System System
[Link]
Use Axes/Axis Helper Methods to
4 figure inches System
access and set x-axis and y-axis tick,
tick labels and axis labels. fig.dpi_scale_trans
display coordinate
System
Use Axes Methods (Accessors) to
5
access artists. Ø All of the transformation objects in the table above take inputs in their coordinate
system and transform the input to the display coordinate system.
Set/Modify the Artist properties after Ø transform is the property common to all artists.
6 Ø While it works under the hood not requiring to be explicitly mentioned, but for
accessing the artists
customization and more control, it is immensely powerful.
Ø Use case :
Add Legend, Annotations if any and Ø Adding annotations/text
7
Set Title to the Axes Ø Adding horizontal/vertical spans
Ø Legend positioning

127 DT Ch ChCh Ch Ch Ch Ch
BLENDED TRANSFORMATIONS - EXAMPLE
Create a Figure instance
1 import numpy as np
import [Link] as transforms
import [Link] as plt
Use Figure to create one or more Axes import [Link] as mpatches
2
or Subplot fig, ax = [Link](figsize = (6,5))
x = [Link](1000)
Use Axes Helper Methods to add
3 [Link](x, 30)
artists to respective containers within ax.set_title(r'$\sigma=1 \/ \dots \/ \sigma=2$', fontsize=16)
the Axes object.
# the x coords of this transformation are data, and the y
coord are axes
Use Axes/Axis Helper Methods to trans = transforms.blended_transform_factory( [Link],
4 [Link])
access and set x-axis and y-axis tick,
x to be in data
tick labels and axis labels. # highlight the 1..2 std dev region with a span.
coordinates
# x to be in data coordinates
# y to span from 0..1 in axes coords.
Use Axes Methods (Accessors) to
5 y to span from 0 -1 in
rect = [Link]((1, 0), width=1, height=1,
access artists. transform=trans,
axes coords. color='yellow', alpha=0.5)
ax.add_patch(rect)
Set/Modify the Artist properties after [Link](0.5,0.9, s = "Blended Coordinates Transformation -
6
accessing the artists Demo", transform = [Link],
ha = 'center', fontsize = 15,
fontweight = 'medium’)
Add Legend, Annotations if any and
7 [Link](fc = 'salmon', alpha = 0.1)
Set Title to the Axes plt.tight_layout(rect = [0,0,1,0.9])

[Link]
128 DT Ch ChCh Ch Ch Ch Ch
Create a Figure instance
1

Use Figure to create one or more Axes


2
or Subplot

Use Axes Helper Methods to add


3
artists to respective containers within
the Axes object.

4
Use Axes/Axis Helper Methods to
access and set x-axis and y-axis tick, COLORS
tick labels and axis labels.

Use Axes Methods (Accessors) to


5
access artists.

Set/Modify the Artist properties after


6
accessing the artists

Add Legend, Annotations if any and


7
Set Title to the Axes

129 DT Ch ChCh Ch Ch Ch Ch
Specifying Colors – Acceptable Formats
Create a Figure instance
1 Format Example
RGB or RGBA (red, green, blue, alpha) tuple of float values in a • (0.1, 0.2, 0.5)
1. • (0.1, 0.2, 0.5, 0.3)
closed interval [0, 1].
Use Figure to create one or more Axes
2 • '#0f0f0f'
or Subplot 2. Case-insensitive hex RGB or RGBA string. • '#0f0f0f80'

Case-insensitive RGB or RGBA string equivalent hex shorthand of • '#abc' as '#aabbcc'


Use Axes Helper Methods to add 3. • '#fb1' as '#ffbb11'
3 duplicated characters.
artists to respective containers within • '0.8' as light gray
the Axes object. String representation of float value in closed interval [0, 1] for black
4. • '0' as black
and white, respectively.
• '1' as white
• 'b' as blue
Use Axes/Axis Helper Methods to
4 • 'g' as green
access and set x-axis and y-axis tick, Single character shorthand notation for shades of colors. • 'r' as red
Note : 'c' as cyan
tick labels and axis labels. 5.
The colors green, cyan, magenta, and yellow do not coincide with •

'm' as magenta
X11/CSS4 colors. • 'y' as yellow
Use Axes Methods (Accessors) to • 'k' as black
5 • 'w' as white
access artists. • 'aquamarine'
6. Case-insensitive X11/CSS4 color name with no spaces. • 'mediumseagreen'
Set/Modify the Artist properties after • 'xkcd:sky blue'
6 7.
Case-insensitive color name from xkcd color
• 'xkcd:eggshell'
accessing the artists survey with 'xkcd:'prefix.

Add Legend, Annotations if any and


7
Set Title to the Axes

130 DT Ch ChCh Ch Ch Ch Ch
Format Example
Create a Figure instance 8. Case-insensitive Tableau Colors from 'T10' categorical palette. 'tab:blue'
1 Note : This is the default color cycle. 'tab:orange'
'tab:green'
'tab:red'
Use Figure to create one or more Axes 'tab:purple'
2 'tab:brown'
or Subplot 'tab:pink'
'tab:gray'
Use Axes Helper Methods to add 'tab:olive'
3 'tab:cyan'
artists to respective containers within
the Axes object. 9. "CN" color spec where 'C' precedes a number acting as an index into the 'C0'
default property cycle. 'C1'
Note : Matplotlib indexes color at draw time and defaults to black if cycle
Use Axes/Axis Helper Methods to
4 does not include color.
access and set x-axis and y-axis tick,
The default property cycle is
tick labels and axis labels. rcParams["axes.prop_cycle"] (default: cycler('color',['#1f77b4', '#ff7f0e', '#2ca
02c', '#d62728', '#9467bd','#8c564b', '#e377c2', '#7f7f7f', '#bcbd22', '#17becf
Use Axes Methods (Accessors) to ']))
5
access artists. Ø 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
Set/Modify the Artist properties after of the artist also becomes important.
6
accessing the artists

Add Legend, Annotations if any and


7
Set Title to the Axes

131 DT Ch ChCh Ch Ch Ch Ch
Create a Figure instance Exploring the Color Charts
1

Use Figure to create one or more Axes


2
or Subplot Ø Basic (8 colors)
Ø Tableau Palette (10 colors)
Use Axes Helper Methods to add Ø CSS colors (140+ colors) Handy go to reference to
3 Ø xkcd colors (954 colors) pick colors
artists to respective containers within
the Axes object. [Link]

Use Axes/Axis Helper Methods to


4 USE CASE SCENARIO ARTIST PROPERTY
access and set x-axis and y-axis tick,
Assign face color, edge color of figure, axes edgecolor (ec) , facecolor (fc)
tick labels and axis labels.
Assign color to texts, axis labels, tick labels color or c
Assign color to marker edge and face in line plot Markeredgecolor (mec),
Use Axes Methods (Accessors) to
5 markerfacecolor(mfc)
access artists.
Initialize the color in bar plots, pie charts. Use edgecolor (ec) , facecolor (fc)
Tableau palette. If more number of categories
Set/Modify the Artist properties after present, then colormaps and external libraries like
6 [palettable] and [colorcet].
accessing the artists
Setting the property cycle of the axes ax.set_prop_cycle(color=['red',
(Note : The property cycle controls the style 'green', 'blue'])
Add Legend, Annotations if any and properties such as color, marker and linestyle of
7 future plot commands. The style properties of data
Set Title to the Axes
already added to the Axes are not modified.)

132 DT Ch ChCh Ch Ch Ch Ch
Create a Figure instance
1

Use Figure to create one or more Axes


2
or Subplot

Use Axes Helper Methods to add


3
artists to respective containers within
the Axes object.

Use Axes/Axis Helper Methods to


4
access and set x-axis and y-axis tick,
tick labels and axis labels.

Use Axes Methods (Accessors) to


5
access artists.

Set/Modify the Artist properties after


6
accessing the artists

Add Legend, Annotations if any and


7
Set Title to the Axes

133 DT Ch ChCh Ch Ch Ch Ch
Create a Figure instance
1
Axis accessor method Description
get_scale The scale of the Axis, e.g., 'log' or 'linear'
Use Figure to create one or more Axes get_view_interval The interval instance of the Axis view limits
2 get_data_interval The interval instance of the Axis data limits
or Subplot
get_gridlines A list of grid lines for the Axis
get_label The Axis label - a Text instance
Use Axes Helper Methods to add get_offset_text The Axis offset text - a Text instance
3
artists to respective containers within get_ticklabels A list of Text instances - keyword minor=True|False
get_ticklines A list of Line2D instances - keyword minor=True|False
the Axes object. get_ticklocs A list of Tick locations - keyword minor=True|False
get_major_locator The [Link] instance for major ticks
Use Axes/Axis Helper Methods to get_major_formatter The [Link] instance for major ticks
4 get_minor_locator The [Link] instance for minor ticks
access and set x-axis and y-axis tick, get_minor_formatter The [Link] instance for minor ticks
tick labels and axis labels. 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
Use Axes Methods (Accessors) to
5
access artists.

Set/Modify the Artist properties after


6
accessing the artists

Add Legend, Annotations if any and


7
Set Title to the Axes

134 DT Ch ChCh Ch Ch Ch Ch
Create a Figure instance
1

Use Figure to create one or more Axes


2
or Subplot

Use Axes Helper Methods to add


3
artists to respective containers within
the Axes object.

Use Axes/Axis Helper Methods to [Link]


4
access and set x-axis and y-axis tick,
tick labels and axis labels.

Use Axes Methods (Accessors) to


5
access artists.

Set/Modify the Artist properties after


6
accessing the artists

Add Legend, Annotations if any and


7
Set Title to the Axes

135 DT Ch ChCh Ch Ch Ch Ch
Create a Figure instance
1

Use Figure to create one or more Axes


2
or Subplot

Use Axes Helper Methods to add


3
artists to respective containers within
the Axes object.

Use Axes/Axis Helper Methods to [Link]


4
access and set x-axis and y-axis tick,
tick labels and axis labels.

Use Axes Methods (Accessors) to


5
access artists.

Set/Modify the Artist properties after


6
accessing the artists

Add Legend, Annotations if any and


7
Set Title to the Axes

136 DT Ch ChCh Ch Ch Ch Ch
Create a Figure instance
1

Use Figure to create one or more Axes


2
or Subplot

Use Axes Helper Methods to add


3
artists to respective containers within
the Axes object.

Use Axes/Axis Helper Methods to


4
access and set x-axis and y-axis tick,
tick labels and axis labels.

Use Axes Methods (Accessors) to


5
access artists.

Set/Modify the Artist properties after


6
accessing the artists

Add Legend, Annotations if any and


7
Set Title to the Axes

137 DT Ch ChCh Ch Ch Ch Ch
Create a Figure instance
1

Use Figure to create one or more Axes


2
or Subplot

Use Axes Helper Methods to add


3
artists to respective containers within
the Axes object.

Use Axes/Axis Helper Methods to


4
access and set x-axis and y-axis tick,
tick labels and axis labels.

Use Axes Methods (Accessors) to


5
access artists.

Set/Modify the Artist properties after


6
accessing the artists

Add Legend, Annotations if any and


7
Set Title to the Axes

138 DT Ch ChCh Ch Ch Ch Ch
Create a Figure instance
1

Use Figure to create one or more Axes


2
or Subplot

Use Axes Helper Methods to add


3
artists to respective containers within
the Axes object.

Use Axes/Axis Helper Methods to


4
access and set x-axis and y-axis tick,
tick labels and axis labels.

Use Axes Methods (Accessors) to


5
access artists.

Set/Modify the Artist properties after


6
accessing the artists

Add Legend, Annotations if any and


7
Set Title to the Axes

139 DT Ch ChCh Ch Ch Ch Ch
Create a Figure instance
1

Use Figure to create one or more Axes


2
or Subplot

Use Axes Helper Methods to add


3
artists to respective containers within
the Axes object.

Use Axes/Axis Helper Methods to


4
access and set x-axis and y-axis tick,
tick labels and axis labels.

Use Axes Methods (Accessors) to


5
access artists.

Set/Modify the Artist properties after


6
accessing the artists

Add Legend, Annotations if any and


7
Set Title to the Axes

140 DT Ch ChCh Ch Ch Ch Ch
Create a Figure instance
1

Use Figure to create one or more Axes


2
or Subplot

Use Axes Helper Methods to add


3
artists to respective containers within
the Axes object.

Use Axes/Axis Helper Methods to


4
access and set x-axis and y-axis tick,
tick labels and axis labels.

Use Axes Methods (Accessors) to


5
access artists.

Set/Modify the Artist properties after


6
accessing the artists

Add Legend, Annotations if any and


7
Set Title to the Axes

141 DT Ch ChCh Ch Ch Ch Ch
Code

Create a Figure instance


1

Use Figure to create one or more Axes


2
or Subplot

Use Axes Helper Methods to add


3
artists to respective containers within
the Axes object.

Use Axes/Axis Helper Methods to


4
access and set x-axis and y-axis tick,
tick labels and axis labels.

Use Axes Methods (Accessors) to


5
access artists.

Set/Modify the Artist properties after


6
accessing the artists

Add Legend, Annotations if any and


7
Set Title to the Axes

142 DT Ch ChCh Ch Ch Ch Ch
What are color Built-in colormaps accessible via [Link].get_cmap to map data on to
maps ? color values.

Classes of
Sequential, Diverging, Cyclic ,Qualitative
colormaps

Accessing a Colour [Link]([Link](0,1, number of colors))


map using colors attribute of listed colormap followed by indexing if required

For categorical bar plots and pie charts, use qualitative colormaps such as
Use Case Scenarios tab10 (10 colors) , tab20, tab20b, tab20c (20 colors), Paired (12 colors)

Additional
External libraries like [palettable] and [colorcet]
Colormaps
Note : Colormaps is a very rich immersive topic. Check out the official matplotlib website to dwelve deeper.

143 DT Ch ChCh Ch Ch Ch Ch
Classes of colormaps Based on Function
Colormap Category Use Case
Sequential: change in lightness and often saturation of for representing information that has ordering.
color incrementally, often using a single hue;
Diverging: change in lightness and possibly saturation when the information being plotted has a critical
of two different colors that meet in the middle at an middle value, such as topography or when the
unsaturated color data deviates around zero.
Cyclic: change in lightness of two different colors that for values that wrap around at the endpoints,
meet in the middle and beginning/end at an such as phase angle, wind direction, or time of
unsaturated color; day.
Qualitative: often are miscellaneous colors to represent information which does not have
ordering or relationships.

Note : Colormaps is a very rich immersive topic. Check out the official matplotlib website to dwelve deeper.
[Link]
[Link]

144 DT Ch ChCh Ch Ch Ch Ch
Range of Matplotlib
Colormaps

145 DT Ch ChCh Ch Ch Ch Ch
Note : All colormaps can be reversed by appending _r.

146 DT Ch ChCh Ch Ch Ch Ch
Note : All colormaps can be reversed by appending _r.

147 DT Ch ChCh Ch Ch Ch Ch
Lightness of Matplotlib
Colormaps

148 DT Ch ChCh Ch Ch Ch Ch
149 DT Ch ChCh Ch Ch Ch Ch
150 DT Ch ChCh Ch Ch Ch Ch
151 DT Ch ChCh Ch Ch Ch Ch
ACCESSING COLORS FROM A MATPLOTLIB COLORMAP is a 1D Array of colours.
COLORMAP [Link] module provides functions and classes for color
specification conversions, and mapping data/numbers onto colours on a
[Link]([Link](start = 0, stop = 1, num = 8 ))
colormap.
0 1
import numpy as np
import [Link] as plt
from matplotlib import cm #[Link] module
0.00 0.14 0.28 0.42 0.56 0.70 0.85 1
from [Link] import ListedColormap, LinearSegmentedColormap

#Get 8 colours from viridis colormap


viridis = cm.get_cmap('viridis', 8)
OR
[Link]([Link](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([Link](0, 0.5, 4))

# Get 4# colors (4th-7th index) from the colormaps


viridis(range(4,8))
OR
viridis([Link](0.5, 1, 4))

*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 [Link].
152 DT Ch ChCh Ch Ch Ch Ch
CREATING LISTED COLORMAP FROM
# Necessary imports – Especially ListedColormap Class
EXISTING MATPLOTLIB COLORMAPs [1] : import numpy as np
import [Link] as plt
from matplotlib import cm
Linear Segmented from [Link] import ListedColormap
Listed ColorMaps
ColorMaps from [Link] import LinearSegmentedColormap

# Note : cm.get_cmap() returns a colormap


• Sequential • Perceptually uniform
• Diverging sequential [2] : type(cm.get_cmap('YlGn', 256))
• Cyclic • Qualitative [2] : [Link]

[3] : type(cm.get_cmap(‘viridis’, 20))


[3] : [Link]
Colormap object generated from a list of colors.
Ø most useful when indexing directly into a colormap
Ø used to generate special colormaps for ordinary [4] : type([Link]([Link](0, 1, 8))) #returns numpy array
mapping. [4] : [Link]

# Creating a Listed Colormap


USE CASE SCENARIO PARAMETER/PROPERTY
Setting cmap in scatter charts cmap kwd argument [5] : YlGn_New = cm.get_cmap('YlGn', 256)
Initialize the color in bar plots, pie facecolor property can take a
charts with colors of listed single color or list of colors [6] : ListedColormap(YlGn_New([Link](0,1,10)))
colormap. Check out tab10, #colors = [Link][:8] [6] : <[Link] at 0x7ffdd4625700>
tab20. Also, External libraries like #colors = [Link]
[palettable] and [colorcet]. [7] : viridis_short = cm.get_cmap(‘viridis’,10)
Setting the property cycle of the ax.set_prop_cycle(color=[Link] ListedColormap(viridis_short([Link](0,1,10)))
axes [Link]) [8] : <[Link] at 0x7ffdd4626700>
153 DT Ch ChCh Ch Ch Ch Ch
CODE CHUNK TO CREATE LISTED COLORMAPS AND ACCESSING COLORS

from [Link] import ListedColormap


colarray = [Link](range(256))
newcmap = ListedColormap(colarray)

#Colormap can be called with an integer array, or with a float array


between 0 and 1.

[Link] #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([Link](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 = [ [Link].tab20c(x) for x in [Link](0, 1, len([Link]()) ]

cmap = ListedColormap(["darkorange", "gold", "lawngreen",


"lightseagreen"])

154 DT Ch ChCh Ch Ch Ch Ch
CODE SCRIPT TO CREATE PSEUDO COLOR PLOT # Doing the necessary imports
import numpy as np
import [Link] as plt
from matplotlib import cm
from [Link] import ListedColormap
[Link]()
%matplotlib inline
#
colarray = [Link](range(256))
newcmp = ListedColormap(colarray)

#Creating the 1X256 Numpy Array (Matrix in Matlab) of float values from 0..1
Z = [Link](0,1,256)[[Link],:]

fig, (ax0, ax1) = [Link](2, 1, figsize = (10,3))


READY TO USE CODE TO ACCESS COLORS FROM CMAPS

#colors = [Link].Dark2(range(15)) #Creating a pseudocolor plot without distinguishable edge between values
#colors = [Link].tab20(range(15)) c = [Link](Z,cmap = newcmp)
#colors = [Link].nipy_spectral([Link](0,0.9,len([Link]()))) ax0.set_title('default: no edges', fontsize = 20)
#colors = [Link]([Link](0.2,1,len([Link]())))
#Creating a pseudocolor plot with distinguishable edge between the values
#colors = [ [Link](x) for x in [Link](0, 1, len([Link]()))] c = [Link](Z, cmap = newcmp, edgecolors='k', linewidths=1)
#colors = [ [Link].Set3(x) for x in [Link](0, 1, len([Link]()))] ax1.set_title('thick edges',fontsize = 20)

.colors attribute for listed colormaps fig.tight_layout()


#colors = [Link] [Link]('Plasma_256_colours.png',dpi=300, format='png’,
#colors = [Link] bbox_inches='tight')

COLORS FROM EXTERNAL LIBRARY PALETTABLE Ø Refer to below Code from Matplotlib Gallery. Note slight tweaking was done to
from [Link] import Bold_10 change the shape of the rectangular grid by using [Link]
#colors = Bold_10.mpl_colors[:5] Ø [Link]
colors = Bold_10.mpl_colors #sphx-glr-gallery-images-contours-and-fields-pcolor-demo-py

155 DT Ch ChCh Ch Ch Ch Ch
Create a Figure instance
1

Use Figure to create one or more Axes


2
or Subplot

Use Axes Helper Methods to add


3
artists to respective containers within
the Axes object.

Use Axes/Axis Helper Methods to


4
access and set x-axis and y-axis tick,
tick labels and axis labels.

Use Axes Methods (Accessors) to


5
access artists.

Set/Modify the Artist properties after


6
accessing the artists

Add Legend, Annotations if any and


7
Set Title to the Axes

156 DT Ch ChCh Ch Ch Ch Ch
ASPECTS OF LEGEND
• legend entry
• The legend module defines the Legend class responsible for
• legend handles
Create a Figure instance drawing legends associated with axes and/or figures.
1 • The Legend class is a container of legend handles and legend • legend labels
texts. • legend keys
1 CONTROLLING THE LEGEND ENTRIES h,l = ax.get_legend_handles_labels()
Use Figure to create one or more Axes
2
or Subplot
• Not all handles can be turned into legend entries automatically, • [Link]
so it is often necessary to create an artist which can. • [Link]
Use Axes Helper Methods to add • Legend handles don't have to exist on the Figure or Axes in
3 order to be used.
artists to respective containers within
the Axes object.
2 CREATING PROXY ARTISTS SPECIFICALLY FOR THE LEGEND

Use Axes/Axis Helper Methods to • loc


4 • Next up is specifying the location of the legend
bbox_to_anchor
access and set x-axis and y-axis tick, • Two parameters to control this : •
tick labels and axis labels. • loc • bbox_transform
• bbox_to_anchor
3 CONTROLLING THE LEGEND LOCATION, PLACEMENT
Use Axes Methods (Accessors) to
5
access artists.
• The legend handler map specifies how to create legend
handles from artists (lines, patches, etc.) in the axes or figures.
Set/Modify the Artist properties after • Custom legend handlers can be defined to support arbitrary
6 objects not covered by default legend handlers.
accessing the artists
4 DEFINING CUSTOM LEGEND HANDLERS

Add Legend, Annotations if any and


7
Set Title to the Axes Matplotlib, Release 3.4.2, Page No. 2567, 2245, 1469
[Link]
[Link] Legend Demo

157 DT Ch ChCh Ch Ch Ch Ch
Create a Figure instance
1 Adding Legend to Axes handles [list of Artist]
A list of Artists (lines, patches) to be added to the legend.
Call signatures:
• legend() labels [list of str]
Use Figure to create one or more Axes
2 • legend(labels) A list of labels to show next to the artists. The length of
or Subplot handles and labels should be the same. If they are not,
• legend(handles, labels) they are truncated to the smaller of both lengths.

Use Axes Helper Methods to add Read more on legend entry, key, label and handle
3
artists to respective containers within
[Link]() Automatic detection of elements • Suffices for most use-cases
the Axes object. to be shown in the legend • Requires explicitly labeling the artist either at
the time of creation or before calling legend
• Artist whose label either starts with
Use Axes/Axis Helper Methods to
4 underscore or does not have label property
access and set x-axis and y-axis tick, set will be ignored from the Legend
tick labels and axis labels. [Link](labels) Labeling existing plot elements This method is discouraged owing to an
implicit ordering of labels and artists.
Use Axes Methods (Accessors) to [Link](handles, Explicitly defining the elements • Gives Total Control over the artists displayed
5 labels) in the legend in the legend
access artists.
• This method is explicit and will accept the
handles and labels at face value.
Set/Modify the Artist properties after • This implies the labels will override the label
6 names assigned at the time of creation.
accessing the artists • Also, one can pass additional artists or
subset of artist to the handles list.
Add Legend, Annotations if any and
7
Set Title to the Axes Matplotlib, Release 3.4.2, Page No. 2567, 2245, 1469
[Link]
[Link] Legend Demo

158 DT Ch ChCh Ch Ch Ch Ch
1
Create a Figure instance Important Terminologies
legend entry A legend is made up of one or more legend
Use Figure to create one or more Axes entries. An entry is made up of exactly one key
2
or Subplot
and one label.
Use Axes Helper Methods to add legend key The colored/patterned marker to the left of
3 each legend label.
artists to respective containers within
the Axes object. legend label The text which describes the handle
represented by the key.
Use Axes/Axis Helper Methods to
4 legend handle The original object which is used to generate an
access and set x-axis and y-axis tick,
tick labels and axis labels. appropriate entry in the legend.

Use Axes Methods (Accessors) to


5
access artists.

Set/Modify the Artist properties after


6
accessing the artists

Add Legend, Annotations if any and


7
Set Title to the Axes Matplotlib, Release 3.4.2, Page No. 2567, 2245, 1469
[Link]
[Link] Legend Demo

159 DT Ch ChCh Ch Ch Ch Ch
Create a Figure instance Automatic detection of elements to be shown in the legend
1
Artists with valid labels are automatically detected.
To check the list of handles detected, use ax.get_legend_handles_labels()
Use Figure to create one or more Axes
2
or Subplot import [Link] as plt
import numpy as np
%matplotlib inline
Use Axes Helper Methods to add [Link]()
3 [Link](450)
artists to respective containers within
x = ['A', 'B','C','D']
the Axes object. y = [Link](100,400,4)

fig, ax = [Link]()
Use Axes/Axis Helper Methods to
4 p = [Link](x,y, color = [Link])
access and set x-axis and y-axis tick,
tick labels and axis labels. #Setting labels to each of the bars
[[Link](label = text) for text,bar in zip(x,p)]

Use Axes Methods (Accessors) to print(ax.get_legend_handles_labels())


5
access artists. #Calling the legend function for automatic
detection of elements with labels to be shown
Set/Modify the Artist properties after [Link]()
6
accessing the artists

Add Legend, Annotations if any and


7
Set Title to the Axes Matplotlib, Release 3.4.2, Page No. 2567, 2245, 1469
[Link]
[Link] Legend Demo

160 DT Ch ChCh Ch Ch Ch Ch
1
Create a Figure instance Explicitly defining the elements in the legend Labels Omitted

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
Use Figure to create one or more Axes
2 respectively:
or Subplot import [Link] as plt
import numpy as np

Use Axes Helper Methods to add [Link]()


3 [Link](450)
artists to respective containers within
x = ['A', 'B','C','D']
the Axes object. y = [Link](100,400,4)

fig, ax = [Link]()
Use Axes/Axis Helper Methods to
4 p = [Link](x,y, color = [Link])
access and set x-axis and y-axis tick,
tick labels and axis labels. #Setting labels to each of the bars
[[Link](label = text) for text,bar in zip(x,p)]

Use Axes Methods (Accessors) to handles = []


5 for bar in p:
access artists.
if bar.get_label() not in [‘A',’D']:
Context : [Link](bar)
Set/Modify the Artist properties after EXCLUDE FROM LEGEND
6 ARTISTs WITH LABELS ’A’ or ‘D’ print(handles) #Check the handles list
accessing the artists [Link](handles = handles)
Note : Its okay to omit labels from the call
signature if all the handles have valid labels. [Link]('Explicitly_Passing_Iterable of \
Add Legend, Annotations if any and
7 [Link]',dpi=300, format='png',
Set Title to the Axes Infact, either give the correct desired labels in this bbox_inches='tight')
call corresponding to all the handles or omit
labels altogether.

161 DT Ch ChCh Ch Ch Ch Ch
1
Create a Figure instance Explicitly defining the elements in the legend Labels Included

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
Use Figure to create one or more Axes
2 respectively:
or Subplot import [Link] as plt
import numpy as np

Use Axes Helper Methods to add [Link]()


3 [Link](450)
artists to respective containers within
x = ['A', 'B','C','D']
the Axes object. y = [Link](100,400,4)

fig, ax = [Link]()
Use Axes/Axis Helper Methods to
4 p = [Link](x,y, color = [Link])
access and set x-axis and y-axis tick,
tick labels and axis labels. #Setting labels to each of the bars
[[Link](label = text) for text,bar in zip(x,p)]

Use Axes Methods (Accessors) to handles = []


5 for bar in p:
access artists.
if bar.get_label() in ['B','C’]: #filter ‘B’, ‘C’
Context : [Link](bar)
Set/Modify the Artist properties after • INCLUDE IN LEGEND, ARTISTs WITH LABELS
6 ’B’ or ‘C’ labels = ['Brazil', 'Chile’] #Creating labels
accessing the artists • TWEAK ‘B’ AND ‘C’ LABELS IN LEGEND [Link](handles = handles, labels = labels)

Note : If labels is included in call signature, then [Link]('Explicitly_Passing_Iterable_ \


Add Legend, Annotations if any and
7 labels in the labels list will override all pre- handles_labels.png',dpi=300, format='png',
Set Title to the Axes existing labels of the handles. bbox_inches='tight')

162 DT Ch ChCh Ch Ch Ch Ch
import [Link] as mpatches
import [Link] as mlines Creating Proxy Artists and Adding in the legend Demo

[Link]() 1. Use ax.get_legend_handles_labels() to get existing handles and labels


fig, ax = [Link](figsize = (7,4)) 2. Create instances of patch and Line2D without drawing them on the
axes. Also, add label in initialization itself
# where some data has already been plotted to ax
3. Add the patch and Line2D to the list of handles from step 1 Steps
4. Call the [Link]() method with below signature :
handles, labels = ax.get_legend_handles_labels()
[Link](handles=handles, **kwargs)
# manually define a new patch
patch = [Link](color='grey', label='Manual Label patch style’)
[Link](handles=handles, labels = labels , **kwargs)

# 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


[[Link](obj) for obj in [patch, line, marker1, marker2]]

# plot the legend


[Link](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 Matplotlib, Release 3.4.2, Page No. 2567, 2245, 1469
leg._legend_title_box._text.set_color('Brown') [Link]
[Link]('Legend_Manual_positioning.png',dpi=150, format='png’, [Link] Legend Demo
bbox_inches='tight')

163 DT Ch ChCh Ch Ch Ch Ch
1
Create a Figure instance Controlling Legend Location, Placement Conceptual

• Use loc in conjunction with bbox_to_anchor


Use Figure to create one or more Axes
2
or Subplot
2 Tuple of 4 Tuple of bbox(x, y, width, height)
OR
Use Axes Helper Methods to add floats floats to place legend
3
artists to respective containers within Specifies the location Represents a point at Represents a bounding box within a corner of
the Axes object. of the legend which the legend which the legend will be placed. Corner is
corner specified by loc specified by loc.
will be anchored.
Use Axes/Axis Helper Methods to
4 bbox – a box
access and set x-axis and y-axis tick, bbox - a point
tick labels and axis labels. upper left upper center upper right
Legend
loc = “upper left” --- A
Use Axes Methods (Accessors) to * B center left center center right
5
access artists.
lower left lower center lower right
Set/Modify the Artist properties after
6
accessing the artists
Note : Use transform object in bbox_transform argument to be explicit about the coordinate system for the
Add Legend, Annotations if any and
7 bbox_to_anchor arguments
Set Title to the Axes

164 DT Ch ChCh Ch Ch Ch Ch
1
Create a Figure instance Controlling Legend Location, Placement Demo

Axes Legend Figure Legend #Setting labels to each of the bars


loc = 'upper left’ loc = 'upper right’
Use Figure to create one or more Axes bbox_to_anchor = (1,1)
[[Link](label = text) for text,bar in zip(x,p)]
bbox_to_anchor = (0,1)
2
or Subplot #Creating handles list
handles = []
for bar in p:
Use Axes Helper Methods to add
3 if bar.get_label() in ['B','C']:
artists to respective containers within [Link](bar)
the Axes object.
#Creating labels list
labels = ['Brazil', 'Chile’]
Use Axes/Axis Helper Methods to
4 #Creating Axes Legend
access and set x-axis and y-axis tick, [Link](handles = handles, labels = labels,
tick labels and axis labels. loc = 'upper left',
bbox_to_anchor = (0,1),
bbox_transform = [Link])
Use Axes Methods (Accessors) to
5 leg = ax.get_legend()
access artists. import [Link] as plt leg.set_title('South America')
import numpy as np
#Creating Figure Legend
Set/Modify the Artist properties after [Link]() [Link](handles = handles, labels = labels,
6 [Link](450)
accessing the artists loc = 'upper right’,
x = ['A', 'B','C','D'] bbox_to_anchor = (1,1))
y = [Link](100,400,4) [Link](fc = 'lightyellow’)
Add Legend, Annotations if any and
7 fig, ax = [Link]() [Link]('Legend_location.png',dpi=300,
Set Title to the Axes p = [Link](x,y, color = [Link]) format='png', bbox_inches='tight')

165 DT Ch ChCh Ch Ch Ch Ch
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 [Link] 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 DT Ch ChCh Ch Ch Ch Ch
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

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.
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 DT Ch ChCh Ch Ch Ch Ch
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 :
[Link]

168 DT Ch ChCh Ch Ch Ch Ch
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.

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 to make this review self contained. However, I would recommend to go to official
documentation of Pandas and Numpy for details.

169 DT Ch ChCh Ch Ch Ch Ch
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.

With this, I wrap up this first edition and wish you the very best !

Bhaskar
Aug’2021

170 DT Ch ChCh Ch Ch Ch Ch
THANK YOU !
bhaskarjroy1605@[Link]
[Link]/in/bhaskar-j-roy-1605

Matplotlib Complete Review Link


Other Interests : Excel VBA | Documentary Film1 | Documentary Film2

171 DT Ch ChCh Ch Ch Ch Ch
DATA
STRUCTURES
OVERVIEW

172 DT Ch ChCh Ch Ch Ch Ch
Overview of Data Structures Pandas
• Python • Provides efficient implementation of a
• Numpy DataFrame.
• DataFrames are essentially multidimensional
Series DataFrame Index
• Pandas arrays with attached row and column labels, and
often with heterogeneous types and/or missing Popular tool for data wrangling and analysis
data • Grouping (Split-Apply-Combine)
• Handling missing data
NumericIndex
• Powerful data wrangling operations similar to
• Input/output support
Indexing
ones in database and spreadsheet framework
Numpy • Data Visualisation Indexing is fundamental
• Integration with Numpy, Matplotlib, Scipy, CategoricalIndex to Pandas and is what
and Scikit-learn makes retrieval and
• Python-based library for mathematical
computations and processing arrays. ndarray • Check access to data much
faster compared to
• Homogenous Arrays of Fixed size. • Pandas online documention IntervalIndex
• Vectorization and Broadcasting makes numpy (N dimensional Array) • Stackoverflow other tools. It is crucial
to set an appropriate
fast. • Python Data Science Handbook
index to optimize
• Check Official Link, Scipy Lectures MultiIndex performance
• Check “Numerical Python: Scientific Computing
(Check A Python Data Analyst’s
and Data Science Applications with Numpy, •Pandas is implemented on top of NumPy
Toolkit by Gayathri Rajagopalan)
SciPy and Matplotlib” by Robert Johansson arrays.
Pandas •A Pandas dataframe is used to store a
DatetimeIndex
Python structured dataset. It is more like a
spreadsheet than an array..
For analyzing data and building models, arrays is a vital data structure. •extension package to Python for multi- TimedeltaIndex
dimensional arrays
The inbuilt data types and containers in Python cannot be restructured into more •closer to hardware (efficiency)
than one dimension, and also do not lend themselves to complex computations. NumPy provides:
•designed for scientific computation
(convenience) PeriodIndex
List Tuples Dictionaries Sets •Also known as array oriented computing
•high-level number objects: integers,
floating point
Python objects:
•containers: lists (costless insertion and
These Containers are also called iterables. They are unidimensional. append), dictionaries (fast lookup)
Check
• Learning Python by Mark Lutz
• Fluent Python by Luciano Ramalho

173 DT Ch ChCh Ch Ch Ch Ch
Operators in Python
Arithmetic Operators Logical Operators : used to combine conditional statements
Operator Name Example Operator Description Example
+ Addition x+y and Returns True if both statements are true x <15 and x > 5
- Subtraction x-y or Returns True if one of the statements is true x < 5 or y <10
* Multiplication x*y not Reverse the result, returns False if the result is true not(x < 5 or y <10)
/ Division x/y
% Modulus x%y Membership Operators : used to test if a sequence is presented in an objet
** Exponentiation x ** y Operator Description Example
// Floor division x // y in Returns True if a sequence with the specified x in y
value is present in the object
Assignment Operators
Operator Example Same As not in Returns True if a sequence with the specified x not in y
value is not present in the object
= x=5 x=5
+= x += 3 x=x+3 Identity Operators : used to compare the objects, not if they are equal, but if they are
-= x -= 3 x=x-3 actually the same object, with the same memory location:
*= x *= 3 x=x*3
Operator Description Example
/= x /= 3 x=x/3
is Returns True if both variables are the same object x is y
%= x %= 3 x=x%3
Returns True if both variables are not the same
//= x //= 3 x = x // 3 is not x is not y
object
**= x **= 3 x = x ** 3
&= x &= 3 x=x&3 Difference between float division and floor division Link
|= x |= 3 x=x|3 / Division x / y 101/25 = 4.04 Not consistent across different versions of
^= x ^= 3 x=x^3 (Python 3.X version) Python (referred to as floating point
>>= x >>= 3 x = x >> 3 division in number of versions)
<<= x <<= 3 x = x << 3
% Modulus x % y 101/25 = 1 Return the remainder
Comparison Operators // Floor x // y 101//25 = 4 Always Return floored quotient of x and y.
Operator Name Example division (integer floor of division)
== Equal x == y
!= Not equal x != y Also, check slide no.339 for ceiling division
> Greater than x>y
< Less than x<y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y

174 DT Ch ChCh Ch Ch Ch Ch
Built-in Functions Back
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 DT Ch ChCh Ch Ch Ch Ch
Pandas DataFrame
Data representation
•Series: 1D
Pandas General Functions Attributes
Indexing, Iteration
•DataFrame: 2D Data manipulations Function application, GroupBy &
Loading data with *read_csv()* window
melt(frame[, id_vars, value_vars, var_name, …]) Unpivot a DataFrame from wide to long format, optionally leaving Binary Operator functions
•header
identifiers set. Computation/descriptive stats
•names
•sep
pivot(data[, index, columns, values]) Return reshaped DataFrame organized by given index / column values. Reindexing/selection/label
manipulation
•encoding pivot_table(data[, values, index, columns, …]) Create a spreadsheet-style pivot table as a DataFrame.
Missing Data handling
•converters crosstab(index, columns[, values, rownames, …]) Compute a simple cross tabulation of two (or more) factors. Reshaping, sorting, transposing
Getting information cut(x, bins[, right, labels, retbins, …]) Bin values into discrete intervals. Combining/comparing/joining/m
•index: Series and DataFrame index qcut(x, q[, labels, retbins, precision, …]) Quantile-based discretization function. erging
Time Series related
•columns: DataFrame column merge(left, right[, how, on, left_on, …]) Merge DataFrame or named Series objects with a database-style join. Plotting
•shape: Series and DataFrame dimensions merge_ordered(left, right[, on, left_on, …]) Perform merge with optional filling/interpolation. Serialization/IO/Conversion
•info(): DataFrame informations merge_asof(left, right[, on, left_on, …]) Perform an asof merge. Pandas Series
•values: Index and Series values Attributes
concat(objs[, axis, join, ignore_index, …]) Concatenate pandas objects along a particular axis with optional set Conversion
•unique(): Series unique values
•nunique(): Series number of unique values
logic along the other axes. Indexing, Iteration

Selecting data get_dummies(data[, prefix, prefix_sep, …]) Convert categorical variable into dummy/indicator variables. Function application, GroupBy &
window
•head() and tail() Top-level missing data Binary Operator functions
•Column accessing Computation/descriptive stats
•Row accessing isna(obj) Detect missing values for an array-like object. Reindexing/selection/label
•Fancy indexing isnull(obj) Detect missing values for an array-like object. manipulation
notna(obj) Detect non-missing values for an array-like object. Missing Data handling
•Logical masking
notnull(obj) Detect non-missing values for an array-like object. Reshaping, sorting
Indexing and merging data Combining/comparing/joining/m
•*set_index()* and *reset_index()* Top-level conversions erging
•*append()* and *concat()* Time Series related
Computing over data to_numeric(arg[, errors, downcast]) Convert argument to a numeric type. Accessors
•sum() Datetime properties
Top-level dealing with datetimelike Datetime methods
•[Link](), [Link]() and [Link]()
Period properties
•*apply()* and lambda to_datetime(arg[, errors, dayfirst, …]) Convert argument to datetime. Timedelta properties
•isin() to_timedelta(arg[, unit, errors]) Convert argument to timedelta. Timedelta methods
•div() date_range([start, end, periods, freq, tz, …]) Return a fixed frequency DatetimeIndex. String handling
Organizing data bdate_range([start, end, periods, freq, tz, …]) Return a fixed frequency DatetimeIndex, with business day as the default Categorical Accessor
•transpose() or T Plotting
frequency.
Index Objects
•*sort_values()* period_range([start, end, periods, freq, name]) Return a fixed frequency PeriodIndex. Numpy Arrays
•*groupby()* timedelta_range([start, end, periods, freq, …]) Return a fixed frequency TimedeltaIndex, with day as the default frequency. Attributes
•*pivot_table()* and *crosstab()* infer_freq(index[, warn]) Infer the most likely frequency given the input index. Array Creation Routines
Displaying data Shape Manipulation - shape,
•*plot()* resize, transpose
Calculation
Check Link – PyParis 2017 Item Selection and Manipulation
Time Series/Date Functionality Array Conversions
Group by: split-apply-combine
Windowing 176 DT Ch ChCh Ch Ch Ch Ch
Pandas DataFrame
Data representation
•Series: 1D
Pandas General Functions Attributes
Indexing, Iteration
•DataFrame: 2D Top-level dealing with intervals Function application, GroupBy &
Loading data with *read_csv()* window
•header interval_range([start, end, periods, freq, …]) Return a fixed frequency IntervalIndex. Binary Operator functions
Computation/descriptive stats
•names
Top-level evaluation Reindexing/selection/label
•sep manipulation
•encoding eval(expr[, parser, engine, truediv, …]) Evaluate a Python expression as a string using various backends. Missing Data handling
•converters Reshaping, sorting, transposing
Getting information Combining/comparing/joining/m
•index: Series and DataFrame index erging
Time Series related
•columns: DataFrame column
Plotting
•shape: Series and DataFrame dimensions Serialization/IO/Conversion
•info(): DataFrame informations Pandas Series
•values: Index and Series values Attributes
•unique(): Series unique values Conversion
Indexing, Iteration
•nunique(): Series number of unique values
Function application, GroupBy &
Selecting data window
•head() and tail() Binary Operator functions
•Column accessing Computation/descriptive stats
•Row accessing Reindexing/selection/label
•Fancy indexing manipulation
Missing Data handling
•Logical masking
Reshaping, sorting
Indexing and merging data Combining/comparing/joining/m
•*set_index()* and *reset_index()* erging
•*append()* and *concat()* Time Series related
Computing over data Accessors
•sum() Datetime properties
Datetime methods
•[Link](), [Link]() and [Link]()
Period properties
•*apply()* and lambda Timedelta properties
•isin() Timedelta methods
•div() String handling
Organizing data Categorical Accessor
•transpose() or T Plotting
Index Objects
•*sort_values()*
Numpy Arrays
•*groupby()* Attributes
•*pivot_table()* and *crosstab()* Array Creation Routines
Displaying data Shape Manipulation - shape,
•*plot()* resize, transpose
Calculation
Check Link – PyParis 2017 Item Selection and Manipulation
Time Series/Date Functionality Array Conversions
Group by: split-apply-combine
Windowing 177 DT Ch ChCh Ch Ch Ch Ch
Pandas DataFrame
Data representation
•Series: 1D
Pandas DataFrame Attributes
Indexing, Iteration
•DataFrame: 2D Attributes Function application, GroupBy &
Loading data with *read_csv()* window
•header [Link] The index (row labels) of the DataFrame. Binary Operator functions
[Link] The column labels of the DataFrame. Computation/descriptive stats
•names
Reindexing/selection/label
•sep [Link] Return the dtypes in the DataFrame.
manipulation
•encoding [Link]([verbose, buf, max_cols, …]) Print a concise summary of a DataFrame. Missing Data handling
•converters Return a subset of the DataFrame’s columns based on the column Reshaping, sorting, transposing
Getting information df.select_dtypes([include, exclude]) dtypes. Combining/comparing/joining/m
•index: Series and DataFrame index erging
[Link] Return a Numpy representation of the DataFrame.
Time Series related
•columns: DataFrame column [Link] Return a list representing the axes of the DataFrame. Plotting
•shape: Series and DataFrame dimensions [Link] Return an int representing the number of axes / array dimensions. Serialization/IO/Conversion
•info(): DataFrame informations [Link] Return an int representing the number of elements in this object. Pandas Series
•values: Index and Series values [Link] Return a tuple representing the dimensionality of the DataFrame. Attributes
•unique(): Series unique values Conversion
df.memory_usage([index, deep]) Return the memory usage of each column in bytes. Indexing, Iteration
•nunique(): Series number of unique values
Function application, GroupBy &
Selecting data Indexing, iteration window
•head() and tail() Binary Operator functions
•Column accessing [Link]([n]) Return the first n rows. Computation/descriptive stats
•Row accessing [Link] Access a single value for a row/column label pair. Reindexing/selection/label
•Fancy indexing [Link] Access a single value for a row/column pair by integer position. manipulation
[Link] Access a group of rows and columns by label(s) or a boolean array. Missing Data handling
•Logical masking
[Link] Purely integer-location based indexing for selection by position. Reshaping, sorting
Indexing and merging data Combining/comparing/joining/m
•*set_index()* and *reset_index()* [Link](loc, column, value[, …]) Insert column into DataFrame at specified location. erging
•*append()* and *concat()* df.__iter__() Iterate over info axis. Time Series related
Computing over data [Link]() Iterate over (column name, Series) pairs. Accessors
•sum() [Link]() Iterate over (column name, Series) pairs. Datetime properties
[Link]() Get the ‘info axis’ (see Indexing for more). Datetime methods
•[Link](), [Link]() and [Link]()
Period properties
•*apply()* and lambda [Link]() Iterate over DataFrame rows as (index, Series) pairs.
Timedelta properties
•isin() [Link]([index, name]) Iterate over DataFrame rows as namedtuples. Timedelta methods
•div() [Link](row_labels, col_labels) (DEPRECATED) Label-based “fancy indexing” function for DataFrame. String handling
Organizing data [Link](item) Return item and drop from frame. Categorical Accessor
•transpose() or T [Link]([n]) Return the last n rows. Plotting
•*sort_values()* [Link](key[, axis, level, drop_level]) Return cross-section from the Series/DataFrame. Index Objects
[Link](key[, default]) Get item from object for given key (ex: DataFrame column). Numpy Arrays
•*groupby()* Attributes
•*pivot_table()* and *crosstab()* [Link](values) Whether each element in the DataFrame is contained in values. Array Creation Routines
Displaying data [Link](cond[, other, inplace, …]) Replace values where the condition is False. Shape Manipulation - shape,
•*plot()* [Link](cond[, other, inplace, axis, …]) Replace values where the condition is True. resize, transpose

Check Link – PyParis 2017


[Link](expr[, inplace]) Query the columns of a DataFrame with a boolean expression. Calculation
Item Selection and Manipulation
Time Series/Date Functionality Array Conversions
Group by: split-apply-combine
Windowing 178 DT Ch ChCh Ch Ch Ch Ch
Data representation Pandas DataFrame Pandas DataFrame
Attributes
•Series: 1D Indexing, Iteration
•DataFrame: 2D Function application, GroupBy & window
Function application, GroupBy &
Loading data with *read_csv()* window
[Link](func[, axis, raw, …]) Apply a function along an axis of the DataFrame. Binary Operator functions
•header
[Link](func[, na_action]) Apply a function to a Dataframe elementwise. Computation/descriptive stats
•names
[Link](func, *args, **kwargs) Apply func(self, *args, **kwargs). Reindexing/selection/label
•sep
[Link]([func, axis]) Aggregate using one or more operations over the specified axis. manipulation
•encoding Missing Data handling
[Link]([func, axis]) Aggregate using one or more operations over the specified axis.
•converters Reshaping, sorting, transposing
[Link](func[, axis]) Call func on self producing a DataFrame with transformed values.
Getting information Combining/comparing/joining/m
[Link]([by, axis, level, …]) Group DataFrame using a mapper or by a Series of columns. erging
•index: Series and DataFrame index
[Link](window[, min_periods, …]) Provide rolling window calculations. Time Series related
•columns: DataFrame column
[Link]([min_periods, center, …]) Provide expanding transformations. Plotting
•shape: Series and DataFrame dimensions Serialization/IO/Conversion
[Link]([com, span, halflife, alpha, …]) Provide exponential weighted (EW) functions.
•info(): DataFrame informations Pandas Series
•values: Index and Series values Binary operator functions Attributes
•unique(): Series unique values Conversion
•nunique(): Series number of unique values [Link](other[, axis, level, fill_value]) Get Addition of dataframe and other, element-wise (binary operator add). Indexing, Iteration
[Link](other[, axis, level, fill_value]) Get Subtraction of dataframe and other, element-wise (binary operator sub). Function application, GroupBy &
Selecting data window
•head() and tail() [Link](other[, axis, level, fill_value]) Get Multiplication of dataframe and other, element-wise (binary operator mul).
Binary Operator functions
•Column accessing [Link](other[, axis, level, fill_value]) Get Floating division of dataframe and other, element-wise (binary operator truediv). Computation/descriptive stats
•Row accessing [Link](other[, axis, level, …]) Get Floating division of dataframe and other, element-wise (binary operator truediv). Reindexing/selection/label
•Fancy indexing [Link](other[, axis, level, …]) Get Integer division of dataframe and other, element-wise (binary operator floordiv). manipulation
•Logical masking [Link](other[, axis, level, fill_value]) Get Modulo of dataframe and other, element-wise (binary operator mod). Missing Data handling
[Link](other[, axis, level, fill_value]) Get Exponential power of dataframe and other, element-wise (binary operator pow). Reshaping, sorting
Indexing and merging data Combining/comparing/joining/m
•*set_index()* and *reset_index()* [Link](other) Compute the matrix multiplication between the DataFrame and other. erging
•*append()* and *concat()* [Link](other[, axis, level, fill_value]) Get Addition of dataframe and other, element-wise (binary operator radd). Time Series related
Computing over data [Link](other[, axis, level, fill_value]) Get Subtraction of dataframe and other, element-wise (binary operator rsub). Accessors
•sum() [Link](other[, axis, level, fill_value]) Get Multiplication of dataframe and other, element-wise (binary operator rmul). Datetime properties
[Link](other[, axis, level, fill_value]) Get Floating division of dataframe and other, element-wise (binary operator rtruediv). Datetime methods
•[Link](), [Link]() and [Link]()
[Link](other[, axis, level, …]) Get Floating division of dataframe and other, element-wise (binary operator rtruediv). Period properties
•*apply()* and lambda Timedelta properties
•isin() [Link](other[, axis, level, …]) Get Integer division of dataframe and other, element-wise (binary operator rfloordiv). Timedelta methods
•div() [Link](other[, axis, level, fill_value]) Get Modulo of dataframe and other, element-wise (binary operator rmod). String handling
Organizing data [Link](other[, axis, level, fill_value]) Get Exponential power of dataframe and other, element-wise (binary operator rpow). Categorical Accessor
•transpose() or T [Link](other[, axis, level]) Get Less than of dataframe and other, element-wise (binary operator lt). Plotting
•*sort_values()* [Link](other[, axis, level]) Get Greater than of dataframe and other, element-wise (binary operator gt). Index Objects
[Link](other[, axis, level]) Get Less than or equal to of dataframe and other, element-wise (binary operator le). Numpy Arrays
•*groupby()* Attributes
•*pivot_table()* and *crosstab()* [Link](other[, axis, level]) Get Greater than or equal to of dataframe and other, element-wise (binary operator ge). Array Creation Routines
Displaying data [Link](other[, axis, level]) Get Not equal to of dataframe and other, element-wise (binary operator ne). Shape Manipulation - shape,
•*plot()* [Link](other[, axis, level]) Get Equal to of dataframe and other, element-wise (binary operator eq). resize, transpose
Calculation
Check Link – PyParis 2017 Item Selection and Manipulation
Time Series/Date Functionality Array Conversions
Group by: split-apply-combine
Windowing 179 DT Ch ChCh Ch Ch Ch Ch
Pandas DataFrame
Data representation Pandas DataFrame Attributes
•Series: 1D
Computations / descriptive stats Indexing, Iteration
•DataFrame: 2D Function application, GroupBy &
Loading data with *read_csv()* [Link]() Return a Series/DataFrame with absolute numeric value of each element. window
•header Binary Operator functions
[Link]([axis, bool_only, skipna, level]) Return whether all elements are True, potentially over an axis. Computation/descriptive stats
•names [Link]([axis, bool_only, skipna, level]) Return whether any element is True, potentially over an axis. Reindexing/selection/label
•sep [Link]([lower, upper, axis, inplace]) Trim values at input threshold(s). manipulation
•encoding Missing Data handling
[Link]([method, min_periods]) Compute pairwise correlation of columns, excluding NA/null values.
•converters Reshaping, sorting, transposing
[Link](other[, axis, drop, method]) Compute pairwise correlation.
Getting information Combining/comparing/joining/m
•index: Series and DataFrame index
[Link]([axis, level, numeric_only]) Count non-NA cells for each column or row. erging
•columns: DataFrame column [Link]([min_periods, ddof]) Compute pairwise covariance of columns, excluding NA/null values. Time Series related

•shape: Series and DataFrame dimensions [Link]([axis, skipna]) Return cumulative maximum over a DataFrame or Series axis. Plotting
Serialization/IO/Conversion
•info(): DataFrame informations [Link]([axis, skipna]) Return cumulative minimum over a DataFrame or Series axis.
Pandas Series
•values: Index and Series values [Link]([axis, skipna]) Return cumulative product over a DataFrame or Series axis. Attributes
•unique(): Series unique values [Link]([axis, skipna]) Return cumulative sum over a DataFrame or Series axis. Conversion
•nunique(): Series number of unique values [Link]([percentiles, include, …]) Generate descriptive statistics. Indexing, Iteration
Function application, GroupBy &
Selecting data [Link]([periods, axis]) First discrete difference of element. window
•head() and tail() [Link](expr[, inplace]) Evaluate a string describing operations on DataFrame columns. Binary Operator functions
•Column accessing [Link]([axis, skipna, level, …]) Return unbiased kurtosis over requested axis. Computation/descriptive stats
•Row accessing [Link]([axis, skipna, level, …]) Return unbiased kurtosis over requested axis. Reindexing/selection/label
•Fancy indexing manipulation
[Link]([axis, skipna, level]) Return the mean absolute deviation of the values over the requested axis. Missing Data handling
•Logical masking [Link]([axis, skipna, level, …]) Return the maximum of the values over the requested axis. Reshaping, sorting
Indexing and merging data [Link]([axis, skipna, level, …]) Return the mean of the values over the requested axis. Combining/comparing/joining/m
•*set_index()* and *reset_index()* [Link]([axis, skipna, level, …]) Return the median of the values over the requested axis. erging
•*append()* and *concat()* [Link]([axis, skipna, level, …]) Return the minimum of the values over the requested axis. Time Series related
Computing over data Accessors
[Link]([axis, numeric_only, dropna]) Get the mode(s) of each element along the selected axis. Datetime properties
•sum()
df.pct_change([periods, fill_method, …]) Percentage change between the current and a prior element. Datetime methods
•[Link](), [Link]() and [Link]()
•*apply()* and lambda
[Link]([axis, skipna, level, …]) Return the product of the values over the requested axis. Period properties
Timedelta properties
•isin() [Link]([axis, skipna, level, …]) Return the product of the values over the requested axis.
Timedelta methods
•div() [Link]([q, axis, numeric_only, …]) Return values at the given quantile over requested axis. String handling
Organizing data [Link]([axis, method, numeric_only, …]) Compute numerical data ranks (1 through n) along axis. Categorical Accessor
•transpose() or T [Link]([decimals]) Round a DataFrame to a variable number of decimal places. Plotting
•*sort_values()* [Link]([axis, skipna, level, ddof, …]) Return unbiased standard error of the mean over requested axis. Index Objects
Numpy Arrays
•*groupby()* [Link]([axis, skipna, level, …]) Return unbiased skew over requested axis. Attributes
•*pivot_table()* and *crosstab()* [Link]([axis, skipna, level, …]) Return the sum of the values over the requested axis. Array Creation Routines
Displaying data [Link]([axis, skipna, level, ddof, …]) Return sample standard deviation over requested axis. Shape Manipulation - shape,
•*plot()* [Link]([axis, skipna, level, ddof, …]) Return unbiased variance over requested axis. resize, transpose
Calculation
Check Link – PyParis 2017 [Link]([axis, dropna]) Count number of distinct elements in specified axis. Item Selection and Manipulation
Time Series/Date Functionality df.value_counts([subset, normalize, …]) Return a Series containing counts of unique rows in the DataFrame. Array Conversions
Group by: split-apply-combine
Windowing 180 DT Ch ChCh Ch Ch Ch Ch
Pandas DataFrame
Data representation Pandas DataFrame Attributes
•Series: 1D Indexing, Iteration
Reindexing / selection / label manipulation
•DataFrame: 2D Function application, GroupBy &
Loading data with *read_csv()* window
df.add_prefix(prefix) Prefix labels with string prefix. Binary Operator functions
•header
df.add_suffix(suffix) Suffix labels with string suffix. Computation/descriptive stats
•names
•sep [Link](other[, join, axis, level, …]) Align two objects on their axes with the specified join method. Reindexing/selection/label
manipulation
•encoding df.at_time(time[, asof, axis]) Select values at particular time of day (e.g., 9:30AM).
Missing Data handling
•converters df.between_time(start_time, end_time) Select values between particular times of the day (e.g., 9:00-9:30 AM). Reshaping, sorting, transposing
Getting information [Link]([labels, axis, index, …]) Drop specified labels from rows or columns. Combining/comparing/joining/m
•index: Series and DataFrame index df.drop_duplicates([subset, keep, …]) Return DataFrame with duplicate rows removed. erging
Time Series related
•columns: DataFrame column [Link]([subset, keep]) Return boolean Series denoting duplicate rows. Plotting
•shape: Series and DataFrame dimensions [Link](other) Test whether two objects contain the same elements. Serialization/IO/Conversion
•info(): DataFrame informations [Link]([items, like, regex, axis]) Subset the dataframe rows or columns according to the specified Pandas Series
•values: Index and Series values Attributes
index labels. Conversion
•unique(): Series unique values
[Link](offset) Select initial periods of time series data based on a date offset. Indexing, Iteration
•nunique(): Series number of unique values
Selecting data [Link]([n]) Return the first n rows. Function application, GroupBy &
window
•head() and tail() [Link]([axis, skipna]) Return index of first occurrence of maximum over requested axis. Binary Operator functions
•Column accessing [Link]([axis, skipna]) Return index of first occurrence of minimum over requested axis. Computation/descriptive stats
•Row accessing [Link](offset) Select final periods of time series data based on a date offset. Reindexing/selection/label
•Fancy indexing [Link]([labels, index, columns, …]) Conform Series/DataFrame to new index with optional filling logic. manipulation
Missing Data handling
•Logical masking df.reindex_like(other[, method, …]) Return an object with matching indices as other object. Reshaping, sorting
Indexing and merging data [Link]([mapper, index, columns, …]) Alter axes labels. Combining/comparing/joining/m
•*set_index()* and *reset_index()* df.rename_axis([mapper, index, …]) Set the name of the axis for the index or columns. erging
•*append()* and *concat()* Time Series related
df.reset_index([level, drop, …]) Reset the index, or a level of it. Accessors
Computing over data
[Link]([n, frac, replace, …]) Return a random sample of items from an axis of object. Datetime properties
•sum()
•[Link](), [Link]() and [Link]() df.set_axis(labels[, axis, inplace]) Assign desired index to given axis. Datetime methods
df.set_index(keys[, drop, append, …]) Set the DataFrame index using existing columns. Period properties
•*apply()* and lambda Timedelta properties
•isin() [Link]([n]) Return the last n rows. Timedelta methods
•div() [Link](indices[, axis, is_copy]) Return the elements in the given positional indices along an axis. String handling
Organizing data [Link]([before, after, axis, copy]) Truncate a Series or DataFrame before and after some index value. Categorical Accessor
•transpose() or T Plotting
Index Objects
•*sort_values()*
Numpy Arrays
•*groupby()* Attributes
•*pivot_table()* and *crosstab()* Array Creation Routines
Displaying data Shape Manipulation - shape,
•*plot()* resize, transpose
Calculation
Check Link – PyParis 2017 Item Selection and Manipulation
Time Series/Date Functionality Array Conversions
Group by: split-apply-combine
Windowing 181 DT Ch ChCh Ch Ch Ch Ch
Data representation Pandas DataFrame Pandas DataFrame
Attributes
•Series: 1D Missing data handling Indexing, Iteration
•DataFrame: 2D Function application, GroupBy &
Loading data with *read_csv()* [Link]([axis, inplace, limit, …]) Synonym for [Link]() with method='bfill'. window
•header Binary Operator functions
[Link]([axis, inplace, limit, downcast]) Synonym for [Link]() with method='bfill'. Computation/descriptive stats
•names
[Link]([axis, how, thresh, …]) Remove missing values. Reindexing/selection/label
•sep
•encoding [Link]([axis, inplace, limit, downcast]) Synonym for [Link]() with method='ffill'. manipulation
Missing Data handling
•converters [Link]([value, method, axis, …]) Fill NA/NaN values using the specified method. Reshaping, sorting, transposing
Getting information [Link]([method, axis, limit, …]) Fill NaN values using an interpolation method. Combining/comparing/joining/m
•index: Series and DataFrame index [Link]() Detect missing values. erging
Time Series related
•columns: DataFrame column [Link]() Detect missing values. Plotting
•shape: Series and DataFrame dimensions [Link]() Detect existing (non-missing) values. Serialization/IO/Conversion
•info(): DataFrame informations [Link]() Detect existing (non-missing) values. Pandas Series
•values: Index and Series values [Link]([axis, inplace, limit, downcast]) Synonym for [Link]() with method='ffill'. Attributes
•unique(): Series unique values Conversion
[Link]([to_replace, value, …]) Replace values given in to_replace with value. Indexing, Iteration
•nunique(): Series number of unique values
Function application, GroupBy &
Selecting data Reshaping, sorting, transposing window
•head() and tail() Binary Operator functions
•Column accessing [Link](level[, axis]) Return Series/DataFrame with requested index / column level(s) removed. Computation/descriptive stats
•Row accessing [Link]([index, columns, values]) Return reshaped DataFrame organized by given index / column values. Reindexing/selection/label
•Fancy indexing manipulation
df.pivot_table([values, index, …]) Create a spreadsheet-style pivot table as a DataFrame. Missing Data handling
•Logical masking df.reorder_levels(order[, axis]) Rearrange index levels using input order. Reshaping, sorting
Indexing and merging data
df.sort_values(by[, axis, ascending, …]) Sort by the values along either axis. Combining/comparing/joining/m
•*set_index()* and *reset_index()* erging
•*append()* and *concat()* df.sort_index([axis, level, …]) Sort object by labels (along an axis). Time Series related
Computing over data [Link](n, columns[, keep]) Return the first n rows ordered by columns in descending order. Accessors
•sum() [Link](n, columns[, keep]) Return the first n rows ordered by columns in ascending order. Datetime properties
Datetime methods
•[Link](), [Link]() and [Link]() [Link]([i, j, axis]) Swap levels i and j in a MultiIndex.
Period properties
•*apply()* and lambda [Link]([level, dropna]) Stack the prescribed level(s) from columns to index. Timedelta properties
•isin() [Link]([level, fill_value]) Pivot a level of the (necessarily hierarchical) index labels. Timedelta methods
•div() [Link](axis1, axis2[, copy]) Interchange axes and swap values axes appropriately. String handling
Organizing data Unpivot a DataFrame from wide to long format, optionally leaving identifiers Categorical Accessor
•transpose() or T Plotting
[Link]([id_vars, value_vars, …]) set. Index Objects
•*sort_values()*
[Link](column[, ignore_index]) Transform each element of a list-like to a row, replicating index values. Numpy Arrays
•*groupby()* Attributes
•*pivot_table()* and *crosstab()* [Link]([axis]) Squeeze 1 dimensional axis objects into scalars.
Array Creation Routines
Displaying data df.to_xarray() Return an xarray object from the pandas object. Shape Manipulation - shape,
•*plot()* df.T Transpose index and columns. resize, transpose
[Link](*args[, copy]) Transpose index and columns. Calculation
Check Link – PyParis 2017 Item Selection and Manipulation
Time Series/Date Functionality Array Conversions
Group by: split-apply-combine
Windowing 182 DT Ch ChCh Ch Ch Ch Ch
Data representation Pandas DataFrame Pandas DataFrame
Attributes
•Series: 1D Combining / comparing / joining / merging Indexing, Iteration
•DataFrame: 2D Function application, GroupBy &
Loading data with *read_csv()* [Link](other[, ignore_index, …]) Append rows of other to the end of caller, returning a new object. window
•header [Link](**kwargs) Assign new columns to a DataFrame. Binary Operator functions
[Link](other[, align_axis, …]) Compare to another DataFrame and show the differences. Computation/descriptive stats
•names
Reindexing/selection/label
•sep [Link](other[, on, how, lsuffix, …]) Join columns of another DataFrame. manipulation
•encoding [Link](right[, how, on, left_on, …]) Merge DataFrame or named Series objects with a database-style join. Missing Data handling
•converters [Link](other[, join, overwrite, …]) Modify in place using non-NA values from another DataFrame. Reshaping, sorting, transposing
Getting information Combining/comparing/joining/m
Time Series-related erging
•index: Series and DataFrame index
Time Series related
•columns: DataFrame column [Link](freq[, method, how, …]) Convert time series to specified frequency. Plotting
•shape: Series and DataFrame dimensions [Link](where[, subset]) Return the last row(s) without any NaNs before where. Serialization/IO/Conversion
•info(): DataFrame informations [Link]([periods, freq, axis, …]) Shift index by desired number of periods with an optional time freq. Pandas Series
•values: Index and Series values Attributes
df.slice_shift([periods, axis]) (DEPRECATED) Equivalent to shift without copying data. Conversion
•unique(): Series unique values
[Link]([periods, freq, axis]) (DEPRECATED) Shift the time index, using the index’s frequency if available. Indexing, Iteration
•nunique(): Series number of unique values
Selecting data df.first_valid_index() Return index for first non-NA value or None, if no NA value is found. Function application, GroupBy &
window
•head() and tail() df.last_valid_index() Return index for last non-NA value or None, if no NA value is found. Binary Operator functions
•Column accessing [Link](rule[, axis, closed, …]) Resample time-series data. Computation/descriptive stats
•Row accessing df.to_period([freq, axis, copy]) Convert DataFrame from DatetimeIndex to PeriodIndex. Reindexing/selection/label
manipulation
•Fancy indexing df.to_timestamp([freq, how, axis, copy]) Cast to DatetimeIndex of timestamps, at beginning of period.
Missing Data handling
•Logical masking df.tz_convert(tz[, axis, level, copy]) Convert tz-aware axis to target time zone. Reshaping, sorting
Indexing and merging data df.tz_localize(tz[, axis, level, …]) Localize tz-naive index of a Series or DataFrame to target time zone. Combining/comparing/joining/m
•*set_index()* and *reset_index()* erging
•*append()* and *concat()* Plotting Time Series related
Computing over data Accessors
[Link]([x, y, kind, ax, ….]) DataFrame plotting accessor and method Datetime properties
•sum()
[Link]([x, y]) Draw a stacked area plot. Datetime methods
•[Link](), [Link]() and [Link]()
•*apply()* and lambda [Link]([x, y]) Vertical bar plot. Period properties
Timedelta properties
•isin() [Link]([x, y]) Make a horizontal bar plot. Timedelta methods
•div() [Link]([by]) Make a box plot of the DataFrame columns. String handling
Organizing data [Link]([bw_method, ind]) Generate Kernel Density Estimate plot using Gaussian kernels. Categorical Accessor
•transpose() or T [Link](x, y[, C, …]) Generate a hexagonal binning plot. Plotting
Index Objects
•*sort_values()* [Link]([by, bins]) Draw one histogram of the DataFrame’s columns. Numpy Arrays
•*groupby()* [Link]([bw_method, ind]) Generate Kernel Density Estimate plot using Gaussian kernels. Attributes
•*pivot_table()* and *crosstab()* [Link]([x, y]) Plot Series or DataFrame as lines. Array Creation Routines
Displaying data Shape Manipulation - shape,
[Link](**kwargs) Generate a pie plot. resize, transpose
•*plot()*
[Link](x, y[, s, c]) Create a scatter plot with varying marker point size and color. Calculation
Check Link – PyParis 2017 Item Selection and Manipulation
[Link]([column, by, ax, …]) Make a box plot from DataFrame columns.
Time Series/Date Functionality Array Conversions
Group by: split-apply-combine [Link]([column, by, grid, …]) Make a histogram of the DataFrame’s columns.
Windowing 183 DT Ch ChCh Ch Ch Ch Ch
Pandas DataFrame
Data representation
•Series: 1D
Pandas DataFrame Attributes
Indexing, Iteration
•DataFrame: 2D Serialization / IO / conversion Function application, GroupBy &
Loading data with *read_csv()* window
•header Binary Operator functions
DataFrame.from_dict(data[, orient, dtype, …]) Construct DataFrame from dict of array-like or dicts.
Computation/descriptive stats
•names DataFrame.from_records(data[, index, …]) Convert structured or record ndarray to DataFrame. Reindexing/selection/label
•sep
df.to_parquet([path, engine, …]) Write a DataFrame to the binary parquet format. manipulation
•encoding Missing Data handling
•converters df.to_pickle(path[, compression, …]) Pickle (serialize) object to file. Reshaping, sorting, transposing
Getting information df.to_csv([path_or_buf, sep, na_rep, …]) Write object to a comma-separated values (csv) file. Combining/comparing/joining/m
•index: Series and DataFrame index df.to_hdf(path_or_buf, key[, mode, …]) Write the contained data to an HDF5 file using HDFStore. erging
Time Series related
•columns: DataFrame column df.to_sql(name, con[, schema, …]) Write records stored in a DataFrame to a SQL database. Plotting
•shape: Series and DataFrame dimensions df.to_dict([orient, into]) Convert the DataFrame to a dictionary. Serialization/IO/Conversion
•info(): DataFrame informations Pandas Series
df.to_numpy([dtype, copy, na_value]) Convert the DataFrame to a NumPy array.
•values: Index and Series values Attributes
•unique(): Series unique values df.to_period([freq, axis, copy]) Convert DataFrame from DatetimeIndex to PeriodIndex. Conversion
•nunique(): Series number of unique values df.to_timestamp([freq, how, axis, copy]) Cast to DatetimeIndex of timestamps, at beginning of period. Indexing, Iteration
Function application, GroupBy &
Selecting data df.to_xarray() Return an xarray object from the pandas object. window
•head() and tail() df.to_xml([path_or_buffer, index, root_name, …]) Render a DataFrame to an XML document. Binary Operator functions
•Column accessing df.to_excel(excel_writer[, …]) Write object to an Excel sheet. Computation/descriptive stats
•Row accessing Reindexing/selection/label
df.to_json([path_or_buf, orient, …]) Convert the object to a JSON string. manipulation
•Fancy indexing
•Logical masking df.to_html([buf, columns, col_space, …]) Render a DataFrame as an HTML table. Missing Data handling
Reshaping, sorting
Indexing and merging data df.to_feather(path, **kwargs) Write a DataFrame to the binary Feather format.
Combining/comparing/joining/m
•*set_index()* and *reset_index()* df.to_latex([buf, columns, …]) Render object to a LaTeX tabular, longtable, or nested erging
•*append()* and *concat()* table/tabular. Time Series related
Computing over data Accessors
df.to_stata(path[, convert_dates, …]) Export DataFrame object to Stata dta format.
•sum() Datetime properties
df.to_gbq(destination_table[, …]) Write a DataFrame to a Google BigQuery table. Datetime methods
•[Link](), [Link]() and [Link]()
df.to_records([index, column_dtypes, …]) Convert DataFrame to a NumPy record array. Period properties
•*apply()* and lambda Timedelta properties
•isin() df.to_string([buf, columns, …]) Render a DataFrame to a console-friendly tabular output. Timedelta methods
•div() df.to_clipboard([excel, sep]) Copy object to the system clipboard. String handling
Organizing data df.to_markdown([buf, mode, index, …]) Print DataFrame in Markdown-friendly format. Categorical Accessor
•transpose() or T Plotting
[Link] Returns a Styler object.
Index Objects
•*sort_values()*
Numpy Arrays
•*groupby()* Attributes
•*pivot_table()* and *crosstab()* Array Creation Routines
Displaying data Shape Manipulation - shape,
•*plot()* resize, transpose
Calculation
Check Link – PyParis 2017 Item Selection and Manipulation
Time Series/Date Functionality Array Conversions
Group by: split-apply-combine
Windowing 184 DT Ch ChCh Ch Ch Ch Ch
Pandas DataFrame
Data representation Pandas Series Attributes
•Series: 1D Indexing, Iteration
•DataFrame: 2D Attributes Function application, GroupBy &
Loading data with *read_csv()* window
•header Binary Operator functions
[Link] The index (axis labels) of the Series. Computation/descriptive stats
•names
[Link] The ExtensionArray of the data backing this Series or Index. Reindexing/selection/label
•sep
[Link] Return Series as ndarray or ndarray-like depending on the dtype. manipulation
•encoding Missing Data handling
•converters [Link] Return the dtype object of the underlying data. Reshaping, sorting, transposing
Getting information [Link] Return a tuple of the shape of the underlying data. Combining/comparing/joining/m
•index: Series and DataFrame index [Link] Return the number of bytes in the underlying data. erging
Time Series related
•columns: DataFrame column [Link] Number of dimensions of the underlying data, by definition 1. Plotting
•shape: Series and DataFrame dimensions [Link] Return the number of elements in the underlying data. Serialization/IO/Conversion
•info(): DataFrame informations Series.T Return the transpose, which is by definition self. Pandas Series
•values: Index and Series values Series.memory_usage([index, deep]) Return the memory usage of the Series. Attributes
•unique(): Series unique values Conversion
[Link] Return if I have any nans; enables various perf speedups. Indexing, Iteration
•nunique(): Series number of unique values
Selecting data
[Link] Indicator whether DataFrame is empty. Function application, GroupBy &
[Link] Return the dtype object of the underlying data. window
•head() and tail() Binary Operator functions
•Column accessing [Link] Return the name of the Series. Computation/descriptive stats
•Row accessing [Link] Get the properties associated with this pandas object. Reindexing/selection/label
•Fancy indexing Series.set_flags(*[, copy, …]) Return a new object with updated flags. manipulation
Missing Data handling
•Logical masking
Indexing and merging data Conversion Reshaping, sorting
Combining/comparing/joining/m
•*set_index()* and *reset_index()* erging
•*append()* and *concat()* [Link](dtype[, copy, errors]) Cast a pandas object to a specified dtype dtype. Time Series related
Computing over data Series.convert_dtypes([infer_objects, …]) Convert columns to best possible dtypes using dtypes supporting [Link]. Accessors
•sum() Datetime properties
Series.infer_objects() Attempt to infer better dtypes for object columns. Datetime methods
•[Link](), [Link]() and [Link]()
[Link]([deep]) Make a copy of this object’s indices and data. Period properties
•*apply()* and lambda Timedelta properties
•isin()
[Link]() Return the bool of a single element Series or DataFrame.
Timedelta methods
•div() Series.to_numpy([dtype, copy, na_value]) A NumPy ndarray representing the values in this Series or Index. String handling
Organizing data Series.to_period([freq, copy]) Convert Series from DatetimeIndex to PeriodIndex. Categorical Accessor
Plotting
•transpose() or T Series.to_timestamp([freq, how, copy]) Cast to DatetimeIndex of Timestamps, at beginning of period.
Index Objects
•*sort_values()* Series.to_list() Return a list of the values. Numpy Arrays
•*groupby()* Series.__array__([dtype]) Return the values as a NumPy array. Attributes
•*pivot_table()* and *crosstab()* Array Creation Routines
Displaying data Shape Manipulation - shape,
•*plot()* resize, transpose
Calculation
Check Link – PyParis 2017 Item Selection and Manipulation
Time Series/Date Functionality Array Conversions
Group by: split-apply-combine
Windowing 185 DT Ch ChCh Ch Ch Ch Ch
Pandas DataFrame
Data representation Pandas Series Attributes
•Series: 1D Indexing, Iteration
•DataFrame: 2D Indexing, iteration Function application, GroupBy &
Loading data with *read_csv()* window
•header Binary Operator functions
[Link](key[, default]) Get item from object for given key (ex: DataFrame column). Computation/descriptive stats
•names [Link] Access a single value for a row/column label pair. Reindexing/selection/label
•sep
[Link] Access a single value for a row/column pair by integer position. manipulation
•encoding Missing Data handling
[Link] Access a group of rows and columns by label(s) or a boolean array.
•converters Reshaping, sorting, transposing
Getting information [Link] Purely integer-location based indexing for selection by position. Combining/comparing/joining/m
•index: Series and DataFrame index Series.__iter__() Return an iterator of the values. erging
•columns: DataFrame column [Link]() Lazily iterate over (index, value) tuples. Time Series related
Plotting
•shape: Series and DataFrame dimensions [Link]() Lazily iterate over (index, value) tuples. Serialization/IO/Conversion
•info(): DataFrame informations [Link]() Return alias for index. Pandas Series
•values: Index and Series values [Link](item) Return item and drops from series. Attributes
•unique(): Series unique values Conversion
[Link]() Return the first element of the underlying data as a Python scalar. Indexing, Iteration
•nunique(): Series number of unique values [Link](key[, axis, level, drop_level]) Return cross-section from the Series/DataFrame. Function application, GroupBy &
Selecting data window
•head() and tail() Binary Operator functions
•Column accessing Function application, GroupBy & window Computation/descriptive stats
•Row accessing Reindexing/selection/label
•Fancy indexing [Link](func[, convert_dtype, args]) Invoke function on values of Series. manipulation
Missing Data handling
•Logical masking [Link]([func, axis]) Aggregate using one or more operations over the specified axis. Reshaping, sorting
Indexing and merging data [Link]([func, axis]) Aggregate using one or more operations over the specified axis. Combining/comparing/joining/m
•*set_index()* and *reset_index()* [Link](func[, axis]) Call func on self producing a Series with transformed values. erging
•*append()* and *concat()* Time Series related
Computing over data
[Link](arg[, na_action]) Map values of Series according to input correspondence. Accessors
•sum() [Link]([by, axis, level, as_index, …]) Group Series using a mapper or by a Series of columns. Datetime properties
Datetime methods
•[Link](), [Link]() and [Link]() [Link](window[, min_periods, …]) Provide rolling window calculations.
Period properties
•*apply()* and lambda [Link]([min_periods, center, …]) Provide expanding transformations. Timedelta properties
•isin() [Link]([com, span, halflife, alpha, …]) Provide exponential weighted (EW) functions. Timedelta methods
•div() [Link](func, *args, **kwargs) Apply func(self, *args, **kwargs). String handling
Organizing data Categorical Accessor
•transpose() or T Plotting
Index Objects
•*sort_values()*
Numpy Arrays
•*groupby()* Attributes
•*pivot_table()* and *crosstab()* Array Creation Routines
Displaying data Shape Manipulation - shape,
•*plot()* resize, transpose
Calculation
Check Link – PyParis 2017 Item Selection and Manipulation
Time Series/Date Functionality Array Conversions
Group by: split-apply-combine
Windowing 186 DT Ch ChCh Ch Ch Ch Ch
Pandas DataFrame
Data representation Pandas Series Attributes
•Series: 1D Indexing, Iteration
•DataFrame: 2D Binary operator functions Function application, GroupBy &
Loading data with *read_csv()* window
Binary Operator functions
•header [Link] Return Addition of series and other, element-wise (binary operator add).
Computation/descriptive stats
•names [Link] Return Subtraction of series and other, element-wise (binary operator sub). Reindexing/selection/label
•sep [Link] Return Multiplication of series and other, element-wise (binary operatormul). manipulation
•encoding Missing Data handling
[Link] Return Floating division of series and other, element-wise (binary operator truediv).
•converters Reshaping, sorting, transposing
Getting information
[Link] Return Floating division of series and other, element-wise (binary operator truediv). Combining/comparing/joining/m
•index: Series and DataFrame index [Link] Return Integer division of series and other, element-wise (binary operator floordiv). erging
•columns: DataFrame column [Link] Return Modulo of series and other, element-wise (binary operator mod). Time Series related
Plotting
•shape: Series and DataFrame dimensions [Link] Return Exponential power of series and other, element-wise (binary operator pow). Serialization/IO/Conversion
•info(): DataFrame informations Pandas Series
•values: Index and Series values Attributes
•unique(): Series unique values Conversion
Indexing, Iteration
•nunique(): Series number of unique values
Function application, GroupBy &
Selecting data window
•head() and tail() Binary Operator functions
•Column accessing Computation/descriptive stats
•Row accessing Reindexing/selection/label
•Fancy indexing manipulation
Missing Data handling
•Logical masking
Reshaping, sorting
Indexing and merging data Combining/comparing/joining/m
•*set_index()* and *reset_index()* erging
•*append()* and *concat()* Time Series related
Computing over data Accessors
•sum() Datetime properties
Datetime methods
•[Link](), [Link]() and [Link]()
Period properties
•*apply()* and lambda Timedelta properties
•isin() Timedelta methods
•div() String handling
Organizing data Categorical Accessor
•transpose() or T Plotting
Index Objects
•*sort_values()*
Numpy Arrays
•*groupby()* Attributes
•*pivot_table()* and *crosstab()* Array Creation Routines
Displaying data Shape Manipulation - shape,
•*plot()* resize, transpose
Calculation
Check Link – PyParis 2017 Item Selection and Manipulation
Time Series/Date Functionality Array Conversions
Group by: split-apply-combine
Windowing 187 DT Ch ChCh Ch Ch Ch Ch
Pandas DataFrame
Data representation Pandas Series Attributes
•Series: 1D Indexing, Iteration
•DataFrame: 2D Computations / descriptive stats Function application, GroupBy &
Loading data with *read_csv()* window
•header Binary Operator functions
[Link]() Return a Series/DataFrame with absolute numeric value of each element.
Computation/descriptive stats
•names [Link] Return whether all elements are True, potentially over an axis. Reindexing/selection/label
•sep
[Link] Return whether any element is True, potentially over an axis. manipulation
•encoding Missing Data handling
•converters [Link]([lag]) Compute the lag-N autocorrelation. Reshaping, sorting, transposing
Getting information [Link](left, right[, inclusive]) Return boolean Series equivalent to left <= series <= right. Combining/comparing/joining/m
•index: Series and DataFrame index [Link]([lower, upper, axis, inplace]) Trim values at input threshold(s). erging
Time Series related
•columns: DataFrame column [Link](other[, method, min_periods]) Compute correlation with other Series, excluding missing values. Plotting
•shape: Series and DataFrame dimensions [Link]([level]) Return number of non-NA/null observations in the Series. Serialization/IO/Conversion
•info(): DataFrame informations Pandas Series
[Link](other[, min_periods, ddof]) Compute covariance with Series, excluding missing values.
•values: Index and Series values Attributes
•unique(): Series unique values [Link]([axis, skipna]) Return cumulative maximum over a DataFrame or Series axis. Conversion
•nunique(): Series number of unique values [Link]([axis, skipna]) Return cumulative minimum over a DataFrame or Series axis. Indexing, Iteration
Function application, GroupBy &
Selecting data [Link]([axis, skipna]) Return cumulative product over a DataFrame or Series axis. window
•head() and tail() [Link]([axis, skipna]) Return cumulative sum over a DataFrame or Series axis. Binary Operator functions
•Column accessing [Link]([percentiles, include, …]) Generate descriptive statistics. Computation/descriptive stats
•Row accessing Reindexing/selection/label
[Link]([periods]) First discrete difference of element. manipulation
•Fancy indexing
•Logical masking [Link]([sort, na_sentinel]) Encode the object as an enumerated type or categorical variable. Missing Data handling
Reshaping, sorting
Indexing and merging data [Link] Return unbiased kurtosis over requested axis.
Combining/comparing/joining/m
•*set_index()* and *reset_index()* [Link]([axis, skipna, level]) Return the mean absolute deviation of the values over the requested axis. erging
•*append()* and *concat()* [Link] Return the maximum of the values over the requested axis. Time Series related
Computing over data Accessors
[Link] Return the mean of the values over the requested axis. Datetime properties
•sum()
[Link]([axis, skipna, level, …]) Return the median of the values over the requested axis. Datetime methods
•[Link](), [Link]() and [Link]()
•*apply()* and lambda [Link] Return the minimum of the values over the requested axis. Period properties
Timedelta properties
•isin() [Link]([dropna]) Return the mode(s) of the Series. Timedelta methods
•div() [Link]([n, keep]) Return the largest n elements. String handling
Organizing data [Link]([n, keep]) Return the smallest n elements. Categorical Accessor
•transpose() or T Plotting
Series.pct_change Percentage change between the current and a prior element. Index Objects
•*sort_values()* [Link]([axis, skipna, level, …]) Return the product of the values over the requested axis. Numpy Arrays
•*groupby()* Attributes
•*pivot_table()* and *crosstab()*
[Link]([q, interpolation]) Return value at the given quantile.
Array Creation Routines
Displaying data [Link] Compute numerical data ranks (1 through n) along axis. Shape Manipulation - shape,
•*plot()* [Link]([axis, skipna, level, ddof, …]) Return unbiased standard error of the mean over requested axis. resize, transpose
Calculation
Check Link – PyParis 2017 Item Selection and Manipulation
Time Series/Date Functionality Array Conversions
Group by: split-apply-combine
Windowing 188 DT Ch ChCh Ch Ch Ch Ch
Pandas DataFrame
Data representation Pandas Series Attributes
•Series: 1D Indexing, Iteration
•DataFrame: 2D Reindexing / selection / label manipulation Function application, GroupBy &
Loading data with *read_csv()* window
•header Binary Operator functions
[Link] Align two objects on their axes with the specified join method.
Computation/descriptive stats
•names [Link] Return Series with specified index labels removed. Reindexing/selection/label
•sep
[Link](level[, axis]) Return Series/DataFrame with requested index / column level(s) removed. manipulation
•encoding Missing Data handling
•converters Series.drop_duplicates Return Series with duplicate values removed. Reshaping, sorting, transposing
Getting information [Link]([keep]) Indicate duplicate Series values. Combining/comparing/joining/m
•index: Series and DataFrame index [Link](other) Test whether two objects contain the same elements. erging
Time Series related
•columns: DataFrame column [Link](offset) Select initial periods of time series data based on a date offset. Plotting
•shape: Series and DataFrame dimensions [Link]([n]) Return the first n rows. Serialization/IO/Conversion
•info(): DataFrame informations Pandas Series
[Link]([axis, skipna]) Return the row label of the maximum value.
•values: Index and Series values Attributes
•unique(): Series unique values [Link]([axis, skipna]) Return the row label of the minimum value. Conversion
•nunique(): Series number of unique values [Link](values) Whether elements in Series are contained in values. Indexing, Iteration
Function application, GroupBy &
Selecting data [Link](offset) Select final periods of time series data based on a date offset. window
•head() and tail() [Link]([index]) Conform Series to new index with optional filling logic. Binary Operator functions
•Column accessing Series.reindex_like Return an object with matching indices as other object. Computation/descriptive stats
•Row accessing Reindexing/selection/label
[Link] Alter Series index labels or name. manipulation
•Fancy indexing
•Logical masking Series.rename_axis Set the name of the axis for the index or columns. Missing Data handling
Reshaping, sorting
Indexing and merging data Series.reset_index Generate a new DataFrame or Series with the index reset.
Combining/comparing/joining/m
•*set_index()* and *reset_index()* [Link] Return a random sample of items from an axis of object. erging
•*append()* and *concat()* Series.set_axis(labels[, axis, inplace]) Assign desired index to given axis. Time Series related
Computing over data Accessors
[Link](indices[, axis, is_copy]) Return the elements in the given positional indices along an axis. Datetime properties
•sum()
[Link]([n]) Return the last n rows. Datetime methods
•[Link](), [Link]() and [Link]()
•*apply()* and lambda [Link] Truncate a Series or DataFrame before and after some index value. Period properties
Timedelta properties
•isin() [Link] Replace values where the condition is False. Timedelta methods
•div() [Link] Replace values where the condition is True. String handling
Organizing data Series.add_prefix(prefix) Prefix labels with string prefix. Categorical Accessor
•transpose() or T Plotting
Series.add_suffix(suffix) Suffix labels with string suffix. Index Objects
•*sort_values()* [Link]([items, like, regex, axis]) Subset the dataframe rows or columns according to the specified index Numpy Arrays
•*groupby()*
labels. Attributes
•*pivot_table()* and *crosstab()* Array Creation Routines
Displaying data Shape Manipulation - shape,
•*plot()* resize, transpose
Calculation
Check Link – PyParis 2017 Item Selection and Manipulation
Time Series/Date Functionality Array Conversions
Group by: split-apply-combine
Windowing 189 DT Ch ChCh Ch Ch Ch Ch
Pandas DataFrame
Data representation Pandas Series Attributes
•Series: 1D Indexing, Iteration
•DataFrame: 2D Missing data handling Function application, GroupBy &
Loading data with *read_csv()* window
•header Binary Operator functions
[Link] Synonym for [Link]() with method='bfill'.
Computation/descriptive stats
•names [Link] Synonym for [Link]() with method='bfill'. Reindexing/selection/label
•sep
[Link]([axis, inplace, how]) Return a new Series with missing values removed. manipulation
•encoding Missing Data handling
•converters [Link] Synonym for [Link]() with method='ffill'. Reshaping, sorting, transposing
Getting information [Link]([value, method, axis, …]) Fill NA/NaN values using the specified method. Combining/comparing/joining/m
•index: Series and DataFrame index [Link] Fill NaN values using an interpolation method. erging
Time Series related
•columns: DataFrame column [Link]() Detect missing values. Plotting
•shape: Series and DataFrame dimensions [Link]() Detect missing values. Serialization/IO/Conversion
•info(): DataFrame informations Pandas Series
[Link]() Detect existing (non-missing) values.
•values: Index and Series values Attributes
•unique(): Series unique values [Link]() Detect existing (non-missing) values. Conversion
•nunique(): Series number of unique values [Link] Synonym for [Link]() with method='ffill'. Indexing, Iteration
Function application, GroupBy &
Selecting data [Link] Replace values given in to_replace with value. window
•head() and tail() Binary Operator functions
•Column accessing Reshaping, sorting Computation/descriptive stats
•Row accessing Reindexing/selection/label
•Fancy indexing manipulation
•Logical masking [Link]([axis, kind, order]) Return the integer indices that would sort the Series values. Missing Data handling
Reshaping, sorting
Indexing and merging data [Link]([axis, skipna]) Return int position of the smallest value in the Series.
Combining/comparing/joining/m
•*set_index()* and *reset_index()* [Link]([axis, skipna]) Return int position of the largest value in the Series. erging
•*append()* and *concat()* Series.reorder_levels(order) Rearrange index levels using input order. Time Series related
Computing over data Accessors
Series.sort_values Sort by the values. Datetime properties
•sum()
Series.sort_index Sort Series by index labels. Datetime methods
•[Link](), [Link]() and [Link]()
•*apply()* and lambda [Link]([i, j, copy]) Swap levels i and j in a MultiIndex. Period properties
Timedelta properties
•isin() [Link]([level, fill_value]) Unstack, also known as pivot, Series with MultiIndex to produce DataFrame. Timedelta methods
•div() [Link]([ignore_index]) Transform each element of a list-like to a row. String handling
Organizing data [Link] Find indices where elements should be inserted to maintain order. Categorical Accessor
•transpose() or T Plotting
[Link]([order]) Return the flattened underlying data as an ndarray. Index Objects
•*sort_values()* [Link](repeats[, axis]) Repeat elements of a Series. Numpy Arrays
•*groupby()* Attributes
•*pivot_table()* and *crosstab()*
[Link]([axis]) Squeeze 1 dimensional axis objects into scalars.
Array Creation Routines
Displaying data [Link]([dtype]) Create a new view of the Series. Shape Manipulation - shape,
•*plot()* resize, transpose
Calculation
Check Link – PyParis 2017 Item Selection and Manipulation
Time Series/Date Functionality Array Conversions
Group by: split-apply-combine
Windowing 190 DT Ch ChCh Ch Ch Ch Ch
Pandas DataFrame
Data representation Pandas Series Attributes
•Series: 1D Indexing, Iteration
•DataFrame: 2D Combining / comparing / joining / merging Function application, GroupBy &
Loading data with *read_csv()* window
•header Binary Operator functions
[Link](to_append[, ignore_index, …]) Concatenate two or more Series.
Computation/descriptive stats
•names [Link](other[, align_axis, …]) Compare to another Series and show the differences. Reindexing/selection/label
•sep
[Link](other) Modify Series in place using values from passed Series. manipulation
•encoding Missing Data handling
•converters Reshaping, sorting, transposing
Getting information Time Series-related Combining/comparing/joining/m
•index: Series and DataFrame index erging
Time Series related
•columns: DataFrame column [Link](freq[, method, how, …]) Convert time series to specified frequency. Plotting
•shape: Series and DataFrame dimensions [Link](where[, subset]) Return the last row(s) without any NaNs before where. Serialization/IO/Conversion
•info(): DataFrame informations Pandas Series
[Link]([periods, freq, axis, fill_value]) Shift index by desired number of periods with an optional time freq.
•values: Index and Series values Attributes
•unique(): Series unique values Series.first_valid_index() Return index for first non-NA value or None, if no NA value is found. Conversion
•nunique(): Series number of unique values Series.last_valid_index() Return index for last non-NA value or None, if no NA value is found. Indexing, Iteration
Function application, GroupBy &
Selecting data [Link](rule[, axis, closed, label, …]) Resample time-series data. window
•head() and tail() Series.tz_convert(tz[, axis, level, copy]) Convert tz-aware axis to target time zone. Binary Operator functions
•Column accessing Series.tz_localize(tz[, axis, level, copy, …]) Localize tz-naive index of a Series or DataFrame to target time zone. Computation/descriptive stats
•Row accessing Reindexing/selection/label
Series.at_time(time[, asof, axis]) Select values at particular time of day (e.g., 9:30AM). manipulation
•Fancy indexing
•Logical masking Series.between_time(start_time, end_time[,…]) Select values between particular times of the day (e.g., 9:00-9:30 AM). Missing Data handling
Reshaping, sorting
Indexing and merging data [Link]([periods, freq, axis]) (DEPRECATED) Shift the time index, using the index’s frequency if
Combining/comparing/joining/m
•*set_index()* and *reset_index()* available. erging
•*append()* and *concat()* Series.slice_shift([periods, axis]) (DEPRECATED) Equivalent to shift without copying data. Time Series related
Computing over data Accessors
•sum() Datetime properties
•[Link](), [Link]() and [Link]()
Accessors Datetime methods

•*apply()* and lambda pandas provides dtype-specific methods under various accessors. These are separate namespaces within Series that Period properties
Timedelta properties
•isin() only apply to specific data types. Timedelta methods
•div() String handling
Organizing data Data Type Accessor Categorical Accessor
Plotting
•transpose() or T Datetime, Timedelta, Period dt
Index Objects
•*sort_values()* String str Numpy Arrays
•*groupby()* Categorical cat Attributes
•*pivot_table()* and *crosstab()* Sparse sparse Array Creation Routines
Displaying data Shape Manipulation - shape,
•*plot()* resize, transpose
Calculation
Check Link – PyParis 2017 Item Selection and Manipulation
Time Series/Date Functionality Array Conversions
Group by: split-apply-combine
Windowing 191 DT Ch ChCh Ch Ch Ch Ch
Pandas DataFrame
Data representation Pandas Series Attributes
•Series: 1D Indexing, Iteration
•DataFrame: 2D Datetime properties Function application, GroupBy &
Loading data with *read_csv()* window
•header [Link] Returns numpy array of python [Link] objects (namely, the date part of Binary Operator functions
Computation/descriptive stats
•names Timestamps without timezone information).
Reindexing/selection/label
•sep [Link] Returns numpy array of [Link]. manipulation
•encoding [Link] Returns numpy array of [Link] also containing timezone information. Missing Data handling
•converters [Link] The year of the datetime. Reshaping, sorting, transposing
Getting information Combining/comparing/joining/m
[Link] The month as January=1, December=12. erging
•index: Series and DataFrame index
•columns: DataFrame column [Link] The day of the datetime. Time Series related
[Link] The hours of the datetime. Plotting
•shape: Series and DataFrame dimensions Serialization/IO/Conversion
•info(): DataFrame informations [Link] The minutes of the datetime. Pandas Series
•values: Index and Series values [Link] The seconds of the datetime. Attributes
•unique(): Series unique values [Link] The microseconds of the datetime. Conversion
Indexing, Iteration
•nunique(): Series number of unique values [Link] The nanoseconds of the datetime. Function application, GroupBy &
Selecting data [Link] (DEPRECATED) The week ordinal of the year. window
•head() and tail() [Link] (DEPRECATED) The week ordinal of the year. Binary Operator functions
•Column accessing [Link] The day of the week with Monday=0, Sunday=6. Computation/descriptive stats
•Row accessing Reindexing/selection/label
[Link].day_of_week The day of the week with Monday=0, Sunday=6. manipulation
•Fancy indexing
•Logical masking [Link] The day of the week with Monday=0, Sunday=6. Missing Data handling
[Link] The ordinal day of the year. Reshaping, sorting
Indexing and merging data Combining/comparing/joining/m
•*set_index()* and *reset_index()* [Link].day_of_year The ordinal day of the year. erging
•*append()* and *concat()* [Link] The quarter of the date. Time Series related
Computing over data [Link].is_month_start Indicates whether the date is the first day of the month. Accessors
Datetime properties
•sum() [Link].is_month_end Indicates whether the date is the last day of the month. Datetime methods
•[Link](), [Link]() and [Link]() [Link].is_quarter_startIndicator for whether the date is the first day of a quarter. Period properties
•*apply()* and lambda [Link].is_quarter_end Indicator for whether the date is the last day of a quarter. Timedelta properties
•isin() [Link].is_year_start Indicate whether the date is the first day of a year. Timedelta methods
•div() String handling
[Link].is_year_end Indicate whether the date is the last day of the year.
Organizing data Categorical Accessor
•transpose() or T [Link].is_leap_year Boolean indicator if the date belongs to a leap year. Plotting
•*sort_values()* [Link] The number of days in the month. Index Objects
[Link].days_in_month The number of days in the month. Numpy Arrays
•*groupby()* Attributes
•*pivot_table()* and *crosstab()* [Link] Return timezone, if any. Array Creation Routines
Displaying data [Link] Return the frequency object for this PeriodArray. Shape Manipulation - shape,
•*plot()* resize, transpose
Calculation
Check Link – PyParis 2017 Item Selection and Manipulation
Time Series/Date Functionality Array Conversions
Group by: split-apply-combine
Windowing 192 DT Ch ChCh Ch Ch Ch Ch
Pandas DataFrame
Data representation
•Series: 1D
Pandas Series Attributes
Indexing, Iteration
•DataFrame: 2D Datetime methods Function application, GroupBy &
Loading data with *read_csv()* window
•header [Link].to_period(*args, **kwargs) Cast to PeriodArray/Index at a particular frequency. Binary Operator functions
•names [Link].to_pydatetime() Return the data as an array of native Python datetime objects. Computation/descriptive stats
Reindexing/selection/label
•sep [Link].tz_localize(*args, **kwargs) Localize tz-naive Datetime Array/Index to tz-aware Datetime Array/Index. manipulation
•encoding [Link].tz_convert(*args, **kwargs) Convert tz-aware Datetime Array/Index from one time zone to another. Missing Data handling
•converters [Link](*args, **kwargs) Convert times to midnight. Reshaping, sorting, transposing
Getting information [Link](*args, **kwargs) Convert to Index using specified date_format. Combining/comparing/joining/m
•index: Series and DataFrame index erging
[Link](*args, **kwargs) Perform round operation on the data to the specified freq. Time Series related
•columns: DataFrame column
[Link](*args, **kwargs) Perform floor operation on the data to the specified freq. Plotting
•shape: Series and DataFrame dimensions Serialization/IO/Conversion
•info(): DataFrame informations
[Link](*args, **kwargs) Perform ceil operation on the data to the specified freq.
Pandas Series
•values: Index and Series values [Link].month_name(*args, **kwargs) Return the month names of the DateTimeIndex with specified locale. Attributes
•unique(): Series unique values [Link].day_name(*args, **kwargs) Return the day names of the DateTimeIndex with specified locale. Conversion
Indexing, Iteration
•nunique(): Series number of unique values Period properties Function application, GroupBy &
Selecting data window
•head() and tail() [Link] Binary Operator functions
•Column accessing [Link].start_time Computation/descriptive stats
•Row accessing [Link].end_time Reindexing/selection/label
•Fancy indexing manipulation
•Logical masking Timedelta properties Missing Data handling
Reshaping, sorting
Indexing and merging data [Link] Number of days for each element. Combining/comparing/joining/m
•*set_index()* and *reset_index()* erging
[Link] Number of seconds (>= 0 and less than 1 day) for each element.
•*append()* and *concat()* Time Series related
Computing over data
[Link] Number of microseconds (>= 0 and less than 1 second) for each element. Accessors
•sum() [Link] Number of nanoseconds (>= 0 and less than 1 microsecond) for each element. Datetime properties
•[Link](), [Link]() and [Link]() [Link] Return a Dataframe of the components of the Timedeltas. Datetime methods
Period properties
•*apply()* and lambda Timedelta methods Timedelta properties
•isin() Timedelta methods
•div() [Link].to_pytimedelta() Return an array of native [Link] objects. String handling
Organizing data [Link].total_seconds(*args, **kwargs) Return total duration of each element expressed in seconds. Categorical Accessor
•transpose() or T Plotting
Index Objects
•*sort_values()*
Numpy Arrays
•*groupby()* Check strftime format codes to pass arguments to [Link](*args, **kwargs) Attributes
•*pivot_table()* and *crosstab()* Array Creation Routines
Displaying data Shape Manipulation - shape,
•*plot()* resize, transpose
Calculation
Check Link – PyParis 2017 Item Selection and Manipulation
Time Series/Date Functionality Array Conversions
Group by: split-apply-combine
Windowing 193 DT Ch ChCh Ch Ch Ch Ch
Pandas DataFrame
Data representation Pandas Series Attributes
•Series: 1D Indexing, Iteration
•DataFrame: 2D String Handling Function application, GroupBy &
Loading data with *read_csv()* [Link]() Convert strings in the Series/Index to be capitalized. window
•header [Link]() Convert strings in the Series/Index to be casefolded. Binary Operator functions
Computation/descriptive stats
•names [Link]([others, sep, na_rep, join]) Concatenate strings in the Series/Index with given separator. Reindexing/selection/label
•sep [Link](width[, fillchar]) Pad left and right side of strings in the Series/Index. manipulation
•encoding [Link](pat[, case, flags, na, …]) Test if pattern or regex is contained within a string of a Series or Index. Missing Data handling
•converters Reshaping, sorting, transposing
[Link](pat[, flags]) Count occurrences of pattern in each string of the Series/Index.
Getting information Combining/comparing/joining/m
•index: Series and DataFrame index
[Link](encoding[, errors]) Decode character string in the Series/Index using indicated encoding. erging
•columns: DataFrame column [Link](encoding[, errors]) Encode character string in the Series/Index using indicated encoding. Time Series related
Plotting
•shape: Series and DataFrame dimensions [Link](pat[, na]) Test if the end of each string element matches a pattern.
Serialization/IO/Conversion
•info(): DataFrame informations [Link](pat[, flags, expand]) Extract capture groups in the regex pat as columns in a DataFrame. Pandas Series
•values: Index and Series values [Link](pat[, flags]) Extract capture groups in the regex pat as columns in DataFrame. Attributes
•unique(): Series unique values [Link](sub[, start, end]) Return lowest indexes in each strings in the Series/Index. Conversion
Indexing, Iteration
•nunique(): Series number of unique values [Link](pat[, flags]) Find all occurrences of pattern or regular expression in the Series/Index. Function application, GroupBy &
Selecting data [Link](pat[, case, flags, na]) Determine if each string entirely matches a regular expression. window
•head() and tail() Binary Operator functions
[Link](i) Extract element from each component at specified position.
•Column accessing Computation/descriptive stats
•Row accessing
[Link](sub[, start, end]) Return lowest indexes in each string in Series/Index. Reindexing/selection/label
•Fancy indexing [Link](sep) Join lists contained as elements in the Series/Index with passed delimiter. manipulation
•Logical masking [Link]() Compute the length of each element in the Series/Index. Missing Data handling
Reshaping, sorting
Indexing and merging data [Link](width[, fillchar]) Pad right side of strings in the Series/Index. Combining/comparing/joining/m
•*set_index()* and *reset_index()* [Link]() Convert strings in the Series/Index to lowercase. erging
•*append()* and *concat()* [Link]([to_strip]) Remove leading characters. Time Series related
Computing over data Accessors
[Link](pat[, case, flags, na]) Determine if each string starts with a match of a regular expression. Datetime properties
•sum() [Link](form) Return the Unicode normal form for the strings in the Series/Index. Datetime methods
•[Link](), [Link]() and [Link]()
[Link](width[, side, fillchar]) Pad strings in the Series/Index up to width. Period properties
•*apply()* and lambda Timedelta properties
•isin()
[Link]([sep, expand]) Split the string at the first occurrence of sep.
Timedelta methods
•div() [Link](repeats) Duplicate each string in the Series or Index. String handling
Organizing data [Link](pat, repl[, n, case, …]) Replace each occurrence of pattern/regex in the Series/Index. Categorical Accessor
•transpose() or T [Link](sub[, start, end]) Return highest indexes in each strings in the Series/Index. Plotting
Index Objects
•*sort_values()* [Link](sub[, start, end]) Return highest indexes in each string in Series/Index. Numpy Arrays
•*groupby()* [Link](width[, fillchar]) Pad left side of strings in the Series/Index. Attributes
•*pivot_table()* and *crosstab()* [Link]([sep, expand]) Split the string at the last occurrence of sep. Array Creation Routines
Displaying data [Link]([to_strip]) Remove trailing characters. Shape Manipulation - shape,
•*plot()* resize, transpose
Calculation
Check Link – PyParis 2017 Item Selection and Manipulation
Time Series/Date Functionality Array Conversions
Group by: split-apply-combine
Windowing 194 DT Ch ChCh Ch Ch Ch Ch
Pandas DataFrame
Data representation Pandas Series Attributes
•Series: 1D Indexing, Iteration
•DataFrame: 2D String Handling Function application, GroupBy &
Loading data with *read_csv()* [Link]([start, stop, step]) Slice substrings from each element in the Series or Index. window
•header [Link].slice_replace([start, stop, repl]) Replace a positional slice of a string with another value. Binary Operator functions
[Link]([pat, n, expand]) Split strings around given separator/delimiter. Computation/descriptive stats
•names
Reindexing/selection/label
•sep [Link]([pat, n, expand]) Split strings around given separator/delimiter. manipulation
•encoding [Link](pat[, na]) Test if the start of each string element matches a pattern. Missing Data handling
•converters [Link]([to_strip]) Remove leading and trailing characters. Reshaping, sorting, transposing
Getting information [Link]() Convert strings in the Series/Index to be swapcased. Combining/comparing/joining/m
•index: Series and DataFrame index erging
[Link]() Convert strings in the Series/Index to titlecase. Time Series related
•columns: DataFrame column [Link](table) Map all characters in the string through the given mapping table. Plotting
•shape: Series and DataFrame dimensions [Link]() Convert strings in the Series/Index to uppercase. Serialization/IO/Conversion
•info(): DataFrame informations Pandas Series
[Link](width, **kwargs) Wrap strings in Series/Index at specified line width.
•values: Index and Series values Attributes
[Link](width) Pad strings in the Series/Index by prepending ‘0’ characters. Conversion
•unique(): Series unique values
[Link]() Check whether all characters in each string are alphanumeric. Indexing, Iteration
•nunique(): Series number of unique values
Selecting data [Link]() Check whether all characters in each string are alphabetic. Function application, GroupBy &
window
•head() and tail() [Link]() Check whether all characters in each string are digits.
Binary Operator functions
•Column accessing [Link]() Check whether all characters in each string are whitespace. Computation/descriptive stats
•Row accessing [Link]() Check whether all characters in each string are lowercase. Reindexing/selection/label
•Fancy indexing [Link]() Check whether all characters in each string are uppercase. manipulation
Missing Data handling
•Logical masking [Link]() Check whether all characters in each string are titlecase.
Reshaping, sorting
Indexing and merging data [Link]() Check whether all characters in each string are numeric. Combining/comparing/joining/m
•*set_index()* and *reset_index()* [Link]() Check whether all characters in each string are decimal. erging
•*append()* and *concat()* [Link].get_dummies([sep]) Return DataFrame of dummy/indicator variables for Series. Time Series related
Computing over data Accessors
[Link]([start, stop, step]) Slice substrings from each element in the Series or Index.
•sum() Datetime properties
[Link].slice_replace([start, stop, repl]) Replace a positional slice of a string with another value. Datetime methods
•[Link](), [Link]() and [Link]() [Link]([pat, n, expand]) Split strings around given separator/delimiter. Period properties
•*apply()* and lambda [Link]([pat, n, expand]) Split strings around given separator/delimiter. Timedelta properties
•isin() Timedelta methods
[Link](pat[, na]) Test if the start of each string element matches a pattern.
•div() String handling
[Link]([to_strip]) Remove leading and trailing characters.
Organizing data Categorical Accessor
[Link]() Convert strings in the Series/Index to be swapcased. Plotting
•transpose() or T
•*sort_values()* [Link]() Convert strings in the Series/Index to titlecase. Index Objects
[Link](table) Map all characters in the string through the given mapping table. Numpy Arrays
•*groupby()* Attributes
•*pivot_table()* and *crosstab()* [Link]() Convert strings in the Series/Index to uppercase. Array Creation Routines
Displaying data Shape Manipulation - shape,
•*plot()* resize, transpose
Calculation
Check Link – PyParis 2017 Item Selection and Manipulation
Time Series/Date Functionality Array Conversions
Group by: split-apply-combine
Windowing 195 DT Ch ChCh Ch Ch Ch Ch
Pandas DataFrame
Data representation Pandas Series Attributes
•Series: 1D Indexing, Iteration
•DataFrame: 2D Categorical accessor Function application, GroupBy &
Loading data with *read_csv()* [Link] The categories of this categorical. window
•header [Link] Whether the categories have an ordered relationship. Binary Operator functions
[Link] Return Series of codes as well as the index. Computation/descriptive stats
•names
Reindexing/selection/label
•sep [Link].rename_categories(*args, **kwargs) Rename categories. manipulation
•encoding [Link].reorder_categories(*args, **kwargs) Reorder categories as specified in new_categories. Missing Data handling
•converters [Link].add_categories(*args, **kwargs) Add new categories. Reshaping, sorting, transposing
Getting information [Link].remove_categories(*args, **kwargs) Remove the specified categories. Combining/comparing/joining/m
•index: Series and DataFrame index erging
[Link].remove_unused_categories(*args, …) Remove categories which are not used. Time Series related
•columns: DataFrame column [Link].set_categories(*args, **kwargs) Set the categories to the specified new_categories. Plotting
•shape: Series and DataFrame dimensions [Link].as_ordered(*args, **kwargs) Set the Categorical to be ordered. Serialization/IO/Conversion
•info(): DataFrame informations Pandas Series
[Link].as_unordered(*args, **kwargs) Set the Categorical to be unordered.
•values: Index and Series values Attributes
•unique(): Series unique values Conversion
•nunique(): Series number of unique values Plotting Indexing, Iteration
Function application, GroupBy &
Selecting data window
•head() and tail() [Link](pat[, case, flags, na]) Determine if each string entirely matches a regular expression. Binary Operator functions
•Column accessing Computation/descriptive stats
[Link]([kind, ax, figsize, ….]) Series plotting accessor and method
•Row accessing Reindexing/selection/label
•Fancy indexing [Link]([x, y]) Draw a stacked area plot. manipulation
[Link]([x, y]) Vertical bar plot. Missing Data handling
•Logical masking
Reshaping, sorting
Indexing and merging data [Link]([x, y]) Make a horizontal bar plot. Combining/comparing/joining/m
•*set_index()* and *reset_index()* [Link]([by]) Make a box plot of the DataFrame columns. erging
•*append()* and *concat()* [Link]([bw_method, ind]) Generate Kernel Density Estimate plot using Gaussian kernels. Time Series related
Computing over data Accessors
[Link]([by, bins]) Draw one histogram of the DataFrame’s columns. Datetime properties
•sum()
•[Link](), [Link]() and [Link]() [Link]([bw_method, ind]) Generate Kernel Density Estimate plot using Gaussian kernels. Datetime methods
Period properties
•*apply()* and lambda [Link]([x, y]) Plot Series or DataFrame as lines.
Timedelta properties
•isin() [Link](**kwargs) Generate a pie plot. Timedelta methods
•div() [Link]([by, ax, grid, xlabelsize, …]) Draw histogram of the input series using matplotlib. String handling
Organizing data Categorical Accessor
•transpose() or T Plotting
Index Objects
•*sort_values()*
Numpy Arrays
•*groupby()* Attributes
•*pivot_table()* and *crosstab()* Array Creation Routines
Displaying data Shape Manipulation - shape,
•*plot()* resize, transpose
Calculation
Check Link – PyParis 2017 Item Selection and Manipulation
Time Series/Date Functionality Array Conversions
Group by: split-apply-combine
Windowing 196 DT Ch ChCh Ch Ch Ch Ch
Pandas DataFrame
Data representation Pandas Series Attributes
•Series: 1D Indexing, Iteration
•DataFrame: 2D Categorical accessor Function application, GroupBy &
Loading data with *read_csv()* [Link] The categories of this categorical. window
•header [Link] Whether the categories have an ordered relationship. Binary Operator functions
[Link] Return Series of codes as well as the index. Computation/descriptive stats
•names
Reindexing/selection/label
•sep [Link].rename_categories(*args, **kwargs) Rename categories. manipulation
•encoding [Link].reorder_categories(*args, **kwargs) Reorder categories as specified in new_categories. Missing Data handling
•converters [Link].add_categories(*args, **kwargs) Add new categories. Reshaping, sorting, transposing
Getting information [Link].remove_categories(*args, **kwargs) Remove the specified categories. Combining/comparing/joining/m
•index: Series and DataFrame index erging
[Link].remove_unused_categories(*args, …) Remove categories which are not used. Time Series related
•columns: DataFrame column [Link].set_categories(*args, **kwargs) Set the categories to the specified new_categories. Plotting
•shape: Series and DataFrame dimensions [Link].as_ordered(*args, **kwargs) Set the Categorical to be ordered. Serialization/IO/Conversion
•info(): DataFrame informations Pandas Series
[Link].as_unordered(*args, **kwargs) Set the Categorical to be unordered.
•values: Index and Series values Attributes
•unique(): Series unique values Conversion
•nunique(): Series number of unique values Plotting Indexing, Iteration
Function application, GroupBy &
Selecting data window
•head() and tail() [Link](pat[, case, flags, na]) Determine if each string entirely matches a regular expression. Binary Operator functions
•Column accessing Computation/descriptive stats
[Link]([kind, ax, figsize, ….]) Series plotting accessor and method
•Row accessing Reindexing/selection/label
•Fancy indexing [Link]([x, y]) Draw a stacked area plot. manipulation
[Link]([x, y]) Vertical bar plot. Missing Data handling
•Logical masking
Reshaping, sorting
Indexing and merging data [Link]([x, y]) Make a horizontal bar plot. Combining/comparing/joining/m
•*set_index()* and *reset_index()* [Link]([by]) Make a box plot of the DataFrame columns. erging
•*append()* and *concat()* Time Series related
[Link]([bw_method, ind]) Generate Kernel Density Estimate plot using Gaussian kernels. Accessors
Computing over data
•sum() [Link]([by, bins]) Draw one histogram of the DataFrame’s columns. Datetime properties
[Link]([bw_method, ind]) Generate Kernel Density Estimate plot using Gaussian kernels. Datetime methods
•[Link](), [Link]() and [Link]()
Period properties
•*apply()* and lambda [Link]([x, y]) Plot Series or DataFrame as lines. Timedelta properties
•isin() [Link](**kwargs) Generate a pie plot. Timedelta methods
•div() [Link]([by, ax, grid, xlabelsize, …]) Draw histogram of the input series using matplotlib. String handling
Organizing data Categorical Accessor
•transpose() or T Plotting
Index Objects
•*sort_values()*
Numpy Arrays
•*groupby()* Attributes
•*pivot_table()* and *crosstab()* Array Creation Routines
Displaying data Shape Manipulation - shape,
•*plot()* resize, transpose
Calculation
Check Link – PyParis 2017 Item Selection and Manipulation
Time Series/Date Functionality Array Conversions
Group by: split-apply-combine
Windowing 197 DT Ch ChCh Ch Ch Ch Ch
Data representation Index Object Pandas DataFrame
Attributes
•Series: 1D
Indexing, Iteration
•DataFrame: 2D Properties
Function application, GroupBy &
Loading data with *read_csv()* window
•header [Link] Return an array representing the data in the Index. Binary Operator functions
•names Index.is_monotonic Alias for is_monotonic_increasing. Computation/descriptive stats
•sep Index.is_monotonic_increasing Return if the index is monotonic increasing (only equal or increasing) values. Reindexing/selection/label
Index.is_monotonic_decreasing Return if the index is monotonic decreasing (only equal or decreasing) values. manipulation
•encoding Missing Data handling
•converters Index.is_unique Return if the index has unique values.
Reshaping, sorting, transposing
Getting information Index.has_duplicates Check if the Index has duplicate values. Combining/comparing/joining/m
•index: Series and DataFrame index [Link] Return if I have any nans; enables various perf speedups. erging
•columns: DataFrame column [Link] Return the dtype object of the underlying data. Time Series related
Index Objects Plotting
•shape: Series and DataFrame dimensions Index.inferred_type Return a string of the type inferred from the values.
Properties Serialization/IO/Conversion
•info(): DataFrame informations Index.is_all_dates Whether or not the index values only consist of dates. Modifying and
[Link] Return a tuple of the shape of the underlying data. Pandas Series
•values: Index and Series values computations
Attributes
•unique(): Series unique values [Link] Return Index or MultiIndex name. Compatibility with
Conversion
[Link] MultiIndex
•nunique(): Series number of unique values Indexing, Iteration
Missing Values
Selecting data [Link] Return the number of bytes in the underlying data. Function application, GroupBy &
Conversion
•head() and tail() [Link] Number of dimensions of the underlying data, by definition 1. Sorting
window
[Link] Return the number of elements in the underlying data. Binary Operator functions
•Column accessing Timespecific
Computation/descriptive stats
•Row accessing [Link] Operations
Reindexing/selection/label
•Fancy indexing Index.T Return the transpose, which is by definition self. Combining/Joining
manipulation
Index.memory_usage([deep]) Memory usage of the values. /Set Operations
•Logical masking Selecting
Missing Data handling
Indexing and merging data [Link] Return an array representing the data in the Index. Reshaping, sorting
•*set_index()* and *reset_index()* Index.is_monotonic Alias for is_monotonic_increasing. Combining/comparing/joining/m
Index.is_monotonic_increasing Return if the index is monotonic increasing (only equal or increasing) values. erging
•*append()* and *concat()* Time Series related
Computing over data Index.is_monotonic_decreasing Return if the index is monotonic decreasing (only equal or decreasing) values.
Accessors
•sum() Index.is_unique Return if the index has unique values. Datetime properties
•[Link](), [Link]() and [Link]() Index.has_duplicates Check if the Index has duplicate values. Datetime methods
•*apply()* and lambda [Link] Return if I have any nans; enables various perf speedups. Period properties
•isin() [Link] Return the dtype object of the underlying data. Timedelta properties
Timedelta methods
•div() String handling
Organizing data Categorical Accessor
•transpose() or T Plotting
•*sort_values()* Numpy Arrays
•*groupby()* Attributes
•*pivot_table()* and *crosstab()* Array Creation Routines
Displaying data Shape Manipulation - shape,
resize, transpose
•*plot()* Calculation
Check Link – PyParis 2017 Item Selection and Manipulation
Time Series/Date Functionality Array Conversions
Group by: split-apply-combine
Windowing 198 DT Ch ChCh Ch Ch Ch Ch
Data representation Index Object Pandas DataFrame
Attributes
•Series: 1D
Indexing, Iteration
•DataFrame: 2D Modifying and computations
Function application, GroupBy &
Loading data with *read_csv()* window
•header [Link](*args, **kwargs) Return whether all elements are Truthy. Binary Operator functions
•names [Link](*args, **kwargs) Return whether any element is Truthy. Computation/descriptive stats
•sep [Link]([axis, skipna]) Return int position of the smallest value in the Series. Reindexing/selection/label
•encoding [Link]([axis, skipna]) Return int position of the largest value in the Series. manipulation
[Link]([name, deep, dtype, names]) Make a copy of this object. Missing Data handling
•converters Reshaping, sorting, transposing
Getting information [Link](loc) Make new Index with passed location(-s) deleted.
Combining/comparing/joining/m
•index: Series and DataFrame index [Link](labels[, errors]) Make new Index with passed list of labels deleted. erging
•columns: DataFrame column Index.drop_duplicates([keep]) Return Index with duplicate values removed. Time Series related
[Link]([keep]) Indicate duplicate index values. Index Objects Plotting
•shape: Series and DataFrame dimensions Properties
•info(): DataFrame informations [Link](other) Determine if two Index object are equal. Serialization/IO/Conversion
Modifying and
•values: Index and Series values [Link]([sort, na_sentinel]) Encode the object as an enumerated type or categorical computations Pandas Series
variable. Attributes
•unique(): Series unique values Compatibility with
Conversion
•nunique(): Series number of unique values [Link](other) Similar to equals, but checks that object attributes and types are MultiIndex
Indexing, Iteration
also equal. Missing Values
Selecting data Conversion
Function application, GroupBy &
•head() and tail() [Link](loc, item) Make new Index inserting new item at location. window
Sorting
•Column accessing Index.is_(other) More flexible, faster check like is but that works through views. Timespecific
Binary Operator functions

•Row accessing Index.is_boolean() Check if the Index only consists of booleans. Operations
Computation/descriptive stats
Index.is_categorical() Check if the Index holds categorical data. Reindexing/selection/label
Combining/Joining
•Fancy indexing manipulation
•Logical masking Index.is_floating() Check if the Index is a floating type. /Set Operations
Missing Data handling
Index.is_integer() Check if the Index only consists of integers. Selecting
Indexing and merging data Reshaping, sorting
•*set_index()* and *reset_index()* Index.is_interval() Check if the Index holds Interval objects. Combining/comparing/joining/m
•*append()* and *concat()* Index.is_mixed() Check if the Index holds data with mixed data types. erging
Index.is_numeric() Check if the Index only consists of numeric data. Time Series related
Computing over data Accessors
•sum() Index.is_object() Check if the Index is of the object dtype.
Datetime properties
•[Link](), [Link]() and [Link]() [Link]([axis, skipna]) Return the minimum value of the Index. Datetime methods
•*apply()* and lambda [Link]([axis, skipna]) Return the maximum value of the Index. Period properties
•isin() [Link](target[, method, level, …]) Create index with target’s values. Timedelta properties
•div() [Link](name[, inplace]) Alter Index or MultiIndex name. Timedelta methods
[Link](repeats[, axis]) Repeat elements of a Index. String handling
Organizing data Categorical Accessor
•transpose() or T [Link](cond[, other]) Replace values where the condition is False.
Plotting
•*sort_values()* [Link](indices[, axis, allow_fill, …]) Return a new Index of the values selected by the indices.
Numpy Arrays
•*groupby()* [Link](mask, value) Return a new Index of the values set with the mask.
Attributes
•*pivot_table()* and *crosstab()* [Link]([level]) Return unique values in the index. Array Creation Routines
Displaying data [Link]([dropna]) Return number of unique elements in the object. Shape Manipulation - shape,
•*plot()* Index.value_counts([normalize, sort, …]) Return a Series containing counts of unique values. resize, transpose
[Link](cond[, other]) Replace values where the condition is False. Calculation
Check Link – PyParis 2017 Item Selection and Manipulation
[Link](indices[, axis, allow_fill, …]) Return a new Index of the values selected by the indices.
Time Series/Date Functionality Array Conversions
Group by: split-apply-combine
Windowing 199 DT Ch ChCh Ch Ch Ch Ch
Data representation Index Object Pandas DataFrame
Attributes
•Series: 1D
Indexing, Iteration
•DataFrame: 2D Compatibility with MultiIndex
Function application, GroupBy &
Loading data with *read_csv()* window
•header Index.set_names(names[, level, inplace]) Set Index or MultiIndex name. Binary Operator functions
•names [Link]([level]) Return index with requested level(s) removed. Computation/descriptive stats
•sep Reindexing/selection/label
Missing values manipulation
•encoding Missing Data handling
•converters Reshaping, sorting, transposing
Getting information [Link]([value, downcast]) Fill NA/NaN values with the specified value. Combining/comparing/joining/m
•index: Series and DataFrame index [Link]([how]) Return Index without NA/NaN values. erging
•columns: DataFrame column [Link]() Detect missing values. Time Series related
Index Objects Plotting
•shape: Series and DataFrame dimensions [Link]() Detect existing (non-missing) values.
Properties Serialization/IO/Conversion
•info(): DataFrame informations Modifying and
Conversion Pandas Series
•values: Index and Series values computations
Attributes
•unique(): Series unique values Compatibility with
Conversion
[Link](dtype[, copy]) Create an Index with values cast to dtypes. MultiIndex
•nunique(): Series number of unique values Indexing, Iteration
Missing Values
Selecting data [Link]() Return the first element of the underlying data as a Python scalar. Function application, GroupBy &
Conversion
•head() and tail() [Link](mapper[, na_action]) Map values using input correspondence (a dict, Series, or function). Sorting
window
[Link]([order]) Return an ndarray of the flattened values of the underlying data. Binary Operator functions
•Column accessing Timespecific
Computation/descriptive stats
•Row accessing Index.to_list() Return a list of the values. Operations
Reindexing/selection/label
•Fancy indexing Index.to_native_types([slicer]) (DEPRECATED) Format specified values of self and return them. Combining/Joining
manipulation
Index.to_series([index, name]) Create a Series with both index and values equal to the index keys. /Set Operations
•Logical masking Selecting
Missing Data handling
Indexing and merging data Index.to_frame([index, name]) Create a DataFrame with a column containing the Index. Reshaping, sorting
•*set_index()* and *reset_index()* [Link]([cls]) Combining/comparing/joining/m
erging
•*append()* and *concat()* Time Series related
Computing over data Sorting
Accessors
•sum() Datetime properties
•[Link](), [Link]() and [Link]() [Link](*args, **kwargs) Return the integer indices that would sort the index. Datetime methods
•*apply()* and lambda [Link](value[, side, sorter]) Find indices where elements should be inserted to maintain order. Period properties
•isin() Index.sort_values([return_indexer, …]) Return a sorted copy of the index. Timedelta properties
Timedelta methods
•div() String handling
Organizing data Time-specific operations Categorical Accessor
•transpose() or T [Link]([periods, freq]) Shift index by desired number of time frequency increments. Plotting
•*sort_values()* Numpy Arrays
•*groupby()* Attributes
•*pivot_table()* and *crosstab()* Array Creation Routines
Displaying data Shape Manipulation - shape,
resize, transpose
•*plot()* Calculation
Check Link – PyParis 2017 Item Selection and Manipulation
Time Series/Date Functionality Array Conversions
Group by: split-apply-combine
Windowing 200 DT Ch ChCh Ch Ch Ch Ch
Data representation Index Object Pandas DataFrame
Attributes
•Series: 1D
Indexing, Iteration
•DataFrame: 2D Combining / joining / set operations
Function application, GroupBy &
Loading data with *read_csv()* window
•header [Link](other) Append a collection of Index options together. Binary Operator functions
•names [Link](other[, how, level, …]) Compute join_index and indexers to conform data structures to Computation/descriptive stats
Reindexing/selection/label
•sep the new index.
manipulation
•encoding [Link](other[, sort]) Form the intersection of two Index objects. Missing Data handling
•converters [Link](other[, sort]) Form the union of two Index objects. Reshaping, sorting, transposing
Getting information [Link](other[, sort]) Return a new Index with elements of index not in other. Combining/comparing/joining/m
•index: Series and DataFrame index Index.symmetric_difference(other[, …]) Compute the symmetric difference of two Index objects. erging
•columns: DataFrame column Time Series related
Index Objects Plotting
•shape: Series and DataFrame dimensions Selecting Properties Serialization/IO/Conversion
•info(): DataFrame informations Modifying and
Pandas Series
•values: Index and Series values [Link](label) Return the label from the index, or, if not present, the previous
computations
Attributes
•unique(): Series unique values Compatibility with
one. Conversion
MultiIndex
•nunique(): Series number of unique values Indexing, Iteration
Index.asof_locs(where, mask) Return the locations (indices) of labels in the index. Missing Values
Selecting data Conversion
Function application, GroupBy &
•head() and tail()
Index.get_indexer Compute indexer and mask for new index given the current window
Sorting
•Column accessing index. Timespecific
Binary Operator functions

•Row accessing Index.get_indexer_for(target, **kwargs) Guaranteed return of an indexer even when non-unique. Operations
Computation/descriptive stats
Reindexing/selection/label
•Fancy indexing Index.get_indexer_non_unique(target) Compute indexer and mask for new index given the current Combining/Joining
manipulation
index. /Set Operations
•Logical masking Selecting
Missing Data handling
Indexing and merging data Index.get_level_values(level) Return an Index of values for requested level. Reshaping, sorting
•*set_index()* and *reset_index()* Index.get_loc(key[, method, tolerance]) Get integer location, slice or boolean mask for requested label. Combining/comparing/joining/m
erging
•*append()* and *concat()* Index.get_slice_bound(label, side[, kind]) Calculate slice bound that corresponds to given label.
Time Series related
Computing over data Index.get_value(series, key) Fast lookup of value from 1-dimensional ndarray. Accessors
•sum() [Link](values[, level]) Return a boolean array where the index values are in values. Datetime properties
•[Link](), [Link]() and [Link]() Index.slice_indexer) Compute the slice indexer for input labels and step. Datetime methods
•*apply()* and lambda Index.slice_locs([start, end, step, kind]) Compute slice locations for input labels. Period properties
•isin() Timedelta properties
Timedelta methods
•div() String handling
Organizing data Categorical Accessor
•transpose() or T Plotting
•*sort_values()* Numpy Arrays
•*groupby()* Attributes
•*pivot_table()* and *crosstab()* Array Creation Routines
Displaying data Shape Manipulation - shape,
resize, transpose
•*plot()* Calculation
Check Link – PyParis 2017 Item Selection and Manipulation
Time Series/Date Functionality Array Conversions
Group by: split-apply-combine
Windowing 201 DT Ch ChCh Ch Ch Ch Ch
Pandas DataFrame
Data representation
•Series: 1D
Numpy : Attributes of N-dimensional Array Attributes
Indexing, Iteration
•DataFrame: 2D Memory layout : Function application, GroupBy &
Loading data with *read_csv()* The following attributes contain information about the memory layout of the window
•header Binary Operator functions
array: Computation/descriptive stats
•names
Reindexing/selection/label
•sep manipulation
•encoding [Link] Information about the memory layout of the array.
Missing Data handling
•converters [Link] Tuple of array dimensions. Reshaping, sorting, transposing
Getting information [Link] Tuple of bytes to step in each dimension when traversing an Combining/comparing/joining/m
•index: Series and DataFrame index array. erging
Time Series related
•columns: DataFrame column [Link] Number of array dimensions. Plotting
•shape: Series and DataFrame dimensions [Link] Python buffer object pointing to the start of the array’s data. Serialization/IO/Conversion
•info(): DataFrame informations Pandas Series
[Link] Number of elements in the array.
•values: Index and Series values Attributes
•unique(): Series unique values [Link] Length of one array element in bytes. Conversion
•nunique(): Series number of unique values [Link] Total bytes consumed by the elements of the array. Indexing, Iteration
Function application, GroupBy &
Selecting data [Link] Base object if memory is from some other object.
window
•head() and tail() Binary Operator functions
•Column accessing Computation/descriptive stats
•Row accessing Other Attributes Reindexing/selection/label
•Fancy indexing [Link] Data-type of the array’s elements. manipulation
ndarray.T The transposed array. Missing Data handling
•Logical masking
Reshaping, sorting
Indexing and merging data [Link] The real part of the array. Combining/comparing/joining/m
•*set_index()* and *reset_index()* [Link] The imaginary part of the array. erging
•*append()* and *concat()* [Link] A 1-D iterator over the array. Time Series related
Computing over data [Link] An object to simplify the interaction of the array with the ctypes Accessors
•sum() Datetime properties
module.
Datetime methods
•[Link](), [Link]() and [Link]()
Period properties
•*apply()* and lambda Timedelta properties
•isin() Timedelta methods
•div() String handling
Organizing data Categorical Accessor
•transpose() or T Plotting
Index Objects
•*sort_values()*
Numpy Arrays
•*groupby()* Attributes
•*pivot_table()* and *crosstab()* Array Creation Routines
Displaying data Shape Manipulation - shape,
•*plot()* resize, transpose
Calculation
Check Link – PyParis 2017 Item Selection and Manipulation
Time Series/Date Functionality Array Conversions
Group by: split-apply-combine
Windowing 202 DT Ch ChCh Ch Ch Ch Ch
Pandas DataFrame
Data representation
•Series: 1D
Numpy Array Creation Routines Attributes
Indexing, Iteration
•DataFrame: 2D Numerical ranges Function application, GroupBy &
Loading data with *read_csv()* window
•header [Link] Return evenly spaced values within a given interval. Binary Operator functions
Computation/descriptive stats
•names [Link] Return evenly spaced numbers over a specified interval.
Reindexing/selection/label
•sep [Link] Return numbers spaced evenly on a log scale. manipulation
•encoding [Link] Return numbers spaced evenly on a log scale (a geometric progression). Missing Data handling
•converters [Link] Return coordinate matrices from coordinate vectors. Reshaping, sorting, transposing
Getting information [Link] nd_grid instance which returns a dense multi-dimensional “meshgrid”. Combining/comparing/joining/m
•index: Series and DataFrame index [Link] nd_grid instance which returns an open multi-dimensional “meshgrid”. erging
Time Series related
•columns: DataFrame column
From shape or value Plotting
•shape: Series and DataFrame dimensions Serialization/IO/Conversion
•info(): DataFrame informations Pandas Series
[Link] Return a new array of given shape and type, without initializing entries.
•values: Index and Series values Attributes
np.empty_like Return a new array with the same shape and type as a given array. Conversion
•unique(): Series unique values
[Link] Return a 2-D array with ones on the diagonal and zeros elsewhere. Indexing, Iteration
•nunique(): Series number of unique values
[Link] Return the identity array. Function application, GroupBy &
Selecting data window
[Link] Return a new array of given shape and type, filled with ones.
•head() and tail() Binary Operator functions
np.ones_like Return an array of ones with the same shape and type as a given array.
•Column accessing Computation/descriptive stats
[Link] Return a new array of given shape and type, filled with zeros.
•Row accessing Reindexing/selection/label
np.zeros_like Return an array of zeros with the same shape and type as a given array. manipulation
•Fancy indexing
[Link] Return a new array of given shape and type, filled with fill_value. Missing Data handling
•Logical masking
np.full_like Return a full array with the same shape and type as a given array. Reshaping, sorting
Indexing and merging data Combining/comparing/joining/m
•*set_index()* and *reset_index()* Building matrices erging
•*append()* and *concat()* Time Series related
Computing over data diag Extract a diagonal or construct a diagonal array. Accessors
•sum() Datetime properties
diagflat Create a two-dimensional array with the flattened input as a diagonal.
Datetime methods
•[Link](), [Link]() and [Link]() tri An array with ones at and below the given diagonal and zeros elsewhere. Period properties
•*apply()* and lambda tril Lower triangle of an array. Timedelta properties
•isin() triu Upper triangle of an array. Timedelta methods
•div() vander Generate a Vandermonde matrix. String handling
Organizing data Categorical Accessor
•transpose() or T Plotting
Index Objects
•*sort_values()*
Numpy Arrays
•*groupby()* Attributes
•*pivot_table()* and *crosstab()* Array Creation Routines
Displaying data Shape Manipulation - shape,
•*plot()* resize, transpose
Calculation
Check Link – PyParis 2017 Item Selection and Manipulation
Time Series/Date Functionality Array Conversions
Group by: split-apply-combine
Windowing 203 DT Ch ChCh Ch Ch Ch Ch
Pandas DataFrame
Data representation
•Series: 1D Numpy Array Creation Routines Attributes
Indexing, Iteration
•DataFrame: 2D Function application, GroupBy &
Loading data with *read_csv()* From existing data window
•header Binary Operator functions
•names [Link] Create an array. Computation/descriptive stats
[Link] Convert the input to an array. Reindexing/selection/label
•sep manipulation
•encoding [Link] Convert the input to an ndarray, but pass ndarray subclasses through. Missing Data handling
•converters [Link] Return a contiguous array (ndim >= 1) in memory (C order). Reshaping, sorting, transposing
Getting information [Link] Interpret the input as a matrix. Combining/comparing/joining/m
•index: Series and DataFrame index erging
[Link] Return an array copy of the given object.
Time Series related
•columns: DataFrame column [Link] Interpret a buffer as a 1-dimensional array. Plotting
•shape: Series and DataFrame dimensions [Link] Construct an array from data in a text or binary file. Serialization/IO/Conversion
•info(): DataFrame informations [Link] Construct an array by executing a function over each coordinate. Pandas Series
•values: Index and Series values [Link] Create a new 1-dimensional array from an iterable object. Attributes
•unique(): Series unique values Conversion
[Link] A new 1-D array initialized from text data in a string. Indexing, Iteration
•nunique(): Series number of unique values
[Link] Load data from a text file. Function application, GroupBy &
Selecting data window
•head() and tail() Binary Operator functions
•Column accessing Computation/descriptive stats
•Row accessing Reindexing/selection/label
•Fancy indexing manipulation
Missing Data handling
•Logical masking
Reshaping, sorting
Indexing and merging data Combining/comparing/joining/m
•*set_index()* and *reset_index()* erging
•*append()* and *concat()* Time Series related
Computing over data Accessors
•sum() Datetime properties
Datetime methods
•[Link](), [Link]() and [Link]()
Period properties
•*apply()* and lambda Timedelta properties
•isin() Timedelta methods
•div() String handling
Organizing data Categorical Accessor
•transpose() or T Plotting
Index Objects
•*sort_values()*
Numpy Arrays
•*groupby()* Attributes
•*pivot_table()* and *crosstab()* Array Creation Routines
Displaying data Shape Manipulation - shape,
•*plot()* resize, transpose
Calculation
Check Link – PyParis 2017 Item Selection and Manipulation
Time Series/Date Functionality Array Conversions
Group by: split-apply-combine
Windowing 204 DT Ch ChCh Ch Ch Ch Ch
Data representation Numpy Array Methods Pandas DataFrame
Attributes
•Series: 1D Indexing, Iteration
•DataFrame: 2D Shape manipulation – for shape, resize, transpose
Function application, GroupBy &
Loading data with *read_csv()* window
•header [Link] Returns an array containing the same data with a new shape. Binary Operator functions
•names [Link] Change shape and size of array in-place. Computation/descriptive stats
[Link] Returns a view of the array with axes transposed. Reindexing/selection/label
•sep manipulation
•encoding [Link] Return a view of the array with axis1 and axis2interchanged.
Missing Data handling
•converters [Link] Return a copy of the array collapsed into one dimension. Reshaping, sorting, transposing
Getting information [Link] Return a flattened array. Combining/comparing/joining/m
•index: Series and DataFrame index [Link] Remove axes of length one from a. erging
[Link] Returns an array containing the same data with a new shape. Time Series related
•columns: DataFrame column
Plotting
•shape: Series and DataFrame dimensions [Link] Change shape and size of array in-place.
Serialization/IO/Conversion
•info(): DataFrame informations [Link] Returns a view of the array with axes transposed.
Pandas Series
•values: Index and Series values Calculation Attributes
•unique(): Series unique values Conversion
Indexing, Iteration
•nunique(): Series number of unique values
[Link] Return the maximum along a given axis. Function application, GroupBy &
Selecting data
[Link] Return indices of the maximum values along the given axis. window
•head() and tail() Binary Operator functions
•Column accessing
[Link] Return the minimum along a given axis.
Computation/descriptive stats
•Row accessing [Link] Return indices of the minimum values along the given axis. Reindexing/selection/label
•Fancy indexing [Link] Peak to peak (maximum - minimum) value along a given axis. manipulation
•Logical masking [Link] Return an array whose values are limited to [min, max]. Missing Data handling
Reshaping, sorting
Indexing and merging data [Link] Complex-conjugate all elements.
Combining/comparing/joining/m
•*set_index()* and *reset_index()* [Link] Return a with each element rounded to the given number of decimals. erging
•*append()* and *concat()* [Link] Return the sum along diagonals of the array. Time Series related
Computing over data [Link] Return the sum of the array elements over the given axis. Accessors
•sum() [Link] Return the cumulative sum of the elements along the given axis. Datetime properties
Datetime methods
•[Link](), [Link]() and [Link]() [Link] Returns the average of the array elements along given axis. Period properties
•*apply()* and lambda [Link] Returns the variance of the array elements, along given axis. Timedelta properties
•isin() [Link] Returns the standard deviation of the array elements along given axis. Timedelta methods
•div() [Link] Return the product of the array elements over the given axis String handling
Organizing data [Link] Return the cumulative product of the elements along the given axis. Categorical Accessor
•transpose() or T Plotting
[Link] Returns True if all elements evaluate to True. Index Objects
•*sort_values()* [Link] Returns True if any of the elements of a evaluate to True. Numpy Arrays
•*groupby()* Attributes
•*pivot_table()* and *crosstab()* Array Creation Routines
Displaying data Shape Manipulation - shape,
•*plot()* resize, transpose
Calculation
Check Link – PyParis 2017 Item Selection and Manipulation
Time Series/Date Functionality Array Conversions
Group by: split-apply-combine
Windowing 205 DT Ch ChCh Ch Ch Ch Ch
Pandas DataFrame
Data representation Numpy Array Methods Attributes
•Series: 1D Indexing, Iteration
•DataFrame: 2D Item Selection and Manipulation Function application, GroupBy &
Loading data with *read_csv()* window
•header [Link] Return an array formed from the elements of a at the given indices. Binary Operator functions
[Link] Set [Link][n] = values[n] for all n in indices. Computation/descriptive stats
•names
[Link] Repeat elements of an array. Reindexing/selection/label
•sep manipulation
[Link] Use an index array to construct a new array from a set of choices.
•encoding Missing Data handling
[Link] Sort an array in-place.
•converters Reshaping, sorting, transposing
[Link] Returns the indices that would sort this array.
Getting information Combining/comparing/joining/m
[Link] Rearranges the elements in the array in such a way that the value of the element erging
•index: Series and DataFrame index in kth position is in the position it would be in a sorted array. Time Series related
•columns: DataFrame column [Link] Returns the indices that would partition this array. Plotting
•shape: Series and DataFrame dimensions [Link] Find indices where elements of v should be inserted in a to maintain order. Serialization/IO/Conversion
•info(): DataFrame informations [Link] Return the indices of the elements that are non-zero. Pandas Series
•values: Index and Series values [Link] Return selected slices of this array along given axis. Attributes
•unique(): Series unique values [Link] Return specified diagonals. Conversion
Indexing, Iteration
•nunique(): Series number of unique values [Link] Return an array formed from the elements of a at the given indices.
Function application, GroupBy &
Selecting data [Link] Set [Link][n] = values[n] for all n in indices. window
•head() and tail() [Link] Repeat elements of an array. Binary Operator functions
•Column accessing [Link] Use an index array to construct a new array from a set of choices. Computation/descriptive stats
•Row accessing [Link] Sort an array in-place. Reindexing/selection/label
•Fancy indexing [Link] Returns the indices that would sort this array. manipulation
[Link] Rearranges the elements in the array in such a way that the value of the element Missing Data handling
•Logical masking
Reshaping, sorting
Indexing and merging data in kth position is in the position it would be in a sorted array.
Combining/comparing/joining/m
•*set_index()* and *reset_index()* [Link] Returns the indices that would partition this array.
erging
•*append()* and *concat()* [Link] Find indices where elements of v should be inserted in a to maintain order. Time Series related
Computing over data [Link] Return the indices of the elements that are non-zero. Accessors
[Link] Return selected slices of this array along given axis. Datetime properties
•sum()
[Link] Return specified diagonals. Datetime methods
•[Link](), [Link]() and [Link]()
Period properties
•*apply()* and lambda Array Conversions Timedelta properties
•isin() [Link] Timedelta methods
Copy an element of an array to a standard Python scalar and return it.
•div() String handling
Organizing data [Link] Return the array as an [Link]-levels deep nested list of Python scalars. Categorical Accessor
•transpose() or T [Link] Insert scalar into an array (scalar is cast to array’s dtype, if possible) Plotting
Index Objects
•*sort_values()* [Link] A compatibility alias for tobytes, with exactly the same behavior.
Numpy Arrays
•*groupby()* [Link] Construct Python bytes containing the raw data bytes in the array. Attributes
•*pivot_table()* and *crosstab()* [Link] Write array to a file as text or binary (default). Array Creation Routines
Displaying data Shape Manipulation - shape,
[Link] Copy of the array, cast to a specified type. resize, transpose
•*plot()*
[Link] Fill the array with a scalar value. Calculation
Check Link – PyParis 2017 Item Selection and Manipulation
Time Series/Date Functionality Array Conversions
Group by: split-apply-combine
Windowing 206 DT Ch ChCh Ch Ch Ch Ch
Pandas DataFrame
Data representation
•Series: 1D Useful Tips for Data wrangling with Pandas Attributes
Indexing, Iteration
•DataFrame: 2D Function application, GroupBy &
Loading data with *read_csv()* General order of precedence for performance of various operations: window
•header Binary Operator functions
1. Vectorization Computation/descriptive stats
•names
•sep
2. Cython routines Reindexing/selection/label

•encoding 3. List Comprehensions (vanilla for loop) manipulation


Missing Data handling
•converters 4. [Link](): Reshaping, sorting, transposing
Getting information • Reductions that can be performed in Cython, Combining/comparing/joining/m
•index: Series and DataFrame index erging
• Iteration in Python space Time Series related
•columns: DataFrame column
•shape: Series and DataFrame dimensions
5. [Link]() and iteritems() Plotting
Serialization/IO/Conversion
•info(): DataFrame informations 6. [Link]()
Pandas Series
•values: Index and Series values iterrows and itertuples should be used in very rare circumstances, such as generating row Attributes
•unique(): Series unique values objects/nametuples for sequential processing, which is really the only thing these functions are Conversion
Indexing, Iteration
•nunique(): Series number of unique values useful for. Function application, GroupBy &
Selecting data window
•head() and tail() Binary Operator functions
•Column accessing Check Computation/descriptive stats
•Row accessing • Essential basic functionality Reindexing/selection/label
manipulation
•Fancy indexing • [Link] Missing Data handling
•Logical masking
Reshaping, sorting
Indexing and merging data Combining/comparing/joining/m
•*set_index()* and *reset_index()*
•*append()* and *concat()*
On Groupby erging
Time Series related
Computing over data Accessors
Datetime properties
•sum() The groupby object has four methods that accept a function (or functions) to perform a calculation
Datetime methods
•[Link](), [Link]() and [Link]() on each group. These four methods are .agg, .filter, .transform, and .apply. Each of the first three Period properties
•*apply()* and lambda
of these methods has a very specific output that the function must return. .agg must return a scalar Timedelta properties
•isin() Timedelta methods
•div() value, .filter must return a Boolean, and .transform must return a Series or DataFrame with the String handling
Organizing data same length as the passed group. The .apply method, however, may return a scalar value, a Series, Categorical Accessor
•transpose() or T or even a DataFrame of any shape, therefore making it very flexible. It is also called only once per Plotting
Index Objects
•*sort_values()* group (on a DataFrame), while the .transform and .agg methods get called once for each Numpy Arrays
•*groupby()*
aggregating column (on a Series). The .apply method's ability to return a single object when Attributes
•*pivot_table()* and *crosstab()* Array Creation Routines
Displaying data operating on multiple columns at the same time is of tremendous utility. Shape Manipulation - shape,
•*plot()* resize, transpose
• Read Pandas 1.x Cookbook, Second Edition, Matt Harrison Theodore Petrou [Grouping for Aggregation, Filtration, and Calculation
Check Link – PyParis 2017 Item Selection and Manipulation
Transformation]
Time Series/Date Functionality Array Conversions
Group by: split-apply-combine
Windowing 207 DT Ch ChCh Ch Ch Ch Ch
Pandas DataFrame
Data representation
•Series: 1D Group by: split-apply-combine Attributes
Indexing, Iteration
•DataFrame: 2D Function application, GroupBy &
Loading data with *read_csv()* By “group by” we are referring to a process involving one or more of the following steps: window
•header • Splitting the data into groups based on some criteria. Binary Operator functions
Computation/descriptive stats
•names • Applying a function to each group independently. Reindexing/selection/label
•sep
•encoding
• Combining the results into a data structure. manipulation
Missing Data handling
•converters Reshaping, sorting, transposing
Getting information Out of these, the split step is the most straightforward. In fact, in many situations we may wish to Combining/comparing/joining/m
erging
•index: Series and DataFrame index split the data set into groups and do something with those groups. In the apply step, we might Time Series related
•columns: DataFrame column wish to do one of the following: Plotting
•shape: Series and DataFrame dimensions Serialization/IO/Conversion
•info(): DataFrame informations Pandas Series
•values: Index and Series values 1. Aggregation: compute a summary statistic (or statistics) for each group. Some examples: Attributes
•unique(): Series unique values • Compute group sums or means. Conversion
Indexing, Iteration
•nunique(): Series number of unique values • Compute group sizes / counts. Function application, GroupBy &
Selecting data 2. Transformation: perform some group-specific computations and return a like-indexed object. window
•head() and tail() Binary Operator functions
•Column accessing
Some examples:
Computation/descriptive stats
•Row accessing • Standardize data (zscore) within a group. Reindexing/selection/label
•Fancy indexing • Filling NAs within groups with a value derived from each group. manipulation
Missing Data handling
•Logical masking 3. Filtration: discard some groups, according to a group-wise computation that evaluates True Reshaping, sorting
Indexing and merging data or False. Some examples: Combining/comparing/joining/m
•*set_index()* and *reset_index()* erging
•*append()* and *concat()*
• Discard data that belongs to groups with only a few members. Time Series related
Computing over data • Filter out data based on the group sum or mean. Accessors
•sum() Datetime properties
Datetime methods
•[Link](), [Link]() and [Link]() Some combination of the above: GroupBy will examine the results of the apply step and try to Period properties
•*apply()* and lambda return a sensibly combined result if it doesn’t fit into either of the above two categories. Timedelta properties
•isin() Timedelta methods
•div() String handling
Organizing data Categorical Accessor
•transpose() or T Plotting
Index Objects
•*sort_values()*
Numpy Arrays
•*groupby()* Attributes
•*pivot_table()* and *crosstab()* Array Creation Routines
Displaying data Shape Manipulation - shape,
•*plot()* resize, transpose
Calculation
Check Link – PyParis 2017 Item Selection and Manipulation
Time Series/Date Functionality Array Conversions
Group by: split-apply-combine
Windowing 208 DT Ch ChCh Ch Ch Ch Ch
Pandas DataFrame
Data representation
•Series: 1D Community tutorials (listed in Pandas official documentation) Attributes
Indexing, Iteration
•DataFrame: 2D Function application, GroupBy &
Loading data with *read_csv()* This is a guide to many pandas tutorials by the community, geared mainly for new users. window
•header Binary Operator functions
Computation/descriptive stats
•names
•sep
pandas cookbook by Julia Evans Reindexing/selection/label

•encoding The goal of this 2015 cookbook (by Julia Evans) is to give you some concrete examples for getting manipulation
Missing Data handling
•converters started with pandas. These are examples with real-world data, and all the bugs and weirdness that Reshaping, sorting, transposing
Getting information entails. For the table of contents, see the pandas-cookbook GitHub repository. Combining/comparing/joining/m
•index: Series and DataFrame index erging
Time Series related
•columns: DataFrame column
•shape: Series and DataFrame dimensions
Learn pandas by Hernan Rojas Plotting
Serialization/IO/Conversion
•info(): DataFrame informations A set of lesson for new pandas users: [Link]
Pandas Series
•values: Index and Series values Attributes
•unique(): Series unique values Practical data analysis with Python Conversion
Indexing, Iteration
•nunique(): Series number of unique values This guide is an introduction to the data analysis process using the Python data ecosystem and an Function application, GroupBy &
Selecting data
•head() and tail()
interesting open dataset. There are four sections covering selected topics as munging window
Binary Operator functions
•Column accessing data, aggregating data, visualizing data and time series. Computation/descriptive stats
•Row accessing Reindexing/selection/label
manipulation
•Fancy indexing Exercises for new users Missing Data handling
•Logical masking Practice your skills with real data sets and exercises. For more resources, please visit the Reshaping, sorting
Indexing and merging data
•*set_index()* and *reset_index()*
main repository. Combining/comparing/joining/m
erging
•*append()* and *concat()* Time Series related
Computing over data Modern pandas Accessors
Datetime properties
•sum() Tutorial series written in 2016 by Tom Augspurger. The source may be found in the GitHub Datetime methods
•[Link](), [Link]() and [Link]() repository TomAugspurger/effective-pandas. Period properties
•*apply()* and lambda
•isin()
• Modern Pandas Timedelta properties
Timedelta methods
•div() • Method Chaining String handling
Organizing data • Indexes Categorical Accessor
Plotting
•transpose() or T • Performance
Index Objects
•*sort_values()* • Tidy Data Numpy Arrays
•*groupby()*
•*pivot_table()* and *crosstab()*
• Visualization Attributes
Array Creation Routines
Displaying data • Timeseries Shape Manipulation - shape,
•*plot()* resize, transpose
Calculation
Check Link – PyParis 2017 Item Selection and Manipulation
Time Series/Date Functionality Array Conversions
Group by: split-apply-combine
Windowing 209 DT Ch ChCh Ch Ch Ch Ch
Pandas DataFrame
Data representation
•Series: 1D Community tutorials (listed in Pandas official documentation) Attributes
Indexing, Iteration
•DataFrame: 2D Function application, GroupBy &
Loading data with *read_csv()* Excel charts with pandas, vincent and xlsxwriter window
Binary Operator functions
•header • Using Pandas and XlsxWriter to create Excel charts Computation/descriptive stats
•names
Reindexing/selection/label
•sep
•encoding Video tutorials manipulation
Missing Data handling
•converters • Pandas From The Ground Up (2015) (2:24) GitHub repo Reshaping, sorting, transposing
Getting information • Introduction Into Pandas (2016) (1:28) GitHub repo Combining/comparing/joining/m
•index: Series and DataFrame index erging
• Pandas: .head() to .tail() (2016) (1:26) GitHub repo Time Series related
•columns: DataFrame column
•shape: Series and DataFrame dimensions
• Data analysis in Python with pandas (2016-2018) GitHub repo and Jupyter Notebook Plotting
Serialization/IO/Conversion
•info(): DataFrame informations • Best practices with pandas (2018) GitHub repo and Jupyter Notebook
Pandas Series
•values: Index and Series values Attributes
•unique(): Series unique values Various tutorials Conversion
Indexing, Iteration
•nunique(): Series number of unique values • Wes McKinney’s (pandas BDFL) blog Function application, GroupBy &
Selecting data
•head() and tail()
• Statistical analysis made easy in Python with SciPy and pandas DataFrames, by Randal Olson window
Binary Operator functions
•Column accessing • Statistical Data Analysis in Python, tutorial videos, by Christopher Fonnesbeck from SciPy 2013 Computation/descriptive stats
•Row accessing • Financial analysis in Python, by Thomas Wiecki Reindexing/selection/label
manipulation
•Fancy indexing • Intro to pandas data structures, by Greg Reda Missing Data handling
•Logical masking • Pandas and Python: Top 10, by Manish Amde Reshaping, sorting
Indexing and merging data
•*set_index()* and *reset_index()*
• Pandas DataFrames Tutorial, by Karlijn Willems Combining/comparing/joining/m
erging
•*append()* and *concat()* • A concise tutorial with real life examples Time Series related
Computing over data Accessors
•sum() Datetime properties
Datetime methods
•[Link](), [Link]() and [Link]()
Period properties
•*apply()* and lambda Timedelta properties
•isin() Timedelta methods
•div() String handling
Organizing data Categorical Accessor
•transpose() or T Plotting
Index Objects
•*sort_values()*
Numpy Arrays
•*groupby()* Attributes
•*pivot_table()* and *crosstab()* Array Creation Routines
Displaying data Shape Manipulation - shape,
•*plot()* resize, transpose
Calculation
Check Link – PyParis 2017 Item Selection and Manipulation
Time Series/Date Functionality Array Conversions
Group by: split-apply-combine
Windowing 210 DT Ch ChCh Ch Ch Ch Ch
PSEUDO
RANDOM NUMBER
GENERATION
(IN PYTHON, NUMPY)

211 DT Ch ChCh Ch Ch Ch Ch
Pseudo Random Number Generation in Python
§ Python Stdlib Random Module Seed Initialize State Update State Update

§ [Link] module
§ Legacy RandomState and
Next Next
§ Generators

Library Module Class Generator Type Random number Random number


Python Standard random [Link](seed) Mersenne Twister Generator
Library MT19937
Pseudo-Random Number Generator (PRNG) generates a sequence of random numbers based on a
Numpy random [Link] Mersenne Twister Generator
seed. Using the same seed makes the PRNG produce the same sequence of random numbers.
e(seed) MT19937
*Source : Real-World Cryptography Version 12 by David Wong
Numpy random [Link](bi Permuted Congruential
t_generator) Generator PCG64 Link
Evaluation Metrics #Setting the seed of the random number generator
Note : PRGN is a vast topic with wide applications in simulations (MonteCarlo), game PRNG >>>[Link](123456)
development, cryptography etc. We shall be covering a small part of it from the point #Sequence of numbers generated starting from the seed
of view of generating random numbers as and when required for simple use cases. Statistical Quality position
Prediction Difficulty >>>[Link](10,50,5)
Reproducible Results array([11, 37, 42, 33, 46])
Seeding for Initializing the Pseudo Random Number Generator Multiple Streams
Period #Note that internally the state of the generator has changed
What is normally called a random number sequence in reality is a "pseudo- Useful Features and a new seed is set for the next function call
random" number sequence because the values are computed using a Time Perfomance >>>[Link](10,50,5)
deterministic algorithm and probability plays no real role. Space Usage #Output shall be based on deterministically created new seed
Code Size & internally
The "seed" is a starting point for the sequence and the guarantee is that if Complexity array([18, 20, 20, 22, 21])
you start from the same seed you will get the same sequence of numbers. k-Dimensional
This is very useful for example for debugging (when you are looking for an Equidistribution #Resetting the seed will produce the same sequence of
random numbers
error in a program you need to be able to reproduce the problem and study
>>>[Link](123456)
it, a non-deterministic program would be much harder to debug because
>>>[Link](10,50,5)
every run would be different). Link
array([11, 37, 42, 33, 46])

212 DT Ch ChCh Ch Ch Ch Ch
Seeding for Initializing the Pseudo Random Number Generator

Python Random import random [Link](a, version) in python is used to initialize the pseudo-random number
Module [Link](10)
generator (PRNG).
[Link] import numpy as np
module [Link](10) #Seed is changed at global level PRNG is algorithm that generates sequence of numbers approximating the properties of
Legacy RandomState rs = [Link](12345) #12345 is the seed random numbers. These random numbers can be reproduced using the seed value. So, if you
Generators rng = [Link].default_rng(12345) #12345 is the seed provide seed value, PRNG starts from an arbitrary starting state using a seed.

What does 'seeding' mean? Argument a is the seed value. If the a value is None, then by default, current system time is
used. and version is An integer specifying how to convert the a parameter into a integer.
It means: pick a place to start. Default value is 2.
Think of a pseudo random number generator as just a really long list of numbers. if you want the same random number to be reproduced, provide the seed. Specifically, the
This list is circular, it eventually repeats. same seed. Link
To use it, you need to pick a starting place. This is called a "seed".

Computers are deterministic


When you call [Link](), it sets the random seed. The sequence of numbers you
The computer random number generator is really a ``pseudo-random" number generate from that point forwards will always be the same.
generator, because computers are deterministic. If you start from the same point,
you get the same sequence! Most random number generates have a method for The thing is, you're only set the seed once, and then you're calling [Link]() three
choosing the starting point, e.g., the clock time (perhaps in milliseconds), so you times. That means you're getting the next three numbers from your [Link](). Of course
get a different sequence each time you call the random number generator. they're different – you haven't reset the seed in between. But every time you run the
program, you'll get the same sequence of three numbers, because you set the seed to the
Although this sounds good, it is actually recommended that you set the seed of same thing before generating them all.
the random number generator yourself, as you may actually want repeatibility: as
you modify your code and assess improvements, you don't want to have to worry Setting the seed only affects the next random number to be generated, because of how
about whether the improvements (or lack of them) comes from a different set of pseudo-random number generation (which [Link] uses) works: it uses the seed to
inputs or from the different code. Of course, you also probably want to check generate a new random number deterministically, and then uses the generated number to
your code with a variety of ``random" sequences. set a new seed for the next number. It effectively boils down to getting a really really
long sequence of random numbers that will, eventually, repeat itself. When you set the seed,
Lowest level random number generators give uniform deviates, i.e., equal you're jumping to a specified point in that sequence – you're not keeping the code there,
probability of results in some range (usually 0 to 1 for floats). E.g., python though.
[Link], [Link], [Link] , [Link],
general random number class [Link] Link Link

213 DT Ch ChCh Ch Ch Ch Ch
Random Module standard Library in Python
Functions supplied by the Random Module [Link](a=None, version=2)
Bookkeeping functions Initialize the random number generator. If a is omitted or None, the
seed Initialize the random number generator current system time is used. [..]
getstate Return an object capturing the current internal state of the generator.
Alternative Generator
setstate setstate() restores the internal state of the generator to what it was at the
class [Link]([seed])Class that implements the default pseudo-
time getstate() was called.
random number generator used by the random module.
Functions for integers
randrange Return a randomly selected element from range(start, stop, step).
Note : The functions supplied by Random module are actually bound
randint Return a random integer N such that a <= N <= b
methods of a hidden instance of the [Link] class. You can
Functions for sequences
instantiate your own instances of Random to get generators that don’t
choice Return a random element from the non-empty sequence seq.
share state.
choices Return a k sized list of elements chosen from the population with
replacement.
shuffle Shuffle the sequence x in place. Python uses the Mersenne Twister as the core generator.
sample Return a k length list of unique elements chosen from the population PRGN by Mersenne Twister Generator
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). >>>import random
uniform Return a random floating point number N such that a <= N <= b for a <= >>>#[Link](123456)
b and b <= N <= a for b < a. >>> rints = [Link](low=0, high=10, size=(4,4))
betavariate Beta distribution. >>> rchoice = [Link](‘abcdefgh’)
expovariate Exponential distribution.
gammavariate Gamma distribution. Also, check discussion thread on generating random integers.
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.

214 DT Ch ChCh Ch Ch Ch Ch
PRNG in [Link] Module
using combinations of a BitGenerator to create sequences and a Generator to use those sequences to # Construct a new Generator
sample from different statistical distributions import numpy as np
>>> rng = [Link].default_rng(123456)
>>> rints = [Link](low=0, high=10, size=(4,4))
The BitGenerator has a limited set of >>> rints
responsibilities. It manages state and provides array([[0, 6, 8, 3], [3, 0, 0, 9], [2, 9, 3, 4], [5, 2, 4, 6]])
functions to produce random doubles and
random unsigned 32- and 64-bit values. >>> rng = [Link].default_rng(123456)
>>> [Link](4)
array([0.63651375, 0.38481166, 0.04744542,
0.95525274])
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 >>> rng = [Link].default_rng(123456)
alternative bit generators to be used with little code >>> rng.standard_normal(10)
duplication. array([ 0.1928212 , -0.06550702, 0.43550665,
0.88235875, 0.37132785, 1.15998882, 0.37835254, -
0.11718594, 2.20800921, 1.95324484])
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: PRNG by Mersenne Twister Generator
PRNG by PCG

215 DT Ch ChCh Ch Ch Ch Ch
Random sampling ([Link]) Distributions

Using RandomState Class (Legacy Random Generation) beta(a, b[, size]) Draw samples from a Beta distribution.
Seeding and State binomial(n, p[, size]) Draw samples from a binomial distribution.
get_state() Return a tuple representing the internal state of the chisquare(df[, size]) Draw samples from a chi-square distribution.
generator. dirichlet(alpha[, size]) Draw samples from the Dirichlet distribution.
set_state(state) Set the internal state of the generator from a tuple. exponential([scale, size]) Draw samples from an exponential distribution.
seed(self[, seed]) Reseed a legacy MT19937 BitGenerator f(dfnum, dfden[, size]) Draw samples from an F distribution.
gamma(shape[, scale, size]) Draw samples from a Gamma distribution.
Simple random data geometric(p[, size]) Draw samples from the geometric distribution.
rand(d0, d1, …, dn) Random values in a given shape. gumbel([loc, scale, size]) Draw samples from a Gumbel distribution.
randn(d0, d1, …, dn) Return a sample (or samples) from the “standard normal” hypergeometric(ngood, nbad, nsample[, s Draw samples from a Hypergeometric distribution.
distribution. ize])
randint(low[, high, size, dtype]) Return random integers from low (inclusive) laplace([loc, scale, size]) Draw samples from the Laplace or double exponential distribution with
specified location (or mean) and scale (decay).
to high (exclusive).
logistic([loc, scale, size]) Draw samples from a logistic distribution.
random_integers(low[, high, size]) Random integers of type np.int_ between low and high,
lognormal([mean, sigma, size]) Draw samples from a log-normal distribution.
inclusive.
logseries(p[, size]) Draw samples from a logarithmic series distribution.
random_sample([size]) Return random floats in the half-open interval [0.0, 1.0).
multinomial(n, pvals[, size]) Draw samples from a multinomial distribution.
choice(a[, size, replace, p]) Generates a random sample from a given 1-D array
multivariate_normal(mean, cov[, size, …]) Draw random samples from a multivariate normal distribution.
bytes(length) Return random bytes.
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.
Permutations
noncentral_f(dfnum, dfden, nonc[, size]) Draw samples from the noncentral F distribution.
shuffle(x) Modify a sequence in-place by shuffling its contents.
normal([loc, scale, size]) Draw random samples from a normal (Gaussian) distribution.
permutation(x) Randomly permute a sequence, or return a permuted
pareto(a[, size]) Draw samples from a Pareto II or Lomax distribution with specified shape.
range.
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.
• RandomState is a class in NumPy. We use an instance of this class to manage rayleigh([scale, size]) Draw samples from a Rayleigh distribution.
random number generation. standard_cauchy([size]) Draw samples from a standard Cauchy distribution with mode = 0.
• Random numbers are generated by methods in the class (e.g. the rand or randn standard_exponential([size]) Draw samples from the standard exponential distribution.
methods). standard_gamma(shape[, size]) Draw samples from a standard Gamma distribution.
• Each instance of RandomState comes with its own specific random number standard_normal([size]) Draw samples from a standard Normal distribution (mean=0, stdev=1).
stream. standard_t(df[, size]) Draw samples from a standard Student’s t distribution with df degrees of
• The random number stream is initialized (“seeded”) when you create a freedom.
triangular(left, mode, right[, size]) Draw samples from the triangular distribution over the interval [left, right].
RandomState instance.
uniform([low, high, size]) Draw samples from a uniform distribution.
• You can reset the “seed” using the seed() method. vonmises(mu, kappa[, size]) Draw samples from a von Mises distribution.
• We can use the RandomState objects to have different random streams or to wald(mean, scale[, size]) Draw samples from a Wald, or inverse Gaussian, distribution.
reset a stream. weibull(a[, size]) Draw samples from a Weibull distribution.
zipf(a[, size]) Draw samples from a Zipf distribution.

216 DT Ch ChCh Ch Ch Ch Ch
Distributions - Methods
Use [Link].default_rng for PRNG instead of the
beta(a, b[, size]) Draw samples from a Beta distribution.
legacy RandomState binomial(n, p[, size]) Draw samples from a binomial distribution.
chisquare(df[, size]) Draw samples from a chi-square distribution.
[Link].default_rng() : Construct a new Generator with the default BitGenerator dirichlet(alpha[, size]) Draw samples from the Dirichlet distribution.
(PCG64). exponential([scale, size]) Draw samples from an exponential distribution.
f(dfnum, dfden[, size]) Draw samples from an F distribution.
import numpy as np gamma(shape[, scale, size]) Draw samples from a Gamma distribution.
#Using [Link].default_rng to instantiate a Generator with numpy’s geometric(p[, size]) Draw samples from the geometric distribution.
default BitGenerator gumbel([loc, scale, size]) Draw samples from a Gumbel distribution.
# We can specify a seed for reproducible results hypergeometric(ngood, nbad, nsample[, size]) Draw samples from a Hypergeometric distribution.
>>> rng = [Link].default_rng(12345) laplace([loc, scale, size]) Draw samples from the Laplace or double exponential distribution with
specified location (or mean) and scale (decay).
>>> rints = [Link](low=0, high=10, size=3)
logistic([loc, scale, size]) Draw samples from a logistic distribution.
>>> rints lognormal([mean, sigma, size]) Draw samples from a log-normal distribution.
array([6, 2, 7]) logseries(p[, size]) Draw samples from a logarithmic series distribution.
multinomial(n, pvals[, size]) Draw samples from a multinomial distribution.
>>> type(rints[0]) multivariate_hypergeometric(colors, nsample) Generate variates from a multivariate hypergeometric distribution.
<class 'numpy.int64'> 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.
Instead of reseeding, instantiate a new Generator object using [Link].default_rng 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.
Why So ? normal([loc, scale, size]) Draw random samples from a normal (Gaussian) distribution.
Avoid the use of [Link](seed) for fixing reproducibility as this operates pareto(a[, size]) Draw samples from a Pareto II or Lomax distribution with specified
at the global state level. For multiple threads and projects with imports and scripts, shape.
poisson([lam, size]) Draw samples from a Poisson distribution.
the results will get obscured in case of multiple declarations of the power(a[, size]) Draws samples in [0, 1] from a power distribution with positive
[Link](seed) with different seed arguments. exponent a - 1.
rayleigh([scale, size]) Draw samples from a Rayleigh distribution.
Accessing the Bit Generator - Attribute 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.
bit_generator Gets the bit generator instance used by the generator
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).
Simple random data - Methods
standard_t(df[, size]) Draw samples from a standard Student’s t distribution with df degrees
integers Return random integers from low (inclusive) to high(exclusive), or if of freedom.
endpoint=True, low (inclusive) to high(inclusive). triangular(left, mode, right[, size]) Draw samples from the triangular distribution over the
random Return random floats in the half-open interval [0.0, 1.0). interval [left, right].
choice Generates a random sample from a given array uniform([low, high, size]) Draw samples from a uniform distribution.
bytes Return random bytes. vonmises(mu, kappa[, size]) Draw samples from a von Mises distribution.
wald(mean, scale[, size]) Draw samples from a Wald, or inverse Gaussian, distribution.
Permutations - Methods weibull(a[, size]) Draw samples from a Weibull distribution.
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.

217 DT Ch ChCh Ch Ch Ch Ch
Reseeding a BitGenerator and Recreating a Bit Generator Prior to every function/method call
import numpy as np
from [Link] import MT19937
from [Link] import RandomState, SeedSequence
import [Link] as plt

seed=123456789

# Reseed a BitGenerator
[Link](seed)
r1 = [Link](1, 6, 1000)
# Reseed a BitGenerator
[Link](seed)
r2 = [Link](1, 6, 1000)

# Recreate a BitGenerator
rs = RandomState(MT19937(SeedSequence(seed)))
c1 = [Link](1, 6, 1000)
# Recreate a BitGenerator
rs = RandomState(MT19937(SeedSequence(seed)))
c2 = [Link](1, 6, 1000)

# Visualise results
fig, axes = [Link](1, 2)
axes[0].hist(r1, 11, density=True)
axes[0].hist(r2, 11, density=True)
axes[0].set_title('Reseed a BitGenerator')

axes[1].hist(c1, 11, density=True) Note : We are reseeding/recreating the BitGenerator everytime we are using the
axes[1].hist(c2, 11, density=True) randint method from [Link] or RandomState Class. Hence, we are getting the
axes[1].set_title('Recreate a BitGenerator') same sequence of numbers. Thus, resulting in each of the subplot above having two
set of overlapping bar plots of same height.
[Link]() Link

218 DT Ch ChCh Ch Ch Ch Ch
Reseeding a BitGenerator and Recreating a Bit Generator Only once in the script
import numpy as np
from [Link] import MT19937
from [Link] import RandomState, SeedSequence
import [Link] as plt

seed=123456789

# Reseed a BitGenerator
[Link](seed)
r1 = [Link](1, 6, 1000)
r2 = [Link](1, 6, 1000)

# Recreate a BitGenerator
rs = RandomState(MT19937(SeedSequence(seed)))
c1 = [Link](1, 6, 1000)
c2 = [Link](1, 6, 1000)

# Visualise results
fig, axes = [Link](1, 2)
axes[0].hist(r1, 11, density=True)
axes[0].hist(r2, 11, density=True)
axes[0].set_title('Reseed a BitGenerator')

axes[1].hist(c1, 11, density=True)


axes[1].hist(c2, 11, density=True)
axes[1].set_title('Recreate a BitGenerator')

[Link]() Link Note : We are reseeding/recreating the BitGenerator only once. After that we are
making two successive randint calls from [Link] or RandomState Class. The two
calls in succession will result in two different sequence of integers. Thus, resulting in
each of the subplot above having two set of overalpping bar plots with different
heights. The blue color indicates the height difference.

219 DT Ch ChCh Ch Ch Ch Ch
[Link] functions (Legacy for backward compatibility) Avoid [Link] functions and instead use default_rng

import numpy as np
import [Link]

[Link]()
fig, axes = [Link](1, 3, figsize=(12, 4))

axes[0].hist([Link](10000))
axes[0].set_title("rand")

axes[1].hist([Link](10000))
axes[1].set_title("randn")

axes[2].hist([Link](low=1, high=10, size=10000), bins=9,


align='left')
axes[2].set_title("randint(low=1, high=10)")

[Link]('[Link]', dpi = 300,


transparent = True)
[Link](

220 DT Ch ChCh Ch Ch Ch Ch
[Link].default_rng to generate random numbers Avoid [Link] functions and instead use default_rng

import numpy as np
import [Link]

[Link]()
fig, axes = [Link](1, 3, figsize=(12, 4))

#Instantiating a generator
rng = [Link].default_rng(12345)

axes[0].hist([Link](10000))
axes[0].set_title("random")

axes[1].hist(rng.standard_normal(10000))
axes[1].set_title("standard_normal")

axes[2].hist([Link](low=1, high=10, size=10000), bins=9,


align='left')
axes[2].set_title("integers(low=1, high=10)")

[Link]('[Link]', dpi = 300, transparent = True)


[Link]()

221 DT Ch ChCh Ch Ch Ch Ch
HANDLING DATE AND TIME

222 DT Ch ChCh Ch Ch Ch Ch
Classes/Objects to handle date and time Python, Numpy, Pandas, Matplotlib

Matplotlib date

Pandas timestamp timedelta period dateoffset datetimeindex PeriodIndex timedeltaindex

NumPy's datetime64 object allows


you to set its precision from hours
Numpy datetime64 timedelta64 all the way to attoseconds (10 ^ -
18).

The datetime module's datetime


object has microsecond precision
(one-millionth of a second).
Python datetime timedelta date time tzinfo timezone

A Specific Date Absolute time Note : timedelta only holds


days, seconds, microseconds
and Time duration internally

With TimeZone support


223 DT Ch ChCh Ch Ch Ch Ch
Conversion Matrix between Go to
Across Pandas, numpy, Python, matplotlib, strings
datetime, Timestamp and datetime64 [Link]

Conversion Python Numpy Pandas matplotlib


Matrix
To string datetime datetime64 timestamp [Link]
From
Python string >>>[Link](strobj) np.datetime64('2005-02-25') [Link]('2017-01- datestr2num(strobj)
01T12’)
strobj: string object #strings in ISO 8601 date or #datetime like string
datetime format.
Python datetime [Link]('%b %d, np.datetime64(dtobj) [Link](dtobj) # or date2num(dtobj)
%Y’) pd.to_datetime(dtobj)
[Link]('The date
import datetime as dt is %b %d, %Y')
dtobj : datetime object

Numpy datetime64 np.datetime_as_string(d, >>> dt64 = np.datetime64('2017-10-24 05:34:20.123456') [Link](dt64)


unit='h’) >>> unix_epoch = np.datetime64(0, 's')
>>> one_second = np.timedelta64(1, 's')
>>> seconds_since_epoch = (dt64 - unix_epoch) /
import numpy as np #Check datetime support one_second
dt64 : datetime64 object functions >>> seconds_since_epoch
1508823260.123456
>>>
[Link](seconds_since_epoch)
[Link](2017, 10, 24, 5, 34, 20, 123456)

Pandas timestamp [Link]('%b %d ts.to_pydatetime() ts.to_datetime64() # or date2num(ts.to_pydatetime())


%Y’) ts.to_numpy() # or
import pandas as pd #dt is datetime accessor. ts.asm8
Link
ts : timestamp object
matplotlib [Link]* num2date(mdate)

*Currently Matplotlib time is only converted back to datetime objects, which have microsecond resolution, and years that only span 0000 to 9999. Check Link.
[Link]
[Link]
224 DT Ch ChCh Ch Ch Ch Ch
Conversion Matrix between Unix time vs Go to
Across Pandas, numpy, Python, matplotlib, strings [Link]
datetime, Timestamp and datetime64
Conversion Platform Independent Conversion Matrix Platform Independent
Matrix
To Unix epoch or Unix time or POSIX From Unix epoch or Unix time or POSIX
From time or Unix timestamp To time or Unix timestamp
Python string >>>[Link](string). timestamp() string >>> import datetime
>>> [Link](2021,8,1,0,0).timestamp() >>> timestamp = [Link](1600000000)
strobj: string object
1627756200.0 >>> print([Link]('%Y-%m-%d %H:%M:%S’))
2020-09-13 17:56:40
Python datetime [Link]() datetime >>> [Link](1172969203.1)
[Link](2007, 3, 4, 6, 16, 43, 100000)
import datetime as dt Check link
dtobj : datetime
object
Numpy datetime64 >>> dt64 = np.datetime64('2017-10-24 05:34:20.123456') datetime64 >>> dt64 = np.datetime64('2017-10-24 05:34:20.123456')
>>> unix_epoch = np.datetime64(0, 's') >>> unix_epoch = np.datetime64(0, 's')
>>> one_second = np.timedelta64(1, 's') >>> one_second = np.timedelta64(1, 's')
import numpy as np >>> seconds_since_epoch = (dt64 - unix_epoch) / one_second >>> seconds_since_epoch = (dt64 - unix_epoch) / one_second
dt64 : datetime64 >>> seconds_since_epoch >>> seconds_since_epoch
object 1508823260.123456 1508823260.123456
>>> [Link](seconds_since_epoch) >>>
[Link](2017, 10, 24, 5, 34, 20, 123456) [Link](seconds_since_epoch)
[Link](2017, 10, 24, 5, 34, 20, 123456)
Pandas timestamp In [64]: stamps = pd.date_range("2012-10-08 18:15:05", timestamp >>> pd.to_datetime([1627756200.0, 1600000000], unit = 's’)
periods=4, freq="D") DatetimeIndex(['2021-07-31 18:30:00', '2020-09-13 12:26:40'],
import pandas as pd In [66]: (stamps - [Link]("1970-01-01")) // dtype='datetime64[ns]', freq=None)
ts : timestamp [Link]("1s") Link
object Int64Index([1349720105, 1349806505, 1349892905, 1349979305], dtype='int64')
matplotlib [Link] [Link].num2epoch(d)
[source]
[Link] [Link].epoch2num(unixtime)
[source]

[Link]
[Link]
225 DT Ch ChCh Ch Ch Ch Ch
Internal Storage of Datetime objects in Python [Link]
internally

/* Fields are packed into successive bytes, each viewed as unsigned and /* All datetime objects are of PyDateTime_DateTimeType, but that can be
* big-endian, unless otherwise noted: * allocated in two ways too, just like for time objects above. In addition,
* * the plain date type is a base class for datetime, so it must also have
* byte offset * a hastzinfo member (although it's unused there).
* 0 year 2 bytes, 1-9999 */
* 2 month 1 byte, 1-12 ...
* 3 day 1 byte, 1-31 #define _PyDateTime_DATETIMEHEAD \
* 4 hour 1 byte, 0-23 _PyTZINFO_HEAD \
* 5 minute 1 byte, 0-59 unsigned char data[_PyDateTime_DATETIME_DATASIZE];
* 6 second 1 byte, 0-59
* 7 usecond 3 bytes, 0-999999 typedef struct
* 10 {
*/ _PyDateTime_DATETIMEHEAD
... } _PyDateTime_BaseDateTime; /* hastzinfo false */

/* # of bytes for year, month, day, hour, minute, second, and usecond. */ typedef struct
#define _PyDateTime_DATETIME_DATASIZE 10 {
... _PyDateTime_DATETIMEHEAD
unsigned char fold;
/* The datetime and time types have hashcodes, and an optional tzinfo member, PyObject *tzinfo;
* present if and only if hastzinfo is true. } PyDateTime_DateTime; /* hastzinfo true */
*/ The 48-byte count
#define _PyTZINFO_HEAD \
PyObject_HEAD \ The 48-byte count breaks down as follows:
Py_hash_t hashcode; \ • 8-byte refcount
char hastzinfo; /* boolean flag */ • 8-byte type pointer
... • 8-byte cached hash
• 1-byte "hastzinfo" flag
• 7-byte padding
• 10-byte manually packed char[10] containing datetime data
• 6-byte padding

226 DT Ch ChCh Ch Ch Ch Ch
Handling Datetime objects and strings [Link] Using Pandas for String conversion to datetime object : link
time-and-strptime-behavior Check answer by alexander, Also check dt accessor methods
Strings to datetime >>>import pandas as pd
strftime() and strptime() Format Codes >>>dates = ['2015-12-25', '2015-12-26’]
module
methods # 1) Use a list comprehension.
Code Meaning >>> [[Link]() for d in pd.to_datetime(dates)]
strptime [[Link](2015, 12, 25), [Link](2015, 12, 26)]
[Link] %a Weekday as Sun, Mon
strftime Weekday as full name as Sunday, # 2) Convert the dates to a DatetimeIndex and extract the
%A
class Monday python dates.
>>> [Link](dates).[Link]()
%w Weekday as decimal no as 0,1,2... [[Link](2015, 12, 25), [Link](2015, 12, 26)]
strptime means string parser, this will convert a string
format to datetime. %d Day of month as 01,02 dateutil capable of parsing most human intelligible date
strftime means string formatter, this will format a %b Months as Jan, Feb representations.
datetime object to string format. [Link] to format a date/time from known formats.
%B Months as January, February § Functions :
String to datetime object = strptime %m Months as 01,02 [Link](parserinfo=None, **kwargs)
datetime object to other formats = strftime classmethod [Link](dt_str) # full ISO-8601 format
%y Year without century as 11,12,13 parser
Jun 1 2021 1:33PM %Y Year with century 2011,2012 § Classes :
is equals to class [Link](dayfirst=False, yearfirst=False)
%b %d %Y %I:%M%p %H 24 Hours clock from 00 to 23
%I 12 Hours clock from 01 to 12 ISO 8601 Date Format : YYYY-MM-DD
• %b :Month as locale’s abbreviated name(Jun)
• %d :Day of the month as a zero-padded decimal %p AM, PM • YYYY is the year [all the digits, i.e. 2012]
number(1) %M Minutes from 00 to 59 • MM is the month [01 (January) to 12(December)]
• %Y :Year with century as a decimal number(2015) • DD is the day [01 to 31]
• %I :Hour (12-hour clock) as a zero-padded decimal %S Seconds from 00 to 59
number(01) [Link]
%f Microseconds 6 decimal numbers
• %M :Minute as a zero-padded decimal number(33) [Link]
• %p :Locale’s equivalent of either AM or PM(PM) Date Format
Check
[Link] [Link](*args, standards RFC3339 Format
string-into-datetime/470303#470303 **kwargs)
SO Thread [Link]

227 DT Ch ChCh Ch Ch Ch Ch
Handling Datetime objects and strings
datetime to strings Pandas timestamp/datetimeindex to strings
date and datetime objects (and time as well) support a mini-
language to specify output, and there are three ways to access it:
Importance of [Link]
• direct method call: [Link]('format here')
• format method (python 2.6+): '{:format dt is the datetime accessor. Whenever your column values are
here}'.format(dt) Timestamps or Timedeltas, Pandas makes the dt accessor
• f-strings (python 3.6+): f'{dt:format here}’ available. From that accessor, you can use many other datetime
specific methods. In this case, I used strftime.
So your example could look like:
• [Link]('The date is %b %d, %Y') Go to Slide
• 'The date is {:%b %d, %Y}'.format(dt) Pandas Documentation link
• f'The date is {dt:%b %d, %Y}’

In all three cases the output is:


The date is Feb 23, 2012

For completeness' sake: you can also directly access the


attributes of the object, but then you only get the numbers:

'The date is %s/%s/%s' % ([Link], [Link], [Link])


# The date is 02/23/2012
The time taken to learn the mini-language is worth it.

#Link :Check answer by Ethan Furman

[Link]
into-datetime/470303#470303

228 DT Ch ChCh Ch Ch Ch Ch
Numpy captures 2 general
time related concepts:

datetime64 : datetime object represents a Ø NumPy has no separate date and time objects, just a single datetime64
object to represent a single moment in time.
single moment in time
Ø 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).
Timedelta64 : An absolute time duration. Ø It's constructor is more flexible and can take a variety of inputs.

Datetime Units
What is epoch time?
The Datetime and Timedelta data types support a large number of time
The Unix epoch (or Unix time or POSIX time or Unix units, as well as generic units which can be coerced into any of the other
timestamp) is the number of seconds that have elapsed units based on input data.
since January 1, 1970 (midnight UTC/GMT), not counting
leap seconds (in ISO 8601: 1970-01-01T00:00:00Z). Datetimes are always stored based on POSIX time (though having a TAI
Literally speaking the epoch is Unix time 0 (midnight
mode which allows for accounting of leap-seconds is proposed), with
1/1/1970), but 'epoch' is often used as a synonym for
Unix time.
an epoch of 1970-01-01T00:00Z. This means the supported dates are
always a symmetric interval around the epoch, called “time span”.(check
official link)
Assuming you are starting with some ISO 8601
strings in your np.datetime64[ns], you can The date units are : Y, M, W, D
use dt.tz_localize to assign a time zone to them, The time units are : h, m, s, ms, us, ns, ps, fs, as
then dt.tz_convert to convert them into another
time zone
Comprehensive
Numpy Official Link
Tutorial SO Thread

229 DT Ch ChCh Ch Ch Ch Ch
Datetime Support Functions in Numpy

Datetime Support Functions

datetime_as_string(arr[, unit, timezone, …]) Convert an array of datetimes into an array of strings.
datetime_data(dtype, /) Get information about the step size of a date or time type.

Business Day Functions

busdaycalendar([weekmask, holidays]) A business day calendar object that efficiently stores information defining
valid days for the busday family of functions.
is_busday(dates[, weekmask, holidays, …]) Calculates which of the given dates are valid days, and which are not.
busday_offset(dates, offsets[, roll, …]) First adjusts the date to fall on a valid day according to the roll rule, then
applies offsets to the given dates counted in valid days.
busday_count(begindates, enddates[, …]) Counts the number of valid days between begindates and enddates, not
including the day of enddates.

# Using [Link] to generate ranges of dates. Check link


>>> [Link]('2005-02', '2005-03', dtype='datetime64[D]’)

array(['2005-02-01', '2005-02-02', '2005-02-03', '2005-02-04', '2005-02-05', '2005-02-06',


'2005-02-07', '2005-02-08', '2005-02-09', '2005-02-10', '2005-02-11', '2005-02-12', '2005-
02-13', '2005-02-14', '2005-02-15', '2005-02-16', '2005-02-17', '2005-02-18', '2005-02-19',
'2005-02-20', '2005-02-21', '2005-02-22', '2005-02-23', '2005-02-24', '2005-02-25', '2005-
02-26', '2005-02-27', '2005-02-28'], dtype='datetime64[D]')

230 DT Ch ChCh Ch Ch Ch Ch
pandas captures 4 general Concept Scalar Class Array Class pandas Data Type Primary Creation
time related concepts: Method
Date times Timestamp DatetimeIndex datetime64[ns] or to_datetime or
datetime64[ns, tz] date_range
Date times : A specific date and time with timezone
support. Time deltas Timedelta TimedeltaIndex timedelta64[ns] to_timedelta or
Similar to [Link] from the standard library. timedelta_range
Time spans Period PeriodIndex period[freq] Period or
Time deltas: An absolute time duration. period_range
Similar to [Link] from the standard library.
Date offsets DateOffset None None DateOffset

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. 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
Similar to [Link] from and PeriodIndex for sequences of spans
dateutil package.
import datetime
In [2]: dti = pd.to_datetime( ["1/1/2018", np.datetime64("2018-01-01"), [Link](2018, 1, 1)]
What is epoch time? In [3]: dti
Out[3]: DatetimeIndex(['2018-01-01', '2018-01-01', '2018-01-01'], dtype='datetime64[ns]', freq=None)
The Unix epoch (or Unix time or POSIX time or Unix timestamp) is
the number of seconds that have elapsed since January 1, 1970 In [21]: [Link](pd.period_range("1/1/2011", freq="M", periods=3))
(midnight UTC/GMT), not counting leap seconds (in ISO 8601: Out[21]:
1970-01-01T00:00:00Z). Literally speaking the epoch is Unix time 0 0 2011-01
(midnight 1/1/1970), but 'epoch' is often used as a synonym for 1 2011-02
Unix time. 2 2011-03 dtype: period[M]

Comprehensive
Pandas Official Link
Tutorial SO Thread

231 DT Ch ChCh Ch Ch Ch Ch
Pandas Date and time handling classes : Creation Methods
Concept Scalar Class Array Class pandas Data Type Primary Creation Method Frequency codes/ Offset Aliases
Date times Timestamp DatetimeIndex datetime64[ns] or datetime64[ns, tz] to_datetime or date_range Alias Description
B business day frequency
Time deltas Timedelta TimedeltaIndex timedelta64[ns] to_timedelta or timedelta_range C custom business day
Time spans Period PeriodIndex period[freq] Period or period_range frequency
D calendar day frequency
Date offsets DateOffset None None DateOffset W weekly frequency
M month end frequency
SM semi-month end
>>> df = [Link]({'year': [2015, 2016], 'month': [2, 3], 'day': [4, 5]}) >>> pd.date_range(start='1/1/2018', end='1/08/2018’) frequency (15th and
>>> pd.to_datetime(df) DatetimeIndex(['2018-01-01', '2018-01-02', '2018-01-03’, '2018-01-04', '2018-01-05’, end of month)
BM business month end
0 2015-02-04 '2018-01-06’, '2018-01-07', '2018-01-08’],dtype='datetime64[ns]’, frequency
1 2016-03-05 freq='D') CBM custom business month
dtype: datetime64[ns] end frequency
>>> pd.date_range(start='1/1/2018', periods=5, freq='M’) MS month start frequency
SMS semi-month start
>>> pd.to_datetime('13000101', format='%Y%m%d', errors='ignore’) DatetimeIndex(['2018-01-31', '2018-02-28', '2018-03-31’, '2018-04-30', '2018-05-31’], frequency (1st and 15th)
[Link](1300, 1, 1, 0, 0) dtype='datetime64[ns]’, freq='M') BMS business month start
>>> pd.to_datetime('13000101', format='%Y%m%d', errors='coerce’) frequency
NaT >>> pd.date_range(start='1/1/2018', periods=5, freq=[Link](3)) CBMS custom business month
DatetimeIndex(['2018-01-31', '2018-04-30', '2018-07-31’, '2018-10-31', '2019-01-31’], start frequency
Q quarter end frequency
>>> pd.to_timedelta([Link](5), unit='s’) dtype='datetime64[ns]’, freq='3M') BQ business quarter end
TimedeltaIndex(['0 days 00:00:00', '0 days 00:00:01', '0 days 00:00:02’, frequency
'0 days 00:00:03', '0 days 00:00:04'], dtype='timedelta64[ns]', freq=None) >>> pd.period_range(start='2017-01-01', end='2018-01-01', freq='M’) QS quarter start frequency
>>> pd.to_timedelta([Link](5), unit='d’) PeriodIndex(['2017-01', '2017-02', '2017-03', '2017-04', '2017-05’, '2017-06’, BQS business quarter start
TimedeltaIndex(['0 days', '1 days', '2 days', '3 days', '4 days'], '2017-07', '2017-08', '2017-09', '2017-10’, '2017-11', '2017-12’, frequency
A, Y year end frequency
dtype='timedelta64[ns]', freq=None) '2018-01'], dtype='period[M]') BA, BY business year end
frequency
# This particular day contains a day light savings time transition >>> pd.timedelta_range(start='1 day', periods=4) AS, YS year start frequency
>>> ts = [Link]("2016-10-30 00:00:00", tz="Europe/Helsinki") TimedeltaIndex(['1 days', '2 days', '3 days', '4 days'], dtype='timedelta64[ns]’, freq='D’) BAS, BYS business year start
frequency
BH business hour
# Respects absolute time >>> pd.timedelta_range(start='1 day', end='2 days', freq='6H’) frequency
>>> ts + [Link](days=1) TimedeltaIndex(['1 days 00:00:00', '1 days 06:00:00', '1 days 12:00:00’, H hourly frequency
Timestamp('2016-10-30 23:00:00+0200', tz='Europe/Helsinki’) '1 days 18:00:00', '2 days 00:00:00'], dtype='timedelta64[ns]’, freq='6H') T, min minutely frequency
S secondly frequency
L, ms milliseconds
# Respects calendar time >>> pd.timedelta_range(start='1 day', end='5 days', periods=4) U, us microseconds
>>> ts + [Link](days=1) TimedeltaIndex(['1 days 00:00:00', '2 days 08:00:00', '3 days 16:00:00’, '5 days 00:00:00’], N nanoseconds
Timestamp('2016-10-31 00:00:00+0200', tz='Europe/Helsinki') dtype='timedelta64[ns]', freq=None)
Also, Check Slide for division of timedeltas to find number of years between two dates.
232 DT Ch ChCh Ch Ch Ch Ch
Datetime Index
Attributes
The DatetimeIndex class contains many time series
related optimizations: year The year of the datetime.
• A large range of dates for various offsets are pre-computed and month The month as January=1, December=12.
cached under the hood in order to make generating subsequent day The day of the datetime.
date ranges very fast (just have to grab a slice). hour The hours of the datetime.
• Fast shifting using the shift method on pandas objects. minute The minutes of the datetime.
• Unioning of overlapping DatetimeIndex objects with the same second The seconds of the datetime.
microsecond The microseconds of the datetime.
frequency is very fast (important for fast data alignment).
nanosecond The nanoseconds of the datetime.
• Quick access to date fields via properties such as year, month, date Returns numpy array of python [Link] objects (namely, the date part of
etc. Timestamps without timezone information).
• Regularization functions like snap and very fast as of logic. time Returns numpy array of [Link].
timetz Returns numpy array of [Link] 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.
REMEMBER day_of_week The day of the week with Monday=0, Sunday=6.
• Valid date strings can be converted to datetime objects weekday The day of the week with Monday=0, Sunday=6.
using to_datetimefunction or as part of read functions. quarter The quarter of the date.
tz Return timezone, if any.
• Datetime objects in pandas support calculations, logical
freq Return the frequency object if it is set, otherwise None.
operations and convenient date-related properties using freqstr Return the frequency object as a string if its set, otherwise None.
the dt accessor. is_month_start Indicates whether the date is the first day of the month.
• A DatetimeIndex contains these date-related properties and is_month_end Indicates whether the date is the last day of the month.
supports convenient slicing. is_quarter_start Indicator for whether the date is the first day of a quarter.
• Resample is a powerful method to change the frequency of a is_quarter_end Indicator for whether the date is the last day of a quarter.
time series. 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.

233 DT Ch ChCh Ch Ch Ch Ch
Datetime Index
Check if two datetime indexes are equal Methods

>>> import pandas as pd normalize(*args, **kwargs) Convert times to midnight.


>>> idx1 = pd.date_range('2020-01-01','2020-12-31',freq='D’) strftime(*args, **kwargs) Convert to Index using specified date_format.
>>> idx2 = pd.date_range('2020-01-01','2020-11-01',freq='D’) snap([freq]) Snap time stamps to nearest occurring frequency.
>>> idx3 = pd.date_range('2020-01-01','2020-12-31',freq='D’) 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
>>> help([Link]) Array/Index.
Help on method equals in module round(*args, **kwargs) Perform round operation on the data to the specified freq.
[Link]: equals(other: object) -> bool 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.
method of [Link] instance
to_period(*args, **kwargs) Cast to PeriodArray/Index at a particular frequency.
Determines if two Index objects contain the same elements.
to_perioddelta(freq) Calculate TimedeltaArray of difference between index values and index
converted to PeriodArray at specified freq.
>>> print([Link](idx2))
to_pydatetime Return Datetime Array/Index as object ndarray of [Link]
False objects.
to_series Create a Series with both index and values equal to the index keys
>>> print([Link](idx3)) useful with map for returning an indexer based on an index.
True 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 [Link]
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.

234 DT Ch ChCh Ch Ch Ch Ch
DateOffset Objects DateOffset
Standard kind of date increment used
Date Offset Frequency StringDescription for a date range.
DateOffset None Generic offset class, defaults to absolute 24 hours
Properties
BDay or BusinessDay 'B' business day (weekday)
CDay or CustomBusinessDay 'C' custom business day [Link]
Week 'W' one week, optionally anchored on a day of the week [Link]
WeekOfMonth 'WOM' the x-th day of the y-th week of each month [Link]
LastWeekOfMonth 'LWOM' the x-th day of the last week of each month [Link]
MonthEnd 'M' calendar month end [Link]
DateOffset.rule_code
MonthBegin 'MS' calendar month begin
DateOffset.n
BMonthEnd or BusinessMonthEnd 'BM' business month end DateOffset.is_month_start
BMonthBegin or BusinessMonthBegin 'BMS' business month begin DateOffset.is_month_end
CBMonthEnd or CustomBusinessMonthEnd 'CBM' custom business month end
Methods
CBMonthBegin or CustomBusinessMonthBegin 'CBMS' custom business month begin
SemiMonthEnd 'SM' 15th (or other day_of_month) and calendar month end [Link](other)
SemiMonthBegin 'SMS' 15th (or other day_of_month) and calendar month begin DateOffset.apply_index(other)
QuarterEnd 'Q' calendar quarter end [Link]
[Link]
QuarterBegin 'QS' calendar quarter begin
[Link]
BQuarterEnd 'BQ business quarter end DateOffset.is_anchored
BQuarterBegin 'BQS' business quarter begin DateOffset.is_on_offset
FY5253Quarter 'REQ' retail (aka 52-53 week) quarter DateOffset.__call__(*args, **kwargs)
YearEnd 'A' calendar year end DateOffset.is_month_start
YearBegin 'AS' or 'BYS' calendar year begin DateOffset.is_month_end
DateOffset.is_quarter_start
BYearEnd 'BA' business year end DateOffset.is_quarter_end
BYearBegin 'BAS' business year begin DateOffset.is_year_start
FY5253 'RE' retail (aka 52-53 week) year DateOffset.is_year_end
Easter None Easter holiday
BusinessHour 'BH' business hour
CustomBusinessHour 'CBH' custom business hour
Day 'D' one absolute day
Hour 'H' one hour
Minute 'T' or 'min' one minute
Second 'S' one second
Milli 'L' or 'ms' one millisecond
Micro 'U' or 'us' one microsecond
Nano 'N' one nanosecond
235 DT Ch ChCh Ch Ch Ch Ch
AnchoredOffset Objects DateOffset
Standard kind of date increment used
Anchored offsets for a date range.
For some frequencies you can specify an anchoring suffix:
Properties
Alias Description
W-SUN weekly frequency (Sundays). Same as ‘W’ [Link]
W-MON weekly frequency (Mondays) [Link]
W-TUE weekly frequency (Tuesdays) [Link]
W-WED weekly frequency (Wednesdays) [Link]
W-THU weekly frequency (Thursdays) [Link]
DateOffset.rule_code
W-FRI weekly frequency (Fridays)
DateOffset.n
W-SAT weekly frequency (Saturdays) DateOffset.is_month_start
(B)Q(S)-DEC quarterly frequency, year ends in December. Same as ‘Q’ DateOffset.is_month_end
(B)Q(S)-JAN quarterly frequency, year ends in January
(B)Q(S)-FEB quarterly frequency, year ends in February Methods
(B)Q(S)-MAR quarterly frequency, year ends in March
[Link](other)
(B)Q(S)-APR quarterly frequency, year ends in April
DateOffset.apply_index(other)
(B)Q(S)-MAY quarterly frequency, year ends in May [Link]
(B)Q(S)-JUN quarterly frequency, year ends in June [Link]
(B)Q(S)-JUL quarterly frequency, year ends in July [Link]
(B)Q(S)-AUG quarterly frequency, year ends in August DateOffset.is_anchored
(B)Q(S)-SEP quarterly frequency, year ends in September DateOffset.is_on_offset
DateOffset.__call__(*args, **kwargs)
(B)Q(S)-OCT quarterly frequency, year ends in October
DateOffset.is_month_start
(B)Q(S)-NOV quarterly frequency, year ends in November DateOffset.is_month_end
(B)A(S)-DEC annual frequency, anchored end of December. Same as ‘A’ DateOffset.is_quarter_start
(B)A(S)-JAN annual frequency, anchored end of January DateOffset.is_quarter_end
(B)A(S)-FEB annual frequency, anchored end of February DateOffset.is_year_start
(B)A(S)-MAR annual frequency, anchored end of March DateOffset.is_year_end
(B)A(S)-APR annual frequency, anchored end of April
(B)A(S)-MAY annual frequency, anchored end of May
(B)A(S)-JUN annual frequency, anchored end of June
(B)A(S)-JUL annual frequency, anchored end of July
(B)A(S)-AUG annual frequency, anchored end of August
(B)A(S)-SEP annual frequency, anchored end of September
(B)A(S)-OCT annual frequency, anchored end of October
(B)A(S)-NOV annual frequency, anchored end of November
These can be used as arguments to date_range, bdate_range, constructors for DatetimeIndex, as well as various other
timeseries-related functions in pandas.
236 DT Ch ChCh Ch Ch Ch Ch
STATISTICS WITH PYTHON

237 DT Ch ChCh Ch Ch Ch Ch
from scipy import stats #[Link] Official documentation
from scipy import optimize

import seaborn as sns


sns.set_style('whitegrid')

def plot_rv_distribution(X, axes=None):


"""Plot the PDF or PMF, CDF, SF and PPF of a given random variable"""

if axes is None:
fig, axes = [Link](1, 3, figsize=(12, 3))
x_min_999, x_max_999 = [Link](0.999)
x999 = [Link](x_min_999, x_max_999, 1000)
x_min_95, x_max_95 = [Link](0.95)
x95 = [Link](x_min_95, x_max_95, 1000)
if hasattr([Link], "pdf"):
axes[0].plot(x999, [Link](x999), label="PDF")
axes[0].fill_between(x95, [Link](x95), alpha=0.25)
else:
# discrete random variables do not have a pdf method, instead we use pmf:
x999_int = [Link]([Link](int))
axes[0].bar(x999_int, [Link](x999_int), label="PMF")
axes[1].plot(x999, [Link](x999), label="CDF")
axes[1].plot(x999, [Link](x999), label="SF")
axes[2].plot(x999, [Link](x999), label="PPF")
for ax in axes:
[Link]()

fig, axes = [Link](3, 3, figsize=(12, 9))


X = [Link]() # norm object, an instance of the rv_continuous class
plot_rv_distribution(X, axes=axes[0, :])
axes[0, 0].set_ylabel("Normal dist.")

X = stats.f(2, 50) # f object, an instance of the rv_continuous class


plot_rv_distribution(X, axes=axes[1, :])
axes[1, 0].set_ylabel("F dist.")

X = [Link](5) # poisson object, an instance of the rv_discrete class


plot_rv_distribution(X, axes=axes[2, :])
axes[2, 0].set_ylabel("Poisson dist.")
Examples of probability distribution functions (PDF) or probability mass functions (PMFs), cumulative distribution
functions (CDF), survival functions (SF), and percent-point functions (PPF) for a normal distribution (top), an F [Link]('Statistical_Distributions.png', dpi = 300)
distribution (middle), and a Poisson distribution (bottom)
Source : Numerical Python: Scientific Computing and Data Science Applications with Numpy, SciPy and Matplotlib by Robert Johansson
238 DT Ch ChCh Ch Ch Ch Ch
def plot_dist_samples(X, X_samples, title=None, ax=None):
""" Plot the PDF and histogram of samples of a continuous random
variable"""
if ax is None:
fig, ax = [Link](1, 1, figsize=(8, 4))
# Setting xlim to Confidence interval with equal areas around the median
x_lim = [Link](.99)
x = [Link](*x_lim, num=100)
[Link](x, [Link](x), label="PDF", lw=3)
[Link](X_samples, label="samples",density = True, bins=75)
ax.set_xlim(*x_lim)
[Link]()
if title: ax.set_title(title)

Probability distribution function (PDF) together with histograms of 2000 random samples from the return ax
Student’s t distribution (left), the χ distribution (middle), and the exponential distribution (right)
2
fig, axes = [Link](1, 3, figsize=(12, 3))
N = 2000
The SciPy stats module provides classes for representing random variables with a large number of probability
distributions. There are two base classes for discrete and continuous random variables: rv_discrete and # Student's t distribution
rv_continuous. These classes are not used directly, but rather used as base classes for random variables with X = stats.t(7.0) # t object, an instance of the rv_continuous class
plot_dist_samples(X, [Link](N), "Student's t dist.", ax=axes[0])
specific distributions, and define a common interface for all random variable classes in SciPy stats.
# The chisquared distribution
X = stats.chi2(5.0) # chi object, an instance of the rv_continuous class
Discrete Distribution Continous Distribution plot_dist_samples(X, [Link](N), r"$\chi^2$ dist.", ax=axes[1])
• Binomial distribution • Normal distribution
• Poisson distribution • Student’s t distribution # The exponential distribution
• Hypergeometric distribution • Chi squared distribution X = [Link](0.5) # expon object, an instance of the rv_continuous class
• F distribution plot_dist_samples(X, [Link](N), "exponential dist.", ax=axes[2])
• Uniform distribution
• Exponential Distribution [Link]('Statistical [Link]', dpi = 300, transparent = True)
• Log normal Distribution
Resources :
• Numerical Python: Scientific Computing and Data Science Applications with Numpy, SciPy and Matplotlib by Robert Johansson
• A First Course in Statistical Inference by Jonathan Gillard
• Business statistics Communicating with numbers by Jaggia/Kelly
• Python for Probability, Statistics, and Machine Learning by Jose Unpingco
• An Introduction to Statistics with Python With Applications in the Life Sciences by Thomas Haslwanter
• Practical Statistics for Data Scientists by Peter Bruce, Andrew Bruce & Peter Gedeck

Source of Plot/code : Numerical Python: Scientific Computing and Data Science Applications with Numpy, SciPy and Matplotlib by Robert Johansson
239 DT Ch ChCh Ch Ch Ch Ch
import [Link] as plt
from scipy import stats

# Instantiating a chi-squared distribution with degrees of freedom = 5. Link


X = stats.chi2(df=5)
# Generating rvs of chi distribution
X_samples = [Link](500)

#Return estimates of shape, location, and scale parameters from data using
Maximum Likelihood Estimation Method. Link
df, loc, scale = [Link](X_samples)

Y = stats.chi2(df=df, loc=loc, scale=scale)

fig, axes = [Link](1, 2, figsize=(12, 4))


Original and recreated probability distribution function (left) and the error (right), from a
# Setting xlim to Confidence interval with equal areas around the median
maximum likelihood fit of 500 random samples of the original distribution x_lim = [Link](.99) #
Maximum Likelihood Estimation – A method of estimation of one or more parameters of a population by
maximizing the likelihood or log-likelihood function of the sample with respect to the parameter(s). The
x = [Link](*x_lim, num=100)
maximum likelihood estimators are functions of the sample observations that make the likelihood function axes[0].plot(x, [Link](x), label="original")
greatest. axes[0].plot(x, [Link](x), label="recreated")
axes[0].legend()
Discrete Distribution Continous Distribution axes[1].plot(x, [Link](x) - [Link](x), label="error")
• Binomial distribution • Normal distribution axes[1].legend()
• Poisson distribution • Student’s t distribution
• Hypergeometric distribution • Chi squared distribution [Link]('[Link]', dpi = 300, transparent = True)
• F distribution
• Uniform distribution
• Exponential Distribution
• Log normal Distribution
Resources :
• Numerical Python: Scientific Computing and Data Science Applications with Numpy, SciPy and Matplotlib by Robert Johansson
• A First Course in Statistical Inference by Jonathan Gillard
• Business statistics Communicating with numbers by Jaggia/Kelly
• Python for Probability, Statistics, and Machine Learning by Jose Unpingco
• An Introduction to Statistics with Python With Applications in the Life Sciences by Thomas Haslwanter
Source of Plot/code : Numerical Python: Scientific Computing and Data Science Applications with Numpy, SciPy and Matplotlib by Robert Johansson
240 DT Ch ChCh Ch Ch Ch Ch
Selected Methods for Discrete and Continuous Random Variables in the SciPy stats Module Statistical functions ([Link])

Probability distributions
Methods Description • Continuous distributions
pdf/pmf probability distribution function (continuous) or probability mass function (discrete). • Multivariate distributions
cdf Cumulative distribution function. • Discrete distributions
sf Survival function (1 – cdf). Summary statistics
Frequency statistics
ppf percent-point function (inverse of cdf). Correlation functions
moment Noncentral moments of nth order. Statistical tests
stats Statistics of the distribution (typically the mean and variance, sometimes additional statistics). Quasi-Monte Carlo
Fit distribution to data using a numerical maximum likelihood optimization (for continuous Masked statistics functions
fit distributions). Other statistical functionality
• Transformations
expect expectation value of a function with respect to the distribution. • Statistical distances
interval the endpoints of the interval that contains a given percentage of the distribution (confidence interval). Random variate generation / CDF
rvs random variable samples. takes as argument the size of the resulting array of samples. Inversion
mean, median, std, var Descriptive statistics: mean, median, standard deviation, and the variance of the distribution. • Circular statistical functions
• Contingency table functions
Summary of Common Hypothesis Test Cases with the Corresponding Distributions and SciPy Functions • Plot-tests
• Univariate and multivariate
Null Hypothesis Distributions SciPy Functions for Test kernel density estimation
test if the mean of a population is a given value. Normal distribution (stats. norm), or Student’s t stats. ttest_1samp • Warnings used in [Link]
distribution (stats.t)
test if the means of two random variables are equal Student’s t distribution (stats.t) stats.ttest_ind, stats.ttest_rel
(independent or paired samples).
test goodness of fit of a continuous distribution to data. Kolmogorov-Smirnov distribution [Link]
test if categorical data occur with given frequency (sum of χ2 distribution (stats.chi2) [Link]
squared normally distributed variables).
test for the independence of categorical variables in a χ2 distribution (stats.chi2) stats.chi2_ contingency
contingency table.
test for equal variance in samples of two or more variables. F distribution (stats.f) [Link], [Link]
test for noncorrelation between two variables. Beta distribution ([Link], [Link],
[Link]
test if two or more variables have the same population [Link]) F distribution stats.f_oneway, [Link]
mean (aNOVa – analysis of variance) .

Check Numerical Python: Scientific Computing and Data Science Applications with Numpy, SciPy and Matplotlib by Robert
Johansson

241 DT Ch ChCh Ch Ch Ch Ch
APPENDIX
All Code examples have been checked with Python Version 3.8.3 and IDE JupyterLab
The package details are as below :
Package Name pandas numpy scipy
Version 1.2.1 1.19.5 1.6.0
Summary Powerful data structures for NumPy is the fundamental SciPy: Scientific Library for
data analysis, time series, and package for array computing Python
statistics with Python.
* Use pip show <package_name> to get details of a package

242 DT Ch ChCh Ch Ch Ch Ch
Back
# set the basic properties xlab.set_style('italic')
ax.set_xlabel('Time of posting (US EST)') xlab.set_size(10)
ax.set_ylabel('Percentage of Frontpaged Submissions') ylab.set_style('italic')
ax.set_title('Likelihood of Reaching the Frontpage') ylab.set_size(10)

# set the limits # tweak the title


ax.set_xlim(0, 24) ttl = [Link]
ax.set_ylim(6, 24) ttl.set_weight('bold')
[Link]('Filline [Link]',dpi=300, format='png',
# set the grid on bbox_inches='tight')
[Link]('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


#[Link] thickness
make-matplotlib-graphs-look-professionally-done-like-this l.set_edgecolors([[0.3, 0.6, .9, 1]])
# Check out the answer by DrV l.set_linewidths([2])

# Necessary imports # add more ticks


import numpy as np ax.set_xticks([Link](25))
import [Link] as plt

[Link]() # remove tick marks


[Link].set_tick_params(size=0)
# create some fictive access data by hour [Link].set_tick_params(size=0)
xdata = [Link](25)
ydata = [Link](10, 20, 25) # change the color of the top and right spines to opaque
ydata[24] = ydata[0] gray
[Link]['right'].set_color((.8,.8,.8))
[Link]['top'].set_color((.8,.8,.8))
# let us make a simple graph
fig = [Link](figsize=[7,5]) # tweak the axis labels
ax = [Link](111) xlab = [Link].get_label()
l = ax.fill_between(xdata, ydata) ylab = [Link].get_label()

243 DT Ch ChCh Ch Ch Ch Ch
#[Link]
Code 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

244 DT Ch ChCh Ch Ch Ch Ch
#Create a date time index # Add x labels for months
dti = pd.date_range('2020-01-01 00:00', '2020-12-31 \ ax.set_xlabel(df_month.index[0].month_name().upper(),
23:59', freq='6H’) labelpad=5)
consumption = [Link](1000, 2000, size=[Link]) [Link].set_label_position('top’)

# Create the data frame # Add title and edit spaces between subplots
df = [Link](dict(consumption=consumption), year = [Link][0].year
index=dti) freq = df_month.[Link]

# Draw and format subplots by looping through months title = f'{year} consumption displayed for each month with
and flattened array of axes a {freq} frequency’
fig, axs = [Link](4, 3, figsize=(13, 9), sharey=True)
[Link]([Link](), y=0.95, fontsize=12)
for month, ax in zip([Link](), [Link]): fig.subplots_adjust(wspace=0.1, hspace=0.5)
# Select monthly data and plot it
df_month = df[[Link] == month] [Link](0.5, 0.99, 'Weekends are highlighted by using \
#[Link] [Link](df_month.index, df_month['consumption’]) the DatetimeIndex’,
ng-weekends-in-small-multiples ha='center', fontsize=14, weight='semibold’)
# Check the answer by Patrick Fitzgerald # set limit similar to plot shown in question
ax.set_ylim(0, 2500) [Link]('Daily consumption with weekends \
CONTEXT [Link]’, bbox_inches = "tight",pad_inches =
• Raw data is time Series with Daily frequency # Draw vertical spans for weekends: computing the time 0.5, dpi=300)
• Figure with multiple subplots with each one for a delta and adding it to the date solves the problem of [Link]()
particular month (iteration used) exceeding the df_month.index
• Weekends need to be highlighted
# Also, Check below excellent links on using spans and fill
timedelta = pd.to_timedelta(df_month.[Link]) areas
import numpy as np weekends =df_month.index[df_month.[Link] • [Link]
import pandas as pd >=5].to_series()
import [Link] as plt matplotlib-plot-can-i-highlight-specific-x-value-
for date in weekends: ranges?noredirect=1&lq=1
import [Link] as mdates # used only for [Link](date, date+timedelta, facecolor='k’,
method 2 • [Link]
edgecolor=None, alpha=.1) highlight-weekends-for-time-series-line-plot-in-
# Format tick labels python/66052245#66052245
[Link]() ax.set_xticks(ax.get_xticks()) • [Link]
tk_labels = [pd.to_datetime(tk, unit='D').strftime('%d’) ht-time-interval-in-multivariate-time-series-plot-using-
# Create sample dataset for tk in ax.get_xticks()]
# random number generator matplotlib-and-se?rq=1
ax.set_xticklabels(tk_labels, rotation=0, ha='center’)
rng = [Link].default_rng(seed=1)

245 DT Ch ChCh Ch Ch Ch Ch
Code #[Link]
highlight-weekends-for-time-series-line-plot-in-
python/66052245#66052245
# Check the answer by Patrick Fitzgerald

Highlighting Weekends in a Time


Series

246 DT Ch ChCh Ch Ch Ch Ch
# 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 = [Link](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 = [Link]([Link](xmin), [Link](xmax)+2)
weekends = [([Link]()>=5)|([Link]()==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
#[Link] [Link].set_major_locator([Link]())
highlight-weekends-for-time-series-line-plot-in- [Link].set_minor_locator([Link](bymonthday=[Link](5, 31, step=7)))
python/66052245#66052245 [Link].set_major_formatter([Link]('\n%b'))
# Check the answer by Patrick Fitzgerald [Link].set_minor_formatter([Link]('%d'))
#Highlighting Weekends on a time series plot # Additional formatting
import numpy as np [Link].autofmt_xdate(rotation=0, ha='center')
import pandas as pd title = 'Daily count of trips with weekends highlighted from SAT 00:00 to MON 00:00'
import [Link] as plt ax.set_title(title, pad=20, fontsize=14)
import [Link] as mdates [Link]('Daily count of trips with weekends [Link]',bbox_inches = "tight",pad_inches = 0.5, dpi=300)
[Link]()
# Create sample dataset
# random number generator
rng = [Link].default_rng(seed=1234)

dti = pd.date_range('2017-01-01', '2017-05-15', freq='D’)


Highlighting Weekends in a
counts = 5000 + [Link]([Link](-1000, 1000, Time Series
size=[Link]))

df = [Link](dict(Counts=counts), index=dti)

247 DT Ch ChCh Ch Ch Ch Ch
#Matplotlib Release, Page no.120-121 import [Link] as plt #Matplotlib Release, Page no.120-121
#[Link] creates a [Link] instance import numpy as np import numpy as np
fig = [Link](figsize = (10,5)) import [Link] as plt
rect = [Link] # a rectangle instance fig, (ax1, ax2) = [Link](1,2, sharey = True) # Fixing random state for reproducibility
rect.set_facecolor('lightgoldenrodyellow') [Link](19680801)
x = [Link](1,20,20) fig, ax = [Link]()
ax1 = fig.add_axes([0.1, 0.3, 0.4, 0.4]) [Link](100*[Link](20))
rect = [Link] #Set the face color of figure background
rect.set_facecolor('lightslategray’) rect = [Link] [Link](facecolor="yellow",alpha =0.5)
[Link](facecolor = 'aliceblue’) [Link].set_facecolor("yellow")
for label in [Link].get_ticklabels(): [Link].set_alpha(0.5)
# label is a Text instance #Set the face color of the axes backgrounds [Link](facecolor = "salmon")
label.set_color('red') [Link](facecolor = 'salmon')
label.set_rotation(45) [Link](facecolor = 'aquamarine') # Use automatic StrMethodFormatter
label.set_fontsize(16) [Link](x) [Link].set_major_formatter('${x:1.2f}')
for line in [Link].get_ticklines(): [Link].set_tick_params(which='major’, labelcolor='green’,
# line is a Line2D instance [Link]() labelleft=False, labelright=True)
line.set_color('green')
line.set_markersize(25) #ax.set_xticks(ax.get_xticks())
line.set_markeredgewidth(3)
Appearance of Plot #ax.set_xticklabels(ax.get_xticklabels(), rotation = 30)
[Link](f'tick_labels and [Link]',dpi=300, Fig, Axes background [Link](ax.get_xticklabels(), rotation=30, ha='right')
format='png', bbox_inches='tight') [Link]()
[Link]() Tick labels, Tick lines
248 DT Ch ChCh Ch Ch Ch Ch
import [Link] as plt

def plot_and_text(axis, text):


'''Simple function to add a straight line
and text to an axis object'''
[Link]([0,1],[0,1])
[Link](0.02, 0.9, text)

f = [Link]()
f2 = [Link]()

_max = 12
for i in range(_max):
axis = f.add_subplot(3,4,i+1, fc=(0,0,0,i/(_max*2)), xticks=[],
#[Link] pos is a three digit integer, where the first digit is the yticks=[])
matplotlib-what-does-the-argument-mean-in-fig-add- number of rows, the second the number of columns, and plot_and_text(axis,chr(i+97) + ') ' + '3,4,' +str(i+1))
subplot111?rq=1 the third the index of the subplot. i.e.
Check the answer by compuphys fig.add_subplot(235) is the same as fig.add_subplot(2, 3,
5). # If this check isn't in place, a
The link lucidly discusses the multiple call signatures of # ValueError: num must be 1 <= num <= 15, not 0 is raised
fig.add_subplot method. Also, Check the documentation : if i < 9:
[Link] axis = f2.add_subplot(341+i, fc=(0,0,0,i/(_max*2)), \
[Link] xticks=[], yticks=[])
The add_subplot() method has several call signatures: plot_and_text(axis,chr(i+97) + ') ' + str(341+i))
• add_subplot(nrows, ncols, index, **kwargs)
• add_subplot(pos, **kwargs) f.tight_layout()
• add_subplot(ax) f2.tight_layout()
• add_subplot()
f._label = 'fig1'
The following code demonstrates the difference between f2._label = 'fig2'
the first two call signatures.
• add_subplot(nrows, ncols, index, **kwargs) for fig in [f, f2]:
• add_subplot(pos, **kwargs) print(f'{fig._label}')
Making multiple subplots [Link](f'Adding Subplots_{fig._label}.png’,
The first call signature can accommodate any number of dpi = 300,format='png', bbox_inches='tight')
rows, columns. Whereas the second call signature is fig.add_subplot() Method [Link]()
limited to total 9 subplots
249 DT Ch ChCh Ch Ch Ch Ch
Automatic Detection Legend Automatic Detection Legend Explicitly Define Legend Elements

#Legend Call Signature : [Link]() #Legend Call Signature : [Link]() #Legend Call Signature : [Link](handles, labels)
#All the lines have been assigned valid text labels #Except the red line, the other lines have been assigned #Explicitly Passing the handles and corresponding labels
text labels
import [Link] as plt
import [Link] as plt import [Link] as plt
fig, ax = [Link]()
fig, ax = [Link]() fig, ax = [Link]() lgh1, = [Link]([0, 0.5], [0, 1], '-g', label="a")
lgh1, = [Link]([0, 0.5], [0, 1], '-g', label="a") lgh1, = [Link]([0, 0.5], [0, 1], '-g', label="a") lgh2, = [Link]([0, 1], [0, 1], '-r')
lgh2, = [Link]([0, 1], [0, 1], '-r', label = 'b') lgh2, = [Link]([0, 1], [0, 1], '-r') lgh3, = [Link]([0, 1], [0, 0.5], '-b', label="c")
lgh3, = [Link]([0, 1], [0, 0.5], '-b', label="c") lgh3, = [Link]([0, 1], [0, 0.5], '-b', label="c")

#[Link]() #This is equivalent to [Link]() #[Link]() #This is equivalent to [Link]() handles = [lgh1, lgh2, lgh3]
[Link]() [Link]() labels = [‘A’, ‘B’, ‘C’]
[Link]() [Link]()
#[Link] #[Link](handles = handles, labels = labels)
b-legend-not-working-correctly-with-handles [Link](handles = handles, labels =labels)
[Link]()

250 DT Ch ChCh Ch Ch Ch Ch
Proxy Artists : Manual Patch Addition Proxy Artists : Manual Lines Addition Labels Appearing before Legend key

#Legend Call Signature : [Link](handles) #Legend Call Signature : [Link]() #[Link]


#The patches are not in the axes but specifically added #Except the red line, the other lines have been assigned b-put-legend-symbols-on-the-right-of-the-labels
for legend text labels
#Secondly, we have used a dictionary of labels and colors #[Link] fig, ax = [Link](figsize = (5,3))
#[Link] -add-legend-items-python-matplotlib [Link]([1,2],[3,4], label='labeltext')
-add-legend-items-python-matplotlib import [Link] as plt [Link](markerfirst=False)
from [Link] import Line2D [Link]().savefig('markerfirst_legend.png',dpi = 150,
import [Link] as mpatches bbox_inches='tight')
import [Link] as plt fig, ax = [Link](figsize=(5,3)) [Link]()
colors = ['black', 'red', 'green’]
fig, ax = [Link]()
legend_dict = { 'data1' : 'green', 'data2' : 'red’, #Instantiating proxy line artists
'data3' : 'blue' } lines = [Line2D([0], [0], color=c, linewidth=3, linestyle='--’)
patchList = [] for c in colors]
for key in legend_dict: labels = ['black data', 'red data', 'green data']
data_key = [Link](color=legend_dict[key], [Link](lines, labels)
label=key)
[Link](data_key) [Link]('proxy artists_line [Link]',dpi = 150,
bbox_inches='tight')
[Link](handles=patchList)
[Link]('legend_from [Link]',bbox_inches='tight')
251 DT Ch ChCh Ch Ch Ch Ch
Making Legend Keys square Legend Addition in Complex Plots Remove Legend key

#Legend Call Signature : [Link](handles)


#The patches are not in the axes but specifically added #[Link]
for legend legend-key-in-matplotlib
#[Link] import [Link] as plt
[Link]
b-make-legend-keys-square import numpy as np
otations/legend_demo.html#sphx-glr-gallery-text-labels-
import [Link] as mlines and-annotations-legend-demo-py
[Link](100)
[Link]() x = [Link](size=5000)
Check the link above. There are various other use cases of
fig, ax = [Link](figsize = (5,3)) fig, ax = [Link]()
legend covered.
rect1 = mlines.Line2D([], [], marker="s", markersize=10, [Link](x, label = 'something')
linewidth=0, color="red")
rect2 = mlines.Line2D([], [], marker="s", markersize=10, [Link]()
linewidth=0, color="yellow") leg = [Link]().get_legend()
[Link][0].set_visible(False)
[Link]((rect1, rect2), ('Men', 'Women'))
[Link]('Making square [Link]',dpi = 150, [Link]()
bbox_inches='tight')
[Link]()

252 DT Ch ChCh Ch Ch Ch Ch
#[Link]
import [Link] as plt
[Link]["[Link]"] = 12, 4
fig, axes = [Link](ncols=3)
upper left upper center upper right
locs = ["upper left", "lower left", "center right"]
for l, ax in zip(locs, [Link]()): #Iterating over the subplots to create line plots center left center center right
ax.set_title(l)
[Link]([1,2,3],[2,3,1], "b-", label="blue") lower left lower center lower right
[Link]([1,2,3],[1,2,1], "r-", label="red")
[Link](loc=l, bbox_to_anchor=(0.6,0.5)) #Note the green dot in the axes is the bbox_to_anchor
[Link]((0.6),(0.5), s=81, c="limegreen", transform=[Link])

plt.tight_layout()
Legend
[Link]('Legend [Link]', dpi = 300)
[Link]()
‘loc’ argument

253 DT Ch ChCh Ch Ch Ch Ch
import [Link] as plt Style Sheets
fig = [Link](dpi=100, figsize=(11, 8), tight_layout=True)
available = ['default'] + [Link] in Matplotlib
Back
#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 [Link](style):
ax = fig.add_subplot(2,3, i + 1)
[Link](x,y)
ax.set_title(style)
[Link]('Styles_second_2.png',bbox_inches = "tight",pad_inches = 0.05, dpi=300)
[Link]()

import [Link] as plt


fig = [Link](dpi=100, figsize=(11, 8), tight_layout=True)
available = ['default'] + [Link]

#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 [Link](style):
ax = fig.add_subplot(2,3, i + 1)
[Link](x,y)
ax.set_title(style)

[Link]('Styles_second_2.png',bbox_inches = "tight",pad_inches = 0.05, dpi=300)


[Link]()

254 DT Ch ChCh Ch Ch Ch Ch
#Accessing colors from the colormap
colors = [ [Link](x) for x in [Link](0,0.4,4)] Tick Locators
[[Link](facecolor = colors[i]) for i,ax in enumerate([Link])]
Back

#Line Plot in axes ax2


[Link](x)

# Setting x and y limits in all the axes


[ax.set_xlim(0,10) for ax in [Link]]
[ax.set_ylim(0,20) for ax in [Link]]

#Setting the tick locators using locator class


[Link].set_major_locator([Link]())
[Link].set_major_locator([Link](2))
[Link].set_major_locator([Link]([0,1,2,8,9]))
[Link].set_major_locator([Link](5))
import [Link] as plt
import numpy as np #Making list of Locator labels
import [Link] as ticker Locator_labels = ['NullLocator()', 'MultipleLocator(2)', 'FixedLocator([0,1,2,8,9])', 'LinearLocator\n(numticks = 5)’]

#Instantiating figure and axes #Adding text iteratively to the figure axes
fig, ax = [Link](2,2,figsize= (10,5), sharey = True) for ax, label in zip([Link],Locator_labels):
[Link](x = 5,y = 2, s = label, rotation_mode = 'anchor', ha = 'center', size = 18)
x = [Link](3,20,20)

#Setting background color of figure [Link]('Tick Locators_2.png', dpi=300, format='png', bbox_inches='tight’)


rect = [Link] [Link]()
[Link](facecolor = 'lightgrey', alpha = 0.5)

ax1,ax2,ax3,ax4 = [Link]()

#[Link](facecolor = 'salmon')
#[Link](facecolor = 'aquamarine’)

# Setting different colors for the axes using colormap


#colors = [ [Link].Pastel1(x) for x in [Link](0, 1,4)]
#colors = [ [Link].tab20c(x) for x in [Link](0, 1,4)]

255 DT Ch ChCh Ch Ch Ch Ch
TICK FORMATTERS (1 of 3) - CODE Back
Code Start

#[Link]
[Link]?highlight=formatter

import [Link] 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
[Link].set_major_locator([Link]())
[Link].set_color('none’)
[Link].set_color('none’)
[Link].set_color('none’)

# define tick positions


[Link].set_major_locator([Link](1.00))
[Link].set_minor_locator([Link](0.25))
[Link].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)
[Link](0.0, 0.2, title, transform=[Link],
fontsize=14, fontname='Monospace’,
color='tab:blue’)
Code continues to Next slide

256 DT Ch ChCh Ch Ch Ch Ch
TICK FORMATTERS(2 of 3) – CODE CONTINUED Code continues from Prev slide

#[Link]

# 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 `~.[Link]` 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 = [Link](2, 1, figsize=(8, 2)) [Link]('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()
Code continues to Next slide

257 DT Ch ChCh Ch Ch Ch Ch
TICK FORMATTERS(3 of 3) – CODE CONTINUED
# StrMethod formatter
setup(axs1[1], title="StrMethodFormatter('{x:.3f}’)”)
axs1[1].xaxis.set_major_formatter([Link]("{x:.3f}"))

# FuncFormatter can be used as a decorator


@[Link]
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([Link](positions))
axs1[3].xaxis.set_major_formatter([Link](labels))

# Scalar formatter
setup(axs1[4], title="ScalarFormatter()")
axs1[4].xaxis.set_major_formatter([Link](useMathText=True))

Code continues from Prev slide # FormatStr formatter


#[Link] setup(axs1[5], title="FormatStrFormatter('#%d’)”)
[Link]?highlight=formatter axs1[5].xaxis.set_major_formatter([Link]("#%d"))
# The remaining examples use Formatter objects.
fig1, axs1 = [Link](7, 1, figsize=(8, 6)) # Percent formatter
[Link]('Formatter Object Formatting’) setup(axs1[6], title="PercentFormatter(xmax=5)")
axs1[6].xaxis.set_major_formatter([Link](xmax=5))
# Null formatter
setup(axs1[0], title="NullFormatter()") fig1.tight_layout()
axs1[0].xaxis.set_major_formatter([Link]()) [Link]()

DT Ch ChCh Ch Ch Ch Ch
Code End
258
[Link](text = '(1,0)\nax1 axes\ncoordinate system’, xy =(1,0), xycoords = [Link], Coordinate
xytext=(2,5 ), textcoords=[Link],fontsize = 15,
bbox = bbox_args_axes, arrowprops = arrow_args, ha = 'left') System
[Link](text = '(1,1)\nax1 axes\ncoordinate system', xy =(1,1),xycoords = [Link],
xytext=(2,17.5), textcoords=[Link],fontsize = 15, Transform
bbox = bbox_args_axes, arrowprops = arrow_args, va = 'top') Objects
[Link](text = '(0,1)\nax2 axes\ncoordinate system’, xy =(0.0,1),xycoords = [Link],
Back
xytext=(0.5, 22.5), textcoords=[Link],fontsize = 15,
bbox = bbox_args_axes, arrowprops = arrow_args)

[Link](text = '(0,20)\nax2 data\ncoordinate system', xy =(0,20),xycoords = [Link],


xytext=(-5, 22.5), textcoords=[Link],fontsize = 15,
bbox = bbox_args_data, arrowprops = arrow_args)
Code start
import [Link] as plt [Link](text = '(0,0.5)\nfig\ncoordinate system', xy =(0,0.5),xycoords = [Link],
from [Link] import Ellipse xytext=(0.03, 0.95), textcoords=[Link], fontsize = 15,
import numpy as np bbox = bbox_args_fig, arrowprops = arrow_args)
from [Link] import OffsetFrom
[Link](text = '(1,1)\nfig\ncoordinate system’, xy =(1,1.1),xycoords = [Link],
[Link]() xytext=(7, 25), textcoords=[Link],fontsize = 15, va = 'top’,
bbox = bbox_args_fig, arrowprops = arrow_args )
#Instantiating Figure and Axes
fig, (ax1, ax2) = [Link](1, 2, figsize = (12,5)) [Link](text = '(1,1)\nax2 axes\ncoordinate system’, xy =(1,1),xycoords = [Link],
xytext=(2,17.5), textcoords=[Link],fontsize = 15, va = 'top',
[Link](edgecolor = ‘0.8’, lw = 4) bbox = bbox_args_axes, arrowprops = arrow_args)
#Configuring bbox and arrow arguments for axes, data [ax.set_xlim(0,10) for ax in [Link]] #Setting x limits in all the axes
and fig [ax.set_ylim(0,20) for ax in [Link]] #Setting y limits in all the axes
bbox_args_axes = dict(boxstyle ="round", fc ="salmon",
alpha = 0.6) [Link](x = 0.5, y = 0.5, s = “Axes instance : ax2”, ha = ‘center’,transform = [Link], fontsize = 20)
bbox_args_data = dict(boxstyle ="round", fc ="cyan", [Link](x = 0.5, y = 0.5, s = “Axes instance : ax1”, ha = ‘center’,transform = [Link], fontsize = 20)
alpha = 0.6) [Link](x = 0.02, y = 0.02, s = “Figure instance”, fontsize = 20)
bbox_args_fig = dict(boxstyle ="round", plt.subplots_adjust(bottom =0.2) #Adjusting padding
fc ="lightgreen", alpha = 0.6)
#plt.tight_layout(rect = [0,0,0.8,0.8])
arrow_args = dict(arrowstyle ="->") [Link](‘coordinate [Link]’, dpi = 150, format=‘png’, bbox_inches='tight’ )
Code End
259 DT Ch ChCh Ch Ch Ch Ch
Back
# 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 [Link]:
[Link](ax)
[Link]([]) #Removing x ticks
import [Link] as gridspec [Link]([]) #Removing y ticks
import [Link] as plt
#Create labels list containing axes names
fig = [Link](figsize=(13.5, 7))
labels = ['Jupyter', 'Ipython', 'scikit', 'statsmodels’,
#Creating Gridspec object
'Seaborn', 'Scipy’, 'Pandas', 'Matplotlib’,
spec = [Link](nrows = 5,ncols = 11, 'Numpy', 'Python’]
wspace = 0.2, hspace=0.1)
ax_list = [ax1, ax2, ax3, ax4, ax5, ax6, ax7, ax8,ax9, ax10]
#Create Axes for Jupyter
#Adding text to axes
ax1 = fig.add_subplot(spec[:, 0])
for i,(ax,label) in enumerate(zip(ax_list,labels)):
#Create Axes IPython if i <=1 :
ax2 = fig.add_subplot(spec[:, 1])
[Link](0.5, 0.5, label, transform = [Link],
fontsize = 20, ha = 'center’,
# Create Axes for scikit
rotation = 90, va = 'center')
ax3 = fig.add_subplot(spec[0, 2::])
else :
[Link](0.5, 0.5, label, transform = [Link],
# Create Axes for Statsmodels
fontsize = 20, ha = 'center')
ax4 = fig.add_subplot(spec[1, 2:4])
[Link]('Libraries_Python.png', dpi = 300,
# Create Axes for Seaborn
format='png’, bbox_inches='tight')
ax5 = fig.add_subplot(spec[1, 4:8])
# Create Axes for Scipy
ax6 = fig.add_subplot(spec[1:3, 8:11])
GridSpec Use
# Create Axes for Pandas
ax7 = fig.add_subplot(spec[2, 2:5])
Display structure of important packages
260 DT Ch ChCh Ch Ch Ch Ch
Horizontal
Alignment
of
Text Instances

Back

Code Start
import [Link] as plt # Creating a list of horizontal alignment values
import [Link] as ticker ha_values = ['left', 'center', 'right’]
# Setting the horizontal alignment by iterating over axes
fig, ax = [Link](1,3, figsize=(18,6)) for i, ax_obj in enumerate([Link]):
if i < len(ha_values):
# Setting both xlim and ylim as (0,20) ax_obj.text(10,10, f'ha : {ha_values[i]}', rotation = 20,
[ax_obj.set(ylim = (0,20), xlim = (0,20)) for ax_obj in rotation_mode = 'anchor’,
[Link]] ha = ha_values[i],fontsize = 22)

# Plotting a scatter point at (10,10) #Setting the figure background color to white
[ax_obj.scatter([10],[10], color = 'r') for ax_obj in [Link]] [Link](color = 'white')

# Setting number of ticks in both x axis and y axis to 5 in # Adding a title


all the axes [Link]("Horizontal Alignment Demo - 'left', 'center’, 'right'\nrotation = 20,
[ax_obj.xaxis.set_major_locator([Link](5)) for rotation_mode = 'anchor' \nposition = (10,10)", x = 0.05, y = 0.95, fontsize = 22, ha = 'left' )
ax_obj in [Link]] plt.tight_layout(rect = [0,0,1,0.95])
[ax_obj.yaxis.set_major_locator([Link](5)) for
ax_obj in [Link]] [Link]("Horizontal Alignment_vertical [Link]”,
dpi=300, format='png', bbox_inches='tight')
Code End

261 DT Ch ChCh Ch Ch Ch Ch
Back #User Defined Function to stylize Axes #Code snippet that uses User defined functions –
def stylize_axes(ax): stylize_axes and Add_data_labels
#Making the axes background light gray
ax.set_facecolor('.85’) # Doing the necessary imports
import [Link] as plt
# Setting the y limits import numpy as np
ax.set_ylim(0,ax.get_ylim()[1]*1.1) import [Link] as spines

#Making the tick lines disppear [Link]()


ax.tick_params(length=0) %matplotlib inline

#Making the xtick labels rotated #create a list of rgba colors from a [Link]
[[Link](size = 13,rotation = 15, colormap
rotation_mode = 'anchor',ha = 'right’) colors = [ [Link].Dark2(x) for x in [Link](0, 1, len(x))]
for labels in ax.get_xticklabels()]
Code Start
#Instantiating figure, axes
data = {'Water':465, #Setting grid lines to white color fig, ax = [Link](figsize = (10,6), frameon = True)
'Alchoholic Beverages':271, [Link](True, axis='x', color='white’)
'Carbonated soft drinks': 224, #Creating a bar plot by [Link] method
'Milk and Dairy products': 260, #Setting grid lines below all the artists # Note the colors passed as the color argument
'New Drinks': 75, ax.set_axisbelow(True) bars = [Link](x,y, color = colors)
'Fruits and Vegetable Juices': 86}
x = list([Link]()) #Making all the spines invisible #Setting the title of plot
y = list([Link]()) [spine.set_visible(False) for spine in [Link]()] StrTitle = 'Consumption of Packed beverages in billion
liters,\n by Beverage Type, Global, 2019'
# UDF for Adding Annotations to place data labels Also check : [Link] ax.set_title(StrTitle, size = 16)
def Add_data_labels(rect): #rect is [Link] object bar-chart-race-animation-in-python-with-matplotlib
for p in rect: #Using the user defined functions – to add data labels
#Retrieving the Axes container of patch object Note : The data figures in the graph are fictitious. and stylize the axes
ax = rect[0].axes Add_data_labels([Link])
#Adding text iteratively on top of each bar UDF stands for User Defined Function. stylize_axes(ax)
#Using get and set on x,y parameters of rectangle
patch
[Link]( x = p.get_x()+p.get_width()/2, Bar Charts [Link]('[Link]', dpi=300, format='png',
bbox_inches='tight')
y = ax.get_ylim()[1]*0.01+ p.get_height(), Code End
s = p.get_height(),
ha = 'center', size = 12)

262 DT Ch ChCh Ch Ch Ch Ch
#Setting xticklines visibility to false # Adding Legend labels and handles
[[Link](visible= False)for ticks in ax.get_xticklines()] [p.set_label(x[i]) for i,p in zip(range(len(x)), [Link])]
[Link](bbox_to_anchor = (1.04,.5),
#Setting the y limits to leave ample room above bar loc="upper left", borderaxespad=0,
ax.set_ylim(0,ax.get_ylim()[1]*1.1) fontsize = 'large’)

#Rotating and aligning the xtick labels #Setting title of the axes
for labels in ax.get_xticklabels(): StrTitle = 'Consumption of Packed beverages in billion
[Link](rotation = 15, size = 12, liters,\n by Beverage Type, Global, 2019'
rotation_mode = 'anchor', ax.set_title(StrTitle, size = 16)
ha = 'right'
)
[Link]('Consumption_4.png', dpi=300, format='png',
#Rotating and aligning the ytick labels bbox_inches='tight')
for labels in [Link].get_ticklabels():
data = {'Water':465, Back [Link](labels, size = 14, rotation_mode = 'anchor’,
'Alchoholic Beverages':271, ha = 'right’)
'Carbonated soft drinks': 224, Bar Chart #Setting the figure and axes background colors
'Milk and Dairy products': 260,
'New Drinks': 75, With [Link](facecolor = 'white')
[Link]([Link], facecolor = 'lightgoldenrodyellow’,
'Fruits and Vegetable Juices': 86} Legend edgecolor = 'grey', lw = 4)
x = list([Link]())
y = list([Link]())
#Making the y gridlines lighter by setting opacity to 0.5
for lines in ax.get_ygridlines():
import numpy as np [Link](alpha = 0.5)
import [Link] as plt #Basic Bar Chart
import [Link] as spines #Setting off the xgridlines to False import [Link] as plt
from matplotlib import cm [Link](axis = 'x', b = False) #data
x = [1, 2, 3, 4, 5]
colors = [ cm.Dark2(x) for x in [Link](0, 1, len(x))] # Adding Annotations to place data labels h = [10, 8, 12, 5, 7]
for p in [Link]: c = ['red', 'yellow', 'black', 'blue', 'orange']
%matplotlib inline [Link](p.get_x()+p.get_width()/2,
#Instantiating figure and axes ax.get_ylim()[1]*0.01+ p.get_height(), #bar plot
fig, ax = [Link](figsize = (9,7), frameon = True) p.get_height(), [Link](x, height = h, color = c)
ha = 'center', size = 14 fig = [Link]()
t = [Link](x,y, color = colors) ) [Link]()

263 DT Ch ChCh Ch Ch Ch Ch
#Using CN color specification
color = ’C9’

#Using Named color in top


right for the facecolor or fc of
bars
color = ’salmon’
[Link](x,y, color = 'salmon')

Bar Chart Styling


color = ’C9’ #Using CN color specification from matplotlib import cm
ax.set_axisbelow(False) #To place grid lines (integral part of axes) above bars colors = [Link][1:]
[Link](axis = 'y', which = 'major',color = '0.75',b = True,ls = '-') [Link](x,y, color = colors)

#Alternatively Accessing colors from Palettable external library


[Link]
# [Link]
graph-elements
colors =[Link].Dark2_7.mpl_colors

264 DT Ch ChCh Ch Ch Ch Ch
'Class k': 0.903, #Setting the font size and colors for the value and text
'Class l': 0.873, labels
'Class m': 0.28, [[Link](color = 'white', fontsize = 15) for txt in pie_list[2]]
'Class n': 0.24, [[Link](color = 'black', fontsize = 12) for txt in pie_list[1]]
'Class o': 0.112}
#Adding a Legend. Note the transform object used
# group together all elements in the dictionary whose [Link](pie_list[0],labels, bbox_to_anchor=(1,0.9),
value is less than 2 loc="upper left", fontsize=10,
# name this group 'All the rest' bbox_transform=[Link]().transAxes)
import itertools
newdic={} #Alternate Legend
for key, group in [Link](dic, lambda k: 'All the #[Link](pie_list[0],labels, bbox_to_anchor=(1,1),
Back rest' if (dic[k]<2) else k): loc="upper right", fontsize=10,
newdic[key] = sum([dic[k] for k in list(group)]) bbox_transform=[Link]().transFigure)
Pie Charts
Code Start #Extracting labels and sizes from the dictionary plt.tight_layout()
#[Link] labels = [Link]()
/group-small-values-in-a-pie-chart sizes = [Link]() [Link]('[Link]',dpi=300, format='png’,
#Changes vis-à-vis the code in the link : bbox_inches='tight')
• Using colors from external library #Accessing colors from external library Palettable [Link]() #Also check link Code End
• Dictionary key value pairings from [Link] import Bold_10
• Placement of the legend and the corresponding colors = Bold_10.mpl_colors #The color code can be replaced by any of the other
transform object used colormaps as below
import [Link] as plt #Instantiating figure and axes
import numpy as np fig, ax = [Link]() #Accessed colors count same as number of values
[Link]() num = len([Link]()) #num for number of values
#Creating pie chart and assigning to variable colors = [ [Link].tab20c(x) for x in [Link](0, 1,num)]
dic = {'Class a': 26.9, pie_list = [Link](sizes, labels=labels, autopct='%1.1f%%', colors = [Link].Dark2(range(15))
'Class b': 18, explode=(0,0,0,0,0,0,0,.2), startangle=0, colors = [Link].tab20(range(15))
'Class c': 16.8, colors = colors) colors = [ [Link](x) for x in [Link](0, 1, num)]
'Class d': 13, #Note pie_list is a tuple containing list of wedges and text colors = [ [Link].Set3(x) for x in [Link](0, 1, num)]
'Class e': 9.83, colors = [Link]
'Class f': 9, #Set equal scaling of the axes (make circles circular) colors = [Link]
'Class g': 3.59, [Link]('equal’)
'Class h': 0.08, colors = [Link].nipy_spectral([Link](0.1,1,num))
'Class i': 1.42, #Setting the background color of figure to white colors = [Link]([Link](0.2,1,num))
'Class j': 1.09, [Link](facecolor = "white")

265 DT Ch ChCh Ch Ch Ch Ch
#Axes for CMRmap #Axes for Bold_10 colormap from palettable
color = [Link]([Link](0,0.9,len(data))) from [Link] import Bold_10
cc = [Link]("color", color)
with [Link]({"axes.prop_cycle" : cc}): color = Bold_10.mpl_colors
ax2 = fig.add_subplot(232, aspect="equal") cc = [Link]("color",Bold_10.mpl_colors)
[Link](data) with [Link]({"axes.prop_cycle" : cc}):
ax2.set_title('CMRmap', fontsize = 20, ax6 = fig.add_subplot(236, aspect="equal")
fontweight = 'medium’, y = 1, x= 0.5, [Link](data)
transform = [Link]) ax6.set_title('Bold_10', fontsize = 20,
#Axes for jet colormap fontweight = 'medium', y = 1, x= 0.5,
color = [Link]([Link](0,0.9,len(data))) transform = [Link])
cc = [Link]("color", color)
with [Link]({"axes.prop_cycle" : cc}): [Link]("Piecharts [Link]",dpi=300, format='png’,
ax3 = fig.add_subplot(233, aspect="equal") bbox_inches='tight')
[Link](data)
ax3.set_title('jet', fontsize = 20, fontweight = 'medium’, [Link]()
import palettable y = 1, x= 0.5, transform = [Link])
import [Link] as plt
Pie Chart Check the link :
import numpy as np colors #Axes for tab20 colormap [Link]
could-i-get-a-different-pie-chart-color
color = [Link].tab20([Link](0,1,len(data))))
#Creating random data for the pie chart cc = [Link]("color", Check answer by ImportanceOfBeingErnest
data = [Link](50,200, size=30) with [Link]({"axes.prop_cycle" : cc}): The code has been tweaked for six pie charts in this slide.
ax4 = fig.add_subplot(234, aspect="equal") There is scope to optimize the code with iteration through
#Instantiating figure [Link](data) a list of colors from six colormaps to draw the
fig = [Link](figsize = (10,7)) ax4.set_title('tab20', fontsize = 20, corresponding pie charts.
fontweight = 'medium', y = 1, x= 0.5,
#Axes for nipy_spectral colormap transform = [Link]) Further, check the discussion in the link. Currently
color = [Link].nipy_spectral([Link](0,.9,len(data))) maximum 20 colors available from the listed colormaps
cc = [Link]("color", color) #Axes for tab10 colormap under the qualitative colormaps list. Namely the
with [Link]({"axes.prop_cycle" : cc}): color = [Link].tab10([Link](0,1,len(data))) colormaps tab20, tab20b, tab20c.
ax1 = fig.add_subplot(231, aspect="equal") cc = [Link]("color", color)
[Link](data) with [Link]({"axes.prop_cycle" : cc}): However, more colors can be obtained from Linear
ax1.set_title('nipy_spectral', fontsize = 20, ax5 = fig.add_subplot(235, aspect="equal") Segmented Colormaps by using the methods in section
fontweight = 'medium', y = 1, x= 0.5, [Link](data) ACCESSING COLORS FROM A MATPLOTLIB
transform = [Link]) ax5.set_title('tab10', fontsize = 20, COLORMAP. Note depending on the colormap
fontweight 'medium', y = 1, x= 0.5, accessed, the colors extracted may look similar but having
transform = [Link]) different luminosity levels.

266 DT Ch ChCh Ch Ch Ch Ch
Pie Chart #Setting the linewidth, edge color of the wedges

Wedge #Necessary imports


import [Link] as plt
Properties import numpy as np

# Fixing random state for reproducibility


[Link](19680765)

ax = [Link](111)
wedges, texts = [Link]([Link]([Link](6)))

for w in wedges:
w.set_linewidth(2)
#[Link] w.set_edgecolor('cyan’)
line-properties-in-matplotlib-pie-chart
fig = [Link]
Three use case scenarios : [Link]('PieChart_linewidths.png',dpi=300,
ü Setting the width of the lines separating the pie wedges format='png')
ü Setting the facecolor or edgecolor of the wedges
ü Retrieving the wedges from [Link] if one has
an axes object but no direct access to the pie's wedges
# Retrieving wedges from axes object when no direct
Also check : access
• [Link]() method Matplotlib, Release 3.4.2, Pg No. 1286
• wedge properties in Matplotlib, Release 3.4.2, Pg No. wedges = [patch for patch in [Link] if \
2421-23 isinstance(patch, [Link])]

267 DT Ch ChCh Ch Ch Ch Ch
cell_width = 212 ax.set_xlim(0, cell_width * ncols)
cell_height = 22 ax.set_ylim(cell_height * (nrows-0.5), -cell_height/2.)
swatch_width = 48 [Link].set_visible(False)
margin = 12 [Link].set_visible(False)
topmargin = 40 ax.set_axis_off()
ax.set_title(title, fontsize=20, loc="left", pad=10)
p = colcount*rowcount
for i, name in enumerate(names[x*p:(x+1)*p]):
# Sort colors by hue, saturation, value and name row = i % nrows
if sort_colors is True: col = i // nrows
by_hsv = y = row * cell_height
sorted((tuple(mcolors.rgb_to_hsv(mcolors.to_rgb(color))),
name) swatch_start_x = cell_width * col
for name, color in [Link]()) text_pos_x = cell_width * col + swatch_width + 7
names = [name for hsv, name in by_hsv]
else: [Link](text_pos_x, y, name, fontsize=14,
names = list(colors) horizontalalignment='left',
Back verticalalignment='center')
n = len(names[x*p:(x+1)*p])
nrows = rowcount - emptyrows ax.add_patch(Rectangle(xy=(swatch_start_x, y-9),
#Creating user defined function to return a tuple – first
ncols = n // nrows + int(n % nrows > 0) width=swatch_width,
element being a figure containing color patches and
height=18, facecolor=colors[name],
second element being the figure name
width = cell_width * ncols + 2 * margin edgecolor='0.7'))
from [Link] import Rectangle height = cell_height * nrows + margin + topmargin
dpi = 72 filename = f'XKCD_Colors_{x+2}.png'
import [Link] as plt
return fig, filename
import [Link] as mcolors
fig, ax = [Link](figsize=(width / dpi, height / dpi),
dpi=dpi) #Creating the figure using the user defined function
def plot_colortable(colors, title, sort_colors=True,
emptyrows=0, x=0, colcount = 4 , rowcount = 30 ): fig.subplots_adjust(margin/width, margin/height, xkcd_fig, filename =
(width-margin)/width, plot_colortable(mcolors.XKCD_COLORS,
"""colors : any of the color tables from mcolors module
(height-topmargin)/height) "XKCD Colors",
title : title to be displayed in the figure
x = 7, rowcount = 30, colcount = 4)
sort_colors : Whether the colors are to be sorted
x : the iteration count
xkcd_fig.[Link](facecolor = 'white')
colcount : number of columns
rowcount : number of rows
XKCD Colors
"""

268 DT Ch ChCh Ch Ch Ch Ch
Back #Instantiating figure and axes
fig, axs = [Link](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 / [Link]()

# we need to normalize the data to 0..1 for the full range


of the colormap
norm = [Link]([Link](), [Link]())

# Now, we'll loop through our objects and set the color of
each accordingly
#[Link] for thisfrac, thispatch in zip(fracs, patches):
sphx-glr-gallery-statistics-hist-py color = [Link](norm(thisfrac))
thispatch.set_facecolor(color)
import [Link] as plt
import numpy as np # We can also normalize our inputs by the total number of
from matplotlib import colors counts
from [Link] import PercentFormatter axs[1].hist(x, bins=n_bins, density=True)

[Link]() # Now we format the y-axis to display percentage


axs[1].yaxis.set_major_formatter(PercentFormatter(xmax=
# Fixing random state for reproducibility 1))
[Link](19680801)
[Link](f'Histograms_colored.png',dpi=300,
N_points = 100000 format='png', bbox_inches='tight’)
n_bins = 20
[Link]()

# Generate a normal distribution, center at x=0 and y=5


x = [Link](N_points) Histogram
y = .4 * x + [Link](100000) + 5
Color Coded
269 DT Ch ChCh Ch Ch Ch Ch
Back
#[Link]
[Link]
import [Link] as plt
import numpy as np

[Link]()

# Create some normally distributed data


mean = [0, 0]
cov = [[1, 1], [1, 2]]

prng = [Link].default_rng(123456)
x, y = prng.multivariate_normal(mean, cov, 3000).T

# Set up the axes with gridspec


fig = [Link](figsize=(6, 6))
grid = [Link](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()

[Link]('Marginal [Link]', dpi = 300, transparent = True)

Marginal Histogram

270 DT Ch ChCh Ch Ch Ch Ch
#[Link] # Now plot each zone on a particular axis
using-pandas-matplotlib for i, zone in enumerate(zones):
import numpy as np [Link][[Link]==zone].hist(column='OS Usage',
import pandas as pd bins=[Link](0,1,10),
import [Link] as plt ax=axes[i],
sharey=True)
# Create some dummy data axes[i].set_title('OS Usage in {0}'.format(zone))
data = [Link]() axes[i].set_xlabel('Value')
zones = ["east","west","south","central"] axes[i].set_ylabel('Count')

data['zone']=[Link]([[zone]*[Link](10, 20) for zone in zones]) fig.tight_layout()


data['OS Usage']=[Link]([Link][0]) [Link](f'Barplots_using_for_loops_Pandas.png',dpi=300, format='png’,
bbox_inches='tight')
# Now create a figure
fig, axes = [Link](1,4, figsize=(12,3))
• Using for loop to create
Histograms histograms in multiple subplots

271 DT Ch ChCh Ch Ch Ch Ch
#[Link]

Soldier_years = [1850, 1851, 1852, 1853, 1854, 1855, 1856, 1857, 1858, 1859, 1860, 1861, 1862, 1863, 1864, 1865, 1866,
1867, 1868, 1869, 1870]
Seaborn point plot
num_records_yob = [7, 5, 8, 9, 15, 17, 23, 19, 52, 55, 73, 73, 107, 137, 65, 182, 228, 257, 477, 853, 2303] Adding data labels
%matplotlib inline
import seaborn as sns
import [Link] as plt
[Link](style="darkgrid")

f, (ax) = [Link](figsize=(12, 6), sharex=True)

sns.set_style("darkgrid")
ax = [Link](x=Soldier_years, y=num_records_yob)
[[Link](p[0], p[1]+50, p[1], color='g') for p in zip(ax.get_xticks(), num_records_yob)]

[Link](f'Datalabels_Seaborn_Pointplot.png',dpi=150, format='png', bbox_inches='tight')

272 DT Ch ChCh Ch Ch Ch Ch
#[Link]

import numpy as np
import [Link] as plt
import seaborn as sns
import pandas as pd

# create some random data; replace that by your actual dataset


data = [Link]([Link](11, 5), columns=['A', 'B', 'C', 'D', 'E'], index = range(2000, 2011, 1))

# plot heatmap
ax = [Link](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
[Link](f'Heatmap_Pandas_Dataframe.png',dpi=150, format='png', bbox_inches='tight')
[Link]()

Plotting Heat Map from Pandas


Pandas + Seaborn + Matplotlib

273 DT Ch ChCh Ch Ch Ch Ch
#[Link] #[Link] #[Link]
-plots-removing-axis-legends-and-white-spaces?rq=1 import [Link] as plt from numpy import random
import numpy as np import [Link] as plt
fig = [Link](figsize=(5, 5))
[Link].set_visible(False) # turn off the patch def make_image(data, outputname, size=(1, 1), dpi=80): data = [Link]((5,5))
fig = [Link]() img = [Link](data, interpolation='nearest')
[Link]([[0, 1], [0.5, 0.7]], interpolation="nearest", fig.set_size_inches(size) img.set_cmap('hot')
cmap='hot') ax = [Link](fig, [0., 0., 1., 1.]) [Link]('off')
[Link]('off') ax.set_axis_off() [Link]("[Link]", bbox_inches='tight', dpi = 300)
fig.add_axes(ax)
[Link]("[Link]", dpi = 300) plt.set_cmap('hot')
[Link](data, aspect='equal')
[Link](outputname, dpi=dpi)

[Link] # data = [Link](inputname)[:,:,0]


data = [Link](1,10).reshape((3, 3))

make_image(data, '[Link]', dpi = 300)

274 DT Ch ChCh Ch Ch Ch Ch
#[Link]
#plotting-two-heat-maps-side-by-side-in-matplotlib/

def corr_heatmaps(data1, data2, method='pearson'):

# Basic Configuration
fig, axes = [Link](ncols=2, figsize=(12, 12))
ax1, ax2 = axes
corr_matrix1 = [Link](method=method)
corr_matrix2 = [Link](method=method)
columns1 = corr_matrix1.columns
columns2 = corr_matrix2.columns
Back
# Heat maps.
im1 = [Link](corr_matrix1, cmap='coolwarm')
im2 = [Link](corr_matrix2, cmap='coolwarm')

# Formatting for heat map 1.


Code Start ax1.set_xticks(range(len(columns1)))
ax1.set_yticks(range(len(columns1)))
import numpy as np
import pandas as pd Plotting heat maps ax1.set_xticklabels(columns1)
import [Link] as plt From Pandas Data frame ax1.set_yticklabels(columns1)
ax1.set_title([Link], y=-0.1)
and Correlation Matrix
df = [Link]([Link](10, 10)) [Link](ax1.get_xticklabels(), rotation=45, ha='left', rotation_mode='anchor')
[Link](im1, fraction=0.045, pad=0.05, ax=ax1)
fig, axes = [Link](ncols=2, figsize=(8, 4)) Creating User
ax1, ax2 = axes
Defined Function # Formatting for heat map 2.
ax2.set_xticks(range(len(columns2)))
ax2.set_yticks(range(len(columns2)))
im1 = [Link]([Link](), cmap = 'YlGnBu') ax2.set_xticklabels(columns2)
im2 = [Link]([Link](), cmap = 'YlGn') ax2.set_yticklabels(columns2)
ax2.set_title([Link], y=-0.1)
[Link](im1, ax=ax1) [Link](ax2.get_xticklabels(), rotation=45, ha='left', rotation_mode='anchor')
[Link](im2, ax=ax2) [Link](im2, fraction=0.045, pad=0.05, ax=ax2)
[Link]('plotting_two_adjacent_heatmaps.png’,
dpi = 300, bbox_inches='tight') fig.tight_layout()
Code End

275 DT Ch ChCh Ch Ch Ch Ch
Seaborn horizontal Basic horizontal bar
bar plot plot horizontal bar plot

boxstyle: round Basic Secondary Axis twinx


roundingsize:.015 Gridlines above bars

Seaborn horizontal
bar plot
horizontal bar plot horizontal bar plot
boxstyle: round Secondary Axis twinx
roundingsize : 2 seaborn style
Only y gridlines Gridlines below bars

Seaborn horizontal
bar plot

• Create spacing
between bars horizontal bar plot
• Adding dummy
seaborn style
row in data
Only xgridlines
Axes.set_belowaxis(True)

Seaborn horizontal
bar plot horizontal bar plot
Connected dot plots
default style
• Create spacing Tick labels inside direction default style
between bars Padding for tick labels
• Resetting position Using Circle patches
of tick/ ticklabel/ Note on Practical Implementation of tick related methods

276 DT Ch ChCh Ch Ch Ch Ch
[Link](figsize=(6, 3))
sns.set_color_codes("pastel")

ax = [Link](x=[Link], y=[Link], orient='h', joinstyle='bevel')

new_patches = []
for patch in reversed([Link]):
bb = patch.get_bbox()
color = patch.get_facecolor()
p_bbox = FancyBboxPatch(([Link], [Link]),
abs([Link]), abs([Link]),
boxstyle="round,pad=-0.0040,rounding_size=0.015",
ec="none", fc=color,
mutation_aspect=4
)
#[Link] [Link]()
corners new_patches.append(p_bbox)
import pandas as pd
import [Link] as plt for patch in new_patches:
import seaborn as sns ax.add_patch(patch)
from [Link] import FancyBboxPatch
[Link](left=True, bottom=True)
[Link]("seaborn") ax.tick_params(axis=u'both', which=u'both', length=0) #Make ticklines zero length
mydict = { plt.tight_layout()
'Event': ['Running', 'Swimming', 'Biking', 'Hiking'],
'Completed': [2, 4, 3, 7], [Link](f'Seaborn bar [Link]',dpi=150, format='png', bbox_inches='tight')
'Participants': [10, 20, 35, 10]} [Link]()

df = [Link](mydict).set_index('Event')
df = [Link](Completion=([Link] / [Link]) * 100) Back to
print(df) Bar Charts List

277 DT Ch ChCh Ch Ch Ch Ch
[Link](figsize=(6, 3))
sns.set_color_codes("pastel")
ax = [Link](x=[Link], y=[Link], joinstyle='bevel')

new_patches = []
for patch in reversed([Link]):
# print([Link], [Link],abs([Link]), abs([Link]))
bb = patch.get_bbox()
color = patch.get_facecolor()
p_bbox = FancyBboxPatch(([Link], [Link]),
abs([Link]), abs([Link]),
boxstyle="round, pad=-0.0040, rounding_size=2",
ec="none", fc=color,
mutation_aspect=0.2
)
#[Link] [Link]()
corners new_patches.append(p_bbox)
import pandas as pd
import [Link] as plt for patch in new_patches:
import seaborn as sns ax.add_patch(patch)
from [Link] import FancyBboxPatch
[Link](left=True, bottom=True) #Removing all spines from plot
[Link]("seaborn") [Link](b = True, axis = 'x')
mydict = { ax.tick_params(axis=u'both', which=u'both', length=0) #Make ticklines zero length
'Event': ['Running', 'Swimming', 'Biking', 'Hiking'], plt.tight_layout()
'Completed': [2, 4, 3, 7],
'Participants': [10, 20, 35, 10]} [Link](f'Seaborn_bar_plot_rounded_corners.png',dpi=300, format='png’,
bbox_inches='tight')
df = [Link](mydict).set_index('Event') [Link]()
df = [Link](Completion=([Link] / [Link]) * 100)
print(df) Back to
Bar Charts List

278 DT Ch ChCh Ch Ch Ch Ch
# Hack of adding dummy row to create spacing between bars
import pandas as pd
import [Link] 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 = [Link](mydict).set_index('Event')
df = [Link](Completion=([Link] / [Link]) * 100)

[Link](figsize=(5, 4))

print([Link])
ax = [Link](x=[Link], y=[Link], color="orange", orient='h')

[Link](rotation=60). #Rotating x ticks by 60

plt.tight_layout()
[Link]('Matplotlib Spacing between bars_dummy [Link]',dpi=150,
bbox_inches='tight')
[Link]()

#[Link] Back to
between-bars Bar Charts List
#Matplotlib Control Spacing Between Bars

279 DT Ch ChCh Ch Ch Ch Ch
Back to Bar Charts List
[Link]('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 = [Link](ax.get_yticks()).astype(float)
# iterate over the boxes/ticks/tick label and change the y position
for i, (patch, label) in enumerate(zip([Link], 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:
#[Link] print(f'ytick positions after: {ax.get_yticks()} \nytick labels after: {ax.get_yticklabels()}’)
between-bars #[Link].set_major_locator([Link](ticks_position))
#Matplotlib Control Spacing Between Bars ax.set_yticks(ticks_position)
import pandas as pd
import [Link] as plt ax.set_ylim([ax.get_ylim()[0]+extra_space, ax.get_ylim()[1]])
import seaborn as sns print(f'ytick positions after: {ax.get_yticks()} \nytick labels after: {ax.get_yticklabels()}’)
import [Link] as ticker [Link].set_major_formatter([Link](label_list))

mydict = { fig, ax = [Link](figsize = (7,4))


'Event': ['Running', 'Swimming', 'Biking', 'Hiking', 'Jogging'], [Link](x=[Link], y=[Link], color="orange", ax = ax, orient='h' )
'Completed': [2, 4, 3, 7, 9], [Link](rotation=60) #Rotating x ticks by 60
'Participants': [10, 20, 35, 10, 20]} plt.tight_layout()
df = [Link](mydict).set_index('Event')
df = [Link](Completion=([Link] / [Link]) * 100) #use the function
add_space_after(ax, 'Hiking', 0.6)
#note: no more blank row [Link](f'Matplotlib Spacing between [Link]',dpi=150, bbox_inches='tight')

280 DT Ch ChCh Ch Ch Ch Ch
#[Link]
#[Link]
#[Link]

[Link]()
fig, ax = [Link]()

# Example data
people = ('Tom', 'Dick', 'Harry', 'Slim', 'Jim')
y_pos = [Link](len(people))
performance = 3 + 10 * [Link](len(people))
error = [Link](len(people))

[Link](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?')

[Link]('[Link]', dpi = 300, bbox_inches='tight')


[Link]()

Back to
Bar Charts List

281 DT Ch ChCh Ch Ch Ch Ch
#[Link]
#[Link]
#[Link]

import [Link] as plt


import pandas as pd
import numpy as np

[Link](123456) # Fixing random state for reproducibility


[Link]('seaborn')

fig, ax = [Link](figsize = (6,4))

# Example data
people = ('Tom', 'Dick', 'Harry', 'Slim', 'Jim')
y_pos = [Link](len(people))
performance = 3 + 10 * [Link](len(people))
error = [Link](len(people))

[Link](y_pos, performance, xerr=error, align='center', color='green',alpha = 0.8, ecolor='black')


ax.set_yticks(y_pos)
Note on Practical Implementation of tick related methods ax.set_yticklabels(people, ha = 'right')
ax.invert_yaxis() # labels read top-to-bottom
Back to ax.set_xlabel('Performance')
Bar Charts List ax.set_title('How fast do you want to go today?’)

[[Link](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

[Link](axis = 'y', b = True). #Setting on gridlines from x axis


[Link](axis = 'x', b = False) #Setting off gridlines from x axis

[Link]('SeabornHorizontalBarplot_textbold.png', dpi = 300, bbox_inches='tight')


[Link]()

282 DT Ch ChCh Ch Ch Ch Ch
Back to Bar Charts List
#[Link]
#[Link]
#[Link]

import [Link] as plt


import pandas as pd
import numpy as np

[Link](123456) # Fixing random state for reproducibility


[Link]('seaborn')

fig, ax = [Link](figsize = (6,4))

# Example data
people = ('Tom', 'Dick', 'Harry', 'Slim', 'Jim')
y_pos = [Link](len(people))
performance = 3 + 10 * [Link](len(people))
error = [Link](len(people))

[Link](y_pos, performance, xerr=error, align='center’, color='green',alpha = 0.8, ecolor='black')


ax.set_yticks(y_pos)
#Creating a User Defined function ax.set_yticklabels(people, ha = 'right')
ax.invert_yaxis() # labels read top-to-bottom
def stylize_axes(ax): ax.set_xlabel('Performance')
ax.set_facecolor('.8') ax.set_title('How fast do you want to go today?')
ax.tick_params(labelsize=10, length=0) [[Link](fontweight = 'semibold') for tkl in ax.get_yticklabels()] #Making ticklabels semi bold
[Link](True, axis='x', color='white') ax.tick_params(axis = 'y', labelcolor = 'black’) #Setting ticklabel size and color
ax.set_axisbelow(True) #Ticks and gridlines below other artists
[spine.set_visible(False) for spine in [Link]()] [Link](axis = 'y', b = False)
stylize_axes(ax). # Using the user defined function stylize_axes

[Link]('SeabornHorizontalBarplot_textbold.png', dpi = 300, bbox_inches='tight')


[Link]()

283 DT Ch ChCh Ch Ch Ch Ch
Back to Bar Charts List
#[Link]
#[Link]
#[Link]

import [Link] as plt


import pandas as pd
import numpy as np

[Link](123456)
[Link]()

fig, ax = [Link](figsize = (6,4))

# Example data
people = ('Tom', 'Dick', 'Harry', 'Slim', 'Jim')
y_pos = [Link](len(people))
performance = 3 + 10 * [Link](len(people))
error = [Link](len(people))

[Link]["[Link]"] = 'line’ #tick and gridlines above other artists except lines
#Can use the below line as well instead of above
#ax.set_axisbelow(False)
• [Link]
ght=zorder [Link](y_pos, performance, xerr=error, align='center’, color='green', ecolor='black’)
• [Link] ax.invert_yaxis() # labels read top-to-bottom
_axisbelow.html?highlight=axisbelow#[Link].set_axi ax.set_xlabel('Performance')
sbelow ax.set_title('How fast do you want to go today?’)
• [Link]
higher-z-index-of-the-axis-labels ax.set_yticks(y_pos)
ax.set_yticklabels(people, horizontalalignment = 'left', fontweight = 'medium’) #ha is important
Axes.set_axisbelow(self, b) : Set whether axis ticks and gridlines ax.tick_params(axis = 'y', pad = -20, labelcolor = 'white’) #pad as -20 for ticklabels inside
are above or below most artists. This controls the zorder of the
[Link]()
ticks and gridlines.

284 DT Ch ChCh Ch Ch Ch Ch
Back to Bar Charts List
#[Link]
# creating-alternative-y-axis-labels-for-a-horizontal-bar-plot-with-matplotlib/52171704
#Check answer by Sheldore

import [Link] as plt


[Link]()

fig, ax = [Link](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']
[Link](y,x)
[Link]('Percent Completion Rate of Each Course')
ax = [Link]()
[Link](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 = [Link]()
Creating alternative y axis labels ax_lim = ax.get_ylim()
ax2_lim = (ax_lim[0], ax_lim[1]) # Aligning the limits of both y axes
for a horizontal bar plot with matplotlib ax2.set_ylim(ax2_lim)
ax2.set_yticks(range(0, len(y)))
• [Link] ax2.set_yticklabels(alternativeYlabels[::-1]) # Reverses the list
ax2.set_ylabel("# enrolled in each course",rotation = 270, labelpad=15)
• (grid lines above bars)
[Link]('SecondaryAxisBarplot_2.png', dpi = 300, bbox_inches='tight')
[Link]()

285 DT Ch ChCh Ch Ch Ch Ch
Back to Bar Charts List
#[Link]
#creating-alternative-y-axis-labels-for-a-horizontal-bar-plot-with-matplotlib/52171704
#Check answer by Sheldore

import [Link] as plt


[Link]()

fig, ax = [Link](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']
[Link](y,x)
[Link]('Percent Completion Rate of Each Course')
ax = [Link]()

[Link](True) # adding vertical grid lines


[Link](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


Creating alternative y axis labels ax2 = [Link]()
ax_lim = ax.get_ylim()
for a horizontal bar plot with matplotlib ax2_lim = (ax_lim[0], ax_lim[1]) # Aligning the limits of both y axes
ax2.set_ylim(ax2_lim)
• [Link] ax2.set_yticks(range(0, len(y)))
ax2.set_yticklabels(alternativeYlabels[::-1]) # Reverses the list
• (grid lines below bars) ax2.set_ylabel("# enrolled in each course",rotation = 270, labelpad=15)

[Link]('SecondaryAxisBarplot_2.png', dpi = 300, bbox_inches='tight')


[Link]()

286 DT Ch ChCh Ch Ch Ch Ch
Back to Bar Charts List
#[Link]
ow_to_make_this/
import [Link] as plt
import numpy as np
import [Link] as mpatches

# Fixing random state for reproducibility


[Link](19680801)

[Link]()
fig, ax = [Link]()

# Example data
people = ('Tom', 'Dick', 'Harry', 'Slim', 'Jim','John')
y_pos = [Link](len(people))
performance = 3 + 10 * [Link](len(people))
[Link]() error = [Link](len(people))
start = 3+[Link](len(people))
Cleveland Dot plots Using Circle Patches
[Link](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?')
[Link](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 = [Link]((x,y),0.2,fc='red’) #Adding red circles for start position
ax.add_patch(circ)
circ = [Link]((x+x1,y),0.2,fc='blue’) #Adding blue circles for start position
ax.add_patch(circ)
Using seaborn plot style, code line : [Link]('scaled')
[Link](‘seaborn’)
[Link]('Different Styles_Dot_Plot.png', dpi = 300, format = 'png',bbox_inches='tight')
[Link]()

287 DT Ch ChCh Ch Ch Ch Ch
Cleveland Dot plots
Using Circle Patches
Multiple Plot Styles

288 DT Ch ChCh Ch Ch Ch Ch
# Example data
people = ('Tom', 'Dick', 'Harry', 'Slim', 'Jim','John')
y_pos = [Link](len(people))
performance = 3 + 10 * [Link](len(people))
error = [Link](len(people))
start = 3+[Link](len(people))

fig = [Link](figsize = (25,9))

for i,ax in enumerate(styles):


with [Link](styles[i]):
print(styles[i])
#[Link] ax = fig.add_subplot(2,3,i+1)
e_or_tips_for_how_to_make_this/ [Link](y_pos, performance, left=start, height=0.03, align='center')
import [Link] as plt ax.set_yticks(y_pos)
from [Link] import Circle ax.set_yticklabels(people,**font_tick) #Setting yticklabel props using ** unpacking
import numpy as np [[Link](**font_tick) for tkl in ax.get_xticklabels()] #Setting xticklabel props using ** unpacking
ax.invert_yaxis() # labels read top-to-bottom
# Fixing random state for reproducibility ax.set_xlabel('Performance', **font_text)
[Link](19680801) ax.set_title('How fast do you want to go today?', loc = 'left', fontdict = font_title)
#[Link](b=True)
#Listing the styles for plotting with for loop for x,y,x1 in zip(start, y_pos, performance): #Adding Circle Patches
styles = ['bmh', 'Solarize_Light2','seaborn', 'fast', circ = Circle((x,y),0.2,fc='red', transform = [Link])
'seaborn-darkgrid','seaborn-whitegrid’] ax.add_patch(circ)
circ = Circle((x+x1,y),0.2,fc='blue',transform = [Link])
#Creating font dicts for different use cases ax.add_patch(circ)
font_text = {'family': 'EB Garamond', [Link]('scaled')
'color': 'darkred', 'weight': 'normal’,'size': 16} #for label plot style
#Adding name of the plot style for the subplot within fancy bbox
font_title = {'family': 'Dejavu Sans', [Link](0.99,1.05,s = f'{styles[i]}',ha = 'right',transform = [Link], **font_text,
'color': 'darkred', 'weight': 'normal','size': 14} #for axes title bbox=dict(facecolor='lightyellow', edgecolor='green', boxstyle='round',pad =.3))

font_tick = {'family': 'Dejavu Sans', plt.subplots_adjust(wspace = .2, hspace = .1) #Adjust horizontal & vertical space btw subplots
'color': 'darkred’, 'weight': 'normal’, 'size': 12} #for tick labels [Link]('Different Styles_Dot_Plot_fancybox_text.png', dpi = 300, bbox_inches='tight')

289 DT Ch ChCh Ch Ch Ch Ch
# libraries
import numpy as np
import pandas as pd
import [Link] as plt

[Link]()
# Create a dataframe
value1=[Link](size=20)
value2=value1+[Link](size=20)/4
df = [Link]({'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([Link])+1)

fig, ax = [Link](figsize = (8,6))


# The horizontal plot is made using the hline function
[Link](y=my_range, xmin=ordered_df['value1'], xmax=ordered_df['value2'], color='grey’,
alpha=0.4)
[Link](ordered_df['value1'], my_range, color='skyblue', alpha=1, label='value1', s = 90)
[Link](ordered_df['value2'], my_range, color='green', alpha=0.4 , label='value2', s= 90)
[Link]()

#[Link] # Add title and axis names


Changes done in the code in link : ax.set_yticks(my_range)
ax.set_yticklabels(ordered_df['group'])
• Changed pyplot interface style to OO style
ax.set_title("Comparison of the value 1 and the value 2", loc='left')
• Configured the grid lines to dashed line style, set_axisbelow to put ax.set_xlabel('Value of the variables')
ticks/gridlines below other artists ax.set_ylabel('Group')
• Changed the scatter marker size to 90
# Configuring the grid lines
[Link](True, alpha = 0.6, ls = 'dashed')
Cleveland • Using Axes Methods ax.set_axisbelow(True)
Dot plots • [Link] and [Link] [Link]('lollipop_charts.png', dpi = 300, format = 'png',bbox_inches='tight')
[Link]() #Show the graph

290 DT Ch ChCh Ch Ch Ch Ch
Cleveland Dot plots Using matplotlib in Python

291 DT Ch ChCh Ch Ch Ch Ch
Cleveland Dot plots Using matplotlib in Python Spines Removed and tick/gridlines above artists

292 DT Ch ChCh Ch Ch Ch Ch
Code Start

# libraries
import numpy as np
import pandas as pd
import [Link] as plt
import [Link] as transforms
import seaborn as sns
import io
[Link](style="whitegrid") # set style
#[Link]('seaborn-whitegrid')

data = [Link](""""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
#[Link]
"Australia/New Zealand" 123.6 170.9
this-plot-that-has-rows-with-two-connected-dots/423861
"Brazil" 208.5 199.8
#[Link] #Dot plots in Excel "Japan" 181.0 216.7
#[Link] "Africa" 185.4 222.0
"Other non-OECD Asia" 202.7 236.0
"OECD Europe" 173.8 239.9
Cleveland • Using Axes Methods "Other non-OECD Americas" 193.1 242.3
"India" 173.8 260.6
Dot plots • [Link] and [Link] "Mexico/Chile" 221.1 269.8""")

Code snippet 1 of 2 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}


Code continues to Next slide

293 DT Ch ChCh Ch Ch Ch Ch
Code continues from Prev slide

fig = [Link](figsize=(13,5))
ax = fig.add_subplot()
y_range = [Link](1, len([Link]) + 1)
colors = [Link](df['2015'] > df['1990'], '#d9d9d9', '#d57883')
[Link](y=y_range, xmin=df['1990'], xmax=df['2015'],
color=colors, lw=5)
[Link](df['1990'], y_range, color='#0096d7', s=150, label='1990', zorder=3)
[Link](df['2015'], y_range, color='#003953', s=150 , label='2015', zorder=3)
for (_, row), y in zip([Link](), y_range):
[Link](f"{row['change']:+.0%}", (max(row["1990"], row["2015"]) + 6, y - 0.25))

[Link](True,alpha = 0.6) #Make grid lines lighter


ax.set_axisbelow(False)

# Make all spines visible


[Link]['right'].set(visible = True)
[Link]['top'].set(visible =True)
[Link]['left'].set(visible =True)
#[Link]
[Link]['right'].set(visible =True)
this-plot-that-has-rows-with-two-connected-dots/423861
#[Link] #Dot plots in Excel ax.set_yticks(y_range)
#[Link] ax.set_yticklabels([Link])

[Link](ncol=2, bbox_to_anchor=(1, 1), loc="lower right", frameon=True )


Cleveland • Using Axes Methods # the x coords of this transformation are data, and the y coord are figure
trans = transforms.blended_transform_factory([Link], [Link])
Dot plots • [Link] and [Link] [Link]("Energy productivity in selected countries and regions,1990 and 2015", **font_suptitle,
x = 50,y = 1, transform = trans,
Code snippet 2 of 2 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)


[Link]('connected dot plots_sample2.png', dpi = 300, format = 'png’, bbox_inches='tight')
[Link]()

Code End
294 DT Ch ChCh Ch Ch Ch Ch
Cleveland Dot plots Using ggplot2 in R

295 DT Ch ChCh Ch Ch Ch Ch
# 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') +
#[Link] geom_point(data = dat_long, aes(x = Energy_productivity, y = Country, colour =
this-plot-that-has-rows-with-two-connected-dots/423861 Year), size = 4) +
library(dplyr) # for data manipulation labs(title = 'Energy productivity in selected countries \nand regions', subtitle =
library(tidyr) # for reshaping the data frame 'Billion dollars GDP per quadrillion BTU', caption = 'Source: EIA, 2016', x = NULL, y
library(stringr) # string manipulation = NULL) +
library(ggplot2) # graphing scale_colour_manual(values = c('#1082CD', '#042B41')) +
theme_bw() +
theme([Link] = c(0.92, 0.20), [Link] = element_blank(),
[Link] = element_rect(colour = 'black'), [Link] =
Cleveland element_blank(), [Link] = element_line(colour = '#E6E6E6’))
• Using ggplot2 in R
Dot plots ggsave('[Link]', width = 20, height = 10, units = 'cm')

296 DT Ch ChCh Ch Ch Ch Ch
#[Link]
#formatting-date-labels-using-seaborn-facetgrid
#Check answer by Diziet Asahi
import pandas as pd
import numpy as np
import seaborn as sns
import [Link] as plt
from datetime import datetime
import [Link] 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 = [Link](range(10, 30), 12)
df = [Link]({'date': datelist, 'value': vallist, 'variable': varlist,
'department': deptlist})

g = [Link](df, row="department", col="variable", sharey='row')


g = [Link]([Link], "date", "value", marker='o', markersize=0.7)

xformatter = [Link]("%m/%d")
[Link][0,0].xaxis.set_major_formatter(xformatter)
[Link][0,0].[Link](f’Date labels Seaborn [Link]',dpi=300, format='png’,
bbox_inches='tight')
[Link]()
• pd.date_range() to generate
Seaborn datetimeindex
Facet Grids • DateFormatter for formatting xtick
labels
Note on Practical Implementation of tick related methods

297 DT Ch ChCh Ch Ch Ch Ch
#[Link]
#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 [Link] as plt
import [Link] as mdates

[Link]('seaborn')
idx = pd.date_range('2011-05-01', '2011-07-01')

prng = [Link].default_rng(123456)
s = [Link](prng.standard_normal(len(idx)), index=idx)

fig, ax = [Link]()

ax.plot_date(idx.to_pydatetime(), s, 'v-') #x values converted to python datetime objects

[Link].set_minor_locator([Link](byweekday=(1),interval=1))
[Link].set_minor_formatter([Link]('%d\n%a'))

[Link](True, which="minor")
[Link]()

[Link].set_major_locator([Link]())
• pd.date_range() to generate datetime [Link].set_major_formatter([Link]('\n\n\n%b\n%Y'))

dates •
index
MonthLocator() for major ticks
plt.tight_layout()
[Link]('Major and Minor date ticks in [Link]',dpi=300, format='png’,
module • WeekdayLocator() for minor ticks bbox_inches='tight')

• Date Formatter
Note on Practical Implementation of tick related methods

298 DT Ch ChCh Ch Ch Ch Ch
import [Link] as plt
from [Link] import MonthLocator, YearLocator
import numpy as np

[Link]()
fig, ax = [Link](1, 1, figsize = (10,5))

#Using [Link] to create a range of datetime64 values


x = [Link]('2010-01', '2021-07', dtype='datetime64[D]')

# [Link](12345)
#y = [Link]([Link]).cumsum()

# Using the generator [Link].default_rng to generate values from standard normal


rng = [Link].default_rng(12345)
y = rng.standard_normal([Link]).cumsum()
[Link](x, y)

yloc = YearLocator()
mloc = MonthLocator()
[Link].set_major_locator(yloc)
[Link].set_minor_locator(mloc)

[Link].set_tick_params(rotation = 30)
[tkl.set_ha('right') for tkl in ax.get_xticklabels()]
[Link](True)

• Using [Link] to generate array of savekargs = dict(dpi = 300, transparent = True, pad_inches = 0.5, bbox_inches = 'tight')
Numpy datetime64[D] values [Link]('Numpy_datetime64_plotting.png', **savekargs)
datetime64 • Using generator [Link].default_range
• YearLocator and MonthLocator Classes
Note on Practical Implementation of tick related methods

299 DT Ch ChCh Ch Ch Ch Ch
#[Link]
import pandas as pd
import numpy as np
import [Link] as plt
import [Link] as mdates

style = ['fivethirtyeight', 'seaborn', 'ggplot', 'default']


[Link](style[3])

idx = pd.date_range('2017-01-01 05:03', '2017-01-01 18:03', freq = 'min')


df = [Link]([Link](len(idx)), index = idx)

fig, ax = [Link](figsize = (10,5))


hours = [Link](interval = 1)
h_fmt = [Link]('%H:%M:%S')

[Link]([Link], [Link], color = 'black', linewidth = 0.4)

#or use
#[Link](ax = ax, color = 'black', linewidth = 0.4, x_compat=True, figsize = (10,5))
#Then tick and format with matplotlib:
[Link].set_major_locator(hours)
[Link].set_major_formatter(h_fmt)

fig.autofmt_xdate()
savekargs = dict(dpi = 300, transparent = True, pad_inches = 0.5, bbox_inches = 'tight')
• Suppressing Tick Resolution Adjustment by [Link]('datetimeindex_plotting.png', **savekargs)
Pandas Pandas plotting [Link]()
Datetime
Tseries • x_compat = True to apply [Link]
index
Plotting locator/formatter
• Ticks at 1 hour interval
Note on Practical Implementation of tick related methods

300 DT Ch ChCh Ch Ch Ch Ch
#[Link]
#force-pandas-xaxis-datetime-index-using-a-specific-format
import pandas as pd
from datetime import datetime
import numpy as np
import [Link] as plt
import [Link] 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': [Link](len(timeInd)), 'data2': [Link](len(timeInd)),
'data3': [Link](len(timeInd))}
df = [Link](data = d, index = timeInd)

[Link]('ggplot')
ax0 = [Link](subplots=True, grid=True, x_compat=True, figsize = (10,6))

ax = [Link]()
# set major ticks location every day
[Link].set_major_locator([Link]())
# set major ticks format
[Link].set_major_formatter([Link]('\n\n\n%d.%m.%Y'))
# set minor ticks location every two hours
[Link].set_minor_locator([Link](interval=2))
# set minor ticks format
[Link].set_minor_formatter([Link]('%H:%M:%S'))
• Suppressing Tick Resolution Adjustment by Pandas
Pandas plotting
for ax in ax0:
[Link](loc = 'upper right')
Tseries • x_compat = True to apply [Link] [Link](visible = False)
locator/formatter
Plotting • Ticks at 2 hour interval
# or just set together date and time for major ticks like
# [Link].set_major_formatter([Link]('%d.%m.%Y %H:%M:%S'))
[Link]('Pandasplotting_datetime_xcompat.png', dpi = 300, pad_inches = 0.02)
Note on Practical Implementation of tick related methods
[Link]()

301 DT Ch ChCh Ch Ch Ch Ch
import [Link] as mdates
import [Link] as plt
import datetime as dt

rng = [Link].default_rng(12345)
dtindex=pd.date_range(start='2020-01-01',end='2020-12-31',freq='M')
df = [Link]({'values':[Link](0,1000, len(dtindex))}, index= dtindex)

fig,ax1 = [Link]()
[Link]([Link],[Link])

[Link].set_major_locator([Link]())

monthyearFmt = [Link]('%Y\n%b')
[Link].set_major_formatter(monthyearFmt)

#Underscore and assignment so that tick locs and labels are not shown in output
_ = [Link](ha = 'center’)
[Link](axis = 'x')

x1 = [Link](2020,0o5,0o1)
x2 = [Link](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,
• pd.date_range() color = 'steelblue' )
• [Link]
TimeSeries • [Link].default_rng(12345) x3 = [Link](2020,9,0o1)
fill_betweenx() x4 = [Link](2020,0o10,0o1)
Plotting •
[Link](xmin = x3, xmax = x4, color = 'salmon', alpha = 0.8)
• Axvspan()
• MonthLocator()
[Link]('Simple TimeSeries [Link]', dpi = 300, transparent = True)
Note on Practical Implementation of tick related methods

302 DT Ch ChCh Ch Ch Ch Ch
import [Link] as plt
import [Link] 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 = [Link].default_rng(123456)
utilization = [[Link](10, 50) for _ in range(len(dates))]
df = [Link]({'dates': dates, 'util': utilization})
df.set_index('dates', inplace=True)

# resample dataframe to daily


df_daily = [Link](df['util'].resample('D').sum())

#plot the dataframe


[Link]('seaborn-whitegrid')
fig = [Link](figsize=(10, 6))
ax = fig.add_subplot(111)
[Link](df_daily.index, df_daily['util'])
[Link].set_major_formatter([Link]('%Y-%m'))

[Link]('[Link]', dpi = 300, transparent = True)


[Link]()

Datetime
Time series
• Resampling the dataframe to Day frequency
index
Plotting
Note on Practical Implementation of tick related methods
[Link]

303 DT Ch ChCh Ch Ch Ch Ch
import pandas as pd
import numpy as np
import [Link] as plt
import matplotlib as mpl
import seaborn as sns

[Link](123456)
perf = [Link](1+[Link](size=250)/100)
df = [Link]({'underwater': perf/[Link](perf)-1})

sns.set_context('notebook')
sns.set_style('ticks', rc = {'[Link]': True, '[Link]': True,
'[Link]': False, '[Link]': True,
'[Link]': False})

#Assigning current axes to ax


ax = [Link]()
[Link](ax=ax, label = 'underwater')
[Link].set_ticklabels([])
ax.set_ylim(ax.get_ylim()[0],0)

#[Link] #Removing spines at bottom and right


#Check answer by Sheldore [Link](left=False, bottom=True, right=True, top = False, ax=ax)
#Matplotlib x-axis at the top, no x axis label, no major tick labels, but outer
major and minor tick wanted #Adding legend with frameon
[Link](loc = 'upper right', bbox_to_anchor = (0.95,0.95),
bbox_transform = [Link], frameon = True )
Pandas
• [Link] # Adding red ticks in x axis pointing outwards
Seaborn
• Adding red ticks in out direction ax.tick_params('x', length=8, width=2, color='red', which='major', direction='out')
Matplotlib
Note on Practical Implementation of tick related methods [Link]('Major_ticks_Outer_No_AxisLabel.png',dpi=300, format='png',
bbox_inches='tight')

304 DT Ch ChCh Ch Ch Ch Ch
#[Link]

import [Link] as plt

[Link]()
fig, ax = [Link]()

[Link]([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))

[Link]('Highlight selected [Link]', dpi = 300, format = 'png',bbox_inches='tight’)


[Link]()

boxstyle types
Class Name Attrs
Circle circle pad=0.3
DArrow darrow pad=0.3
LArrow larrow pad=0.3
Format select • Indexing/slicing axis tick labels RArrow rarrow pad=0.3
few tick labels • Setting the bbox for text artist Round round pad=0.3, rounding_size=None
Round4 round4 pad=0.3, rounding_size=None
[Link] Roundtooth roundtooth pad=0.3, tooth_size=None
Sawtooth sawtooth pad=0.3, tooth_size=None
FancyBboxPatch Demo Square square pad=0.3

Note on Practical Implementation of tick related methods

305 DT Ch ChCh Ch Ch Ch Ch
#[Link]
#The above link contains only one tick formatted. The example is extended here for different
possible use-cases
[Link]()
fig, ax = [Link]()
[Link]([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
[Link](bbox = bbox_dict, **fontargs)

[Link].set_tick_params(pad = 10) #Increasing the padding between tick and labels


[Link]('Highlight selected ticks_2.png', dpi = 300,bbox_inches='tight', pad_inches = 0.3)
[Link]()

boxstyle types
Class Name Attrs
Circle circle pad=0.3
• Iterate over axis tick labels
Format all tick DArrow darrow pad=0.3
• Setting the bbox for text artist LArrow larrow pad=0.3
labels
• Set font properties RArrow rarrow pad=0.3
Round round pad=0.3, rounding_size=None
[Link] Round4 round4 pad=0.3, rounding_size=None
Note on Practical Implementation of tick related methods
Roundtooth roundtooth pad=0.3, tooth_size=None
Sawtooth sawtooth pad=0.3, tooth_size=None
Square square pad=0.3

306 DT Ch ChCh Ch Ch Ch Ch
import [Link] as plt
from matplotlib import cm
from [Link] import ListedColormap
from [Link] import LinearSegmentedColormap
from itertools import cycle, islice #Using iterator to cycle colors and boxstyle
import [Link] as mpatch

[Link]()
fig, ax = [Link]()
[Link]([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([Link].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)([Link](0,0.6,10)))
#Using islice to always start from first position
color_list = islice(cycle([Link]), 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)
• [Link]
• Formatting only selected tick labels [Link].set_tick_params(pad = 10) #Ensuring tick labels have sufficient gap from axis
for tkl in ax.get_xticklabels():
• specify-where-to-start-in-an-itertools-cycle-function [Link](bbox = {**bboxargs, 'boxstyle' : next(boxstyle_list), 'facecolor':next(color_list)},
• fancybox_demo **fontargs )
• add-new-keys-to-a-dictionary
[Link]('Highlight selected ticks_3.png', dpi = 300, format ='png’,bbox_inches='tight’,
• Use cycle iterator object with islice pad_inches = 0.3)
Format all tick • Set the bbox style with cycle [Link]()
• Create listedcolormap from colormap and
labels use that within cycle object to assign color
to ticklabels
Note on Practical Implementation of tick related methods
307 DT Ch ChCh Ch Ch Ch Ch
import [Link] as plt
import [Link] as mtransforms
import [Link] as mpatch
from [Link] import FancyBboxPatch

styles = [Link].get_styles()
spacing = 1.2
figheight = (spacing * len(styles) + .5)
fig = [Link](figsize=(4 / 1.5, figheight / 1.5))
fontsize = 0.3 * 72
for i, stylename in enumerate(sorted(styles)):
[Link](0.5, (spacing * (len(styles) - i) - 0.5) / figheight,
stylename, ha="center", size=fontsize,
bbox=dict(boxstyle=stylename, fc="w", ec="k"))

boxstyle types
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
Patches • FancyBboxPatch Square square pad=0.3

• [Link]
• fancybox_demo

308 DT Ch ChCh Ch Ch Ch Ch
import pandas as pd
import numpy as np
import [Link] as plt

[Link](123456)
a = [Link](5,15, size=10)
b = [Link](5,15, size=10)

df = [Link]({"a":a})
df2 = [Link]({"b":b})

fig, (ax, ax2) = [Link](ncols=2, sharey=True, figsize = (10,5) )

ax.invert_xaxis()
[Link].tick_right()

df["a"].plot(kind='barh', x='LABEL', legend=False, ax=ax)


df2["b"].plot(kind='barh', x='LABEL',ax=ax2)

[Link].set_visible(False)
[Link].set_visible(False)
Adjacent [Link].set_visible(False)
• Pandas DataFrame + Matplotlib
Barh charts [Link]('Two barh [Link]', dpi = 300)
[Link]()
• [Link]
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

309 DT Ch ChCh Ch Ch Ch Ch
Back font_color = '#525252' axes[1].set_xticks([1000, 2000, 3000, 4000, 5000, 6000,
hfont = {'fontname':'Calibri’} # dict for unpacking later 7000, 8000])
facecolor = '#eaeaf2' axes[1].yaxis.set_tick_params(size=0)
color_red = '#fd625e'
color_blue = '#01b8aa' axes[1].set_xticklabels([1000, 2000, 3000, 4000, 5000, 6000,
index = [Link] 7000,8000])
column0 = data['Expenditures']
column1 = data['Revenues'] for label in (axes[0].get_xticklabels() +axes[0].get_yticklabels()):
title0 = 'Expenditures, USD thousand' [Link](fontsize=13, color=font_color, **hfont)
title1 = 'Revenues, USD thousand’ for label in (axes[1].get_xticklabels() +axes[1].get_yticklabels()):
[Link](fontsize=13, color=font_color, **hfont)
import [Link] as plt
[Link]() [Link](facecolor = 'white’)
#wspace=0 for no gap between the two axes
Bi Directional Charts fig, axes = [Link](figsize=(10,5), facecolor=facecolor, plt.subplots_adjust(wspace=0, top=0.85, bottom=0.1,
ncols=2, sharey=True) left=0.18, right=0.95)
fig.tight_layout() [[Link](True) for ax in [Link]]
#[Link] filename = 'mpl-bidirectional2'
bidirectional
axes[0].barh(index, column0, align='center’, [Link](filename+'.png', dpi=300, format='png’,
import requests
color=color_red, zorder=10) bbox_inches='tight')
import pandas as pd axes[0].set_title(title0, fontsize=18, pad=15, color= \
color_red, fontweight = 'medium', **hfont)
url = The final data frame used for the plot after data aggregation
axes[1].barh(index, column1, align='center’,
'[Link] and manipulation is as below (can be used directly) :
color=color_blue, zorder=10)
nment_budget'
axes[1].set_title(title1, fontsize=18, pad=15, color= \
response = [Link](url) color_blue, fontweight = 'medium', **hfont)
Country Revenues Expenditures
tables = pd.read_html([Link]) United States 5923.829 -9818.53
tables[0].to_csv('[Link]’) # If you have positive numbers and want to invert the x-axis China 3622.313 -5388.81
of the left plot Germany 1729.224 -2038.25
df = pd.read_csv('[Link]’)
#axes[0].invert_xaxis() Japan 1666.454 -2362.68
data = df[['Country','Revenues','Expenditures']].head(10) France 1334.944 -1609.71
# To show data from highest to lowest
data.set_index('Country', inplace=True) United Kingdom 966.407 -1400.78
[Link]().invert_yaxis()
data['Revenues'] = data['Revenues'].astype(float) Italy 863.785 -1103.72
data['Revenues'] = data['Revenues'] / 1000 India 620.739 -940.771
axes[0].set(yticks=[Link], yticklabels=[Link])
data['Expenditures'] = data['Expenditures'].astype(float) axes[0].yaxis.tick_left() Canada 598.434 -917.271
data['Expenditures'] = data['Expenditures'] / 1000 * (-1) axes[0].tick_params(axis='y', colors='white') # tick color Spain 481.945 -657.75
310 DT Ch ChCh Ch Ch Ch Ch
#[Link]
import numpy as np
import [Link] 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 = [Link](facecolor="white")
ax = fig.add_subplot(1, 1, 1)
bar_width = 0.5
bar_l = [Link](1, 4)
tick_pos = [i + (bar_width / 2) for i in bar_l]

ax1 = [Link](bar_l, A, width=bar_width, label="A", color="green")


ax2 = [Link](bar_l, B, bottom=A, width=bar_width, label="B", color="blue")

ax.set_ylabel("Count", fontsize=18)
ax.set_xlabel("Class", fontsize=18)
[Link](loc="best") #Adding Legend to “best” location
[Link](tick_pos, ["C1", "C2", "C3"], fontsize=16)
[Link](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()
[Link](r1.get_x() + r1.get_width() / 2., h1 / 2., "%d" % h1, **text_kwargs) #unpacking dict
Stacked • Adding Labels to the bars
[Link](r2.get_x() + r2.get_width() / 2., h1 + h2 / 2., "%d" % h2, **text_kwargs)

Bar Charts [Link]('labels_stacked_bar_charts.png', dpi = 300, format = 'png’, bbox_inches='tight')


[Link]()

311 DT Ch ChCh Ch Ch Ch Ch
[Link]('ggplot')
text_kwargs = {'ha':"center", 'va':"center", 'color':"white",
'fontsize':18, 'fontweight':"semibold"}

ax = [Link](stacked=True, kind='bar', figsize=(10, 6), rot='horizontal')


fig = [Link]

# .patches gives acess to all the patches (bars)


for rect in [Link]:
# 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
#[Link]
#Check answer by Trenton McKinney # [Link](x, y, text)
label_x = x + width / 2
Below changes vis-a-vis above link
label_y = y + height / 2
• Dictionary style unpacking of text keyword arguments
• Tick labelsize, Axis labelsize and labelpad # plot only when height is greater than specified value
• Resetting yaxis limits if height > 0:
[Link](label_x, label_y, label_text, **text_kwargs) #Dictionary style unpacking
import pandas as pd
import [Link] as plt
#Data
Stacked [Link](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
A = [45, 17, 47]
B = [91, 70, 72]
Bar Charts ax.tick_params(labelsize = 16)
ax.set_ylim(0, ax.get_ylim()[1]*1.1) #Increasing the upper ylim to create spacing
C = [68, 43, 13]
ggplot style [Link]('labels_stacked_bar_charts_ggplot.png', dpi = 300,
format = 'png',bbox_inches='tight')
# pandas dataframe [Link]()
df = [Link](data={'A': A, 'B': B, 'C': C})
[Link] = ['C1', 'C2', 'C3']

312 DT Ch ChCh Ch Ch Ch Ch
# Pandas Bar Plotting with datetime in x axis # plot
#[Link] fig, ax = [Link]()
format df_months.plot(kind="bar", figsize=(16,5), stacked=True, ax=ax)
import pandas as pd
import [Link] as plt # Decorations/Embellishments
import numpy as np [Link](True, alpha = 0.5, ls = 'dotted')
ax.set_axisbelow(True)
[Link]('default')
# generate sample data # format xtick-labels with list comprehension
start = pd.to_datetime("1-1-2012") ax.set_xticklabels([[Link]("%Y-%m") for x in df_months.index],ha = 'right', rotation=45)
index = pd.date_range(start, periods= 365) [Link]('ticklabels_using [Link]', dpi = 300, bbox_inches = 'tight',
transparent = True)
prng = [Link].default_rng(123456) [Link]()
df = [Link]({'A' : [Link](365), 'B' : [Link](365)}, index=index)

# resample to any timeframe you need, e.g. months


Pandas • Datetime Index
df_months = [Link]("M").sum() Stacked Bar • Using for [Link] for tick labels
• Using Resample to ‘Months’
plots

313 DT Ch ChCh Ch Ch Ch Ch
Share-x on Data with different x values #[Link]
#Check answer by gmds
import pandas as pd
import numpy as np
Matplotlib
import [Link] as plt
import seaborn as sns

fig, axes = [Link](2, sharex=‘all’)


ax1, ax2 = axes

df = [Link]({‘x’: [‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’], ‘y1’: [Link](6)})


df2 = [Link]({‘x’: [‘B’, ‘D’, ‘E’], ‘y2’: [Link](3)})

combined = [Link](df2, how=‘left’, on=‘x’)

[Link](combined[‘x’], combined[‘y1’])
[Link](combined[‘x’], combined[‘y2’])

[Link](f’Share_x_across_subplots.png’,dpi=150, format=‘png’, bbox_inches='tight’)


[Link]()

#Alternately, one can also use Seaborn


df = [Link]({'x':['A','B','C','D','E','F'],'y1':[Link](6)}) Seaborn
df2 = [Link]({'x':['B','D','E'],'y2':[Link](3)})

order = [Link](list(df.x)+list(df2.x))

fig,axes = [Link](2, sharex='all')

[Link](x='x',y='y1',data=df,ax=axes[0], order=order)
[Link](x='x',y='y2',data=df2,ax=axes[1], order=order)
[Link]()

314 DT Ch ChCh Ch Ch Ch Ch
import pandas as pd
import numpy as np
import [Link] as plt
[Link](‘ggplot’)

# Preparing Sample data


N = 100
industry = ['a','b','c']
city = ['x','y','z']
ind = [Link](industry, N)
cty = [Link](city, N)
jobs = [Link](low=1,high=250,size=N)
df_city =[Link]({'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 = [Link](1, cols, figsize=(8, 8), sharey = True )

for x, city in enumerate(df_city.city.value_counts().[Link]): #for loop to create bar plots


data = df_city[(df_city['city'] == city)]
data = [Link](['industry']).[Link]()
print (data)
print (type([Link]))
left= [k[0] for k in enumerate(data)]
right= [k[1] for k in enumerate(data)]

Faceted Bar Charts color = [Link].get_cmap('Set1').colors[x] #Access colors from Listed colormaps

axes[x].bar(left,right,label="%s" % (city), color = color)


Pandas + Matplotlib axes[x].set_xticks(left, minor=False)
axes[x].set_xticklabels([Link])
Similar to facet_wrap in ggplot2,
Facet_grid in Seaborn axes[x].legend(loc='best')
axes[x].grid(True)
Also check : [Link]('Employment By Industry By City’, fontsize=20)
§ [Link]
-matplotlib-faceting-bar-plots plt.subplots_adjust(wspace = 0.2)
[Link](f'Multiple Subplots_shared_y.png',dpi=300, format='png', bbox_inches='tight')

315 DT Ch ChCh Ch Ch Ch Ch
Pareto Chart
import pandas as pd
import [Link] as plt
from [Link] import PercentFormatter

df = [Link]({'country': [177.0, 7.0, 4.0, 2.0, 2.0, 1.0, 1.0, 1.0]})


[Link] = ['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 = [Link](figsize = (10,5))

[Link]([Link], df["country"], color="C0")


ax2 = [Link]() #Creating twin axes with shared x -axis
#[Link] [Link]([Link], df["cumpercentage"], color="C1", marker="D",
python ms=7)
[Link].set_major_formatter(PercentFormatter())
A Pareto Chart is a graph that indicates the frequency of defects, as well as ax.tick_params(axis="y", colors="C0",labelsize = 13)
their cumulative impact. Pareto Charts are useful to find the defects to prioritize ax.tick_params(axis="x", colors="0", rotation = 15)
in order to observe the greatest overall improvement. ax2.tick_params(axis="y", colors="C1", labelsize = 13)
Pareto chart contains both bars and a line graph, where individual values are for labels in ax.get_xticklabels():
represented in descending order by bars, and the cumulative total is [Link](size = 13,rotation = 0, rotation_mode = 'anchor’,
represented by the line. The chart is named for the Pareto principle, which, in ha = 'center')
turn, derives its name from Vilfredo Pareto, a noted Italian economist.
[Link](f'Pareto [Link]',dpi=150, format='png’,
bbox_inches='tight')

316 DT Ch ChCh Ch Ch Ch Ch
Back #Optional line plotting with thick lines
#[Link]([Link], [Link], lw=2)

p = ax1.fill_between([Link], pricemin, [Link],


facecolor='blue', alpha=0.5)

[Link](True) #Make Gridlines visible


ax1.set_ylabel('price’)

#Make y ticklabels invisible


for label in ax1.get_yticklabels():
label.set_visible(False) Fill Between and Alpha
[Link]('Google (GOOG) daily closing price')
fig.autofmt_xdate()
[Link]("Fill [Link]",dpi=300, format='png’,
#[Link] bbox_inches='tight’)
kers/fill_between_alpha.html#sphx-glr-gallery-lines-bars- [Link]()
and-markers-fill-between-alpha-py

import [Link] as plt


import numpy as np
import [Link] as cbook

# Fixing random state for reproducibility


[Link](19680801)

# load up some sample financial data


r = (cbook.get_sample_data('[Link]’,
np_load=True)['price_data'].view([Link]))

# create two subplots with the shared x and y axes


fig, ax1 = [Link]()

pricemin = [Link]() #For entering as argument later


#Above plot by uncommenting the optional line plotting
[Link]([Link], [Link], lw=2)

317 DT Ch ChCh Ch Ch Ch Ch
Back
# color fiat differently # Decorate
[Link][[Link] == 'Fiat X1-9', 'colors'] = 'darkorange' [Link]('Diverging Bars of Car Mileage’,
df.sort_values('mpg_z', inplace=True) fontdict={'size':20})
df.reset_index(inplace=True) [Link](linestyle='--', alpha=0.5)

# Draw plot [Link]("aesthetic [Link]",dpi=300, format='png’,


import [Link] as patches bbox_inches='tight')
[Link]()
fig, ax = [Link](figsize=(10,12), dpi= 300)
[Link](y=[Link], xmin=0, xmax=df.mpg_z,
color=[Link], alpha=0.4, linewidth=1) Interesting Use Case
[Link](df.mpg_z, [Link], color=[Link],
s=[600 if x == 'Fiat X1-9' else 300 for x in ü Horizontal lines
[Link]],
alpha=0.6)
ü Scatter points
[Link]([Link], [Link]) ü Annotations
[Link](fontsize=12)
ü Patches
# Annotate
[Link]('Mercedes Models', xy=(0.0, 11.0), ARTIST
xytext=(1.0, 11), xycoords='data', Horizontal lines • Starts from the base z score of 0
fontsize=15, ha='center', va='center', and move in either direction
bbox=dict(boxstyle='square', fc='firebrick'), • Visually appealing and helps
#[Link] arrowprops=dict(arrowstyle='-[, widthB=2.0, mapping the car model label with
matplotlib-visualizations-the-master-plots-python/ - 14.- lengthB=1.5', lw=2.0, color='steelblue’), the scatter point
Area-Chart color='white')
Scatter points The scatter points with size 300 for all
import pandas as pd # Add Patches cars except Fiat X1-9. x position
# Prepare Data p1 = [Link]((-2.0, -1), width=.3, height=3, indicates the z score in terms of
df = alpha=.2, facecolor='red') mileage.
pd.read_csv("[Link] p2 = [Link]((1.5, 27), width=.8, height=5,
Annotations To markout the Mercedes models
aster/[Link]") alpha=.2, facecolor='green’)
x = [Link][:, ['mpg']] Patches To highlight the cars at each of the
df['mpg_z'] = (x - [Link]())/[Link]() extremes
df['colors'] = 'black' [Link]().add_patch(p1)
[Link]().add_patch(p2) Raw Data – Next Slide
318 DT Ch ChCh Ch Ch Ch Ch
Back
"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"
#[Link] 3.89871773792359,8,304,150,3.15,3.435,17.3,0,0,3,2,0,"AMC Javelin","AMC Javelin"
matplotlib-visualizations-the-master-plots-python/ - 14.-Area- 3.64691650576209,8,350,245,3.73,3.84,15.41,0,0,3,4,0,"Camaro Z28","Camaro Z28"
Chart 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"
Raw Data Source Link: 5.09901951359278,4,120.3,91,4.43,2.14,16.7,0,1,5,2,1,"Porsche 914-2","Porsche 914-2"
[Link]
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"
Raw Data : Table on the right
4.43846820423443,6,145,175,3.62,2.77,15.5,0,1,5,6,1,"Ferrari Dino","Ferrari Dino"
• Copy onto excel sheet and save as csv. Use pd.read_csv in
3.87298334620742,8,301,335,3.54,3.57,14.6,0,1,5,8,0,"Maserati Bora","Maserati Bora"
jupyter notebook. `
4.62601340248815,4,121,109,4.11,2.78,18.6,1,1,4,2,1,"Volvo 142E","Volvo 142E"
• Alternately, copy onto excel sheet and Use Text to
columns in Data Tab. Proceed to R Studio for EDA in R. Raw Data

319 DT Ch ChCh Ch Ch Ch Ch
#Turn off the minor ticks Also check :
Back plt.minorticks_off() § [Link]
arkers/[Link]#sphx-glr-gallery-lines-bars-and-
# Turn on the grid markers-barchart-py
#[Link](which='major', linestyle='-', linewidth='0.5’,
color='green') § [Link]
#[Link](which='minor', linestyle=':', linewidth='0.5’, bar-charts-matplotlib/
color='black’)
§ To get a list of all containers in the axes, use
#Display the y axis grid lines [Link]
[Link](True, axis = 'y', alpha = 0.95, ls = ':’) [Link]
get-a-barcontainer-object-from-an-axessubplot-object-
#Instantiating axes object to use methods in-matplotllib
ax = [Link]() § To get list of all artists in the axes, use ax.get_children()
§ To get list of the attributes and methods of axes
#Adding Legend object, use print(dir(ax))
from pandas import DataFrame [Link](bbox_to_anchor=(1.05, 0.5), loc='center left’,
import [Link] as plt fontsize = 15)
import numpy as np
#Setting xtick label properties
Grouped Bar Chart
# setting font size to 30
#[Link]('ggplot')
[[Link](rotation =0, ha = ‘center’, fontsize = 15) for
ticklabel in ax.get_xticklabels()]
Pandas plotting
[Link]() #Setting ytick parameters
[Link]({'[Link]': 20}) [Link].set_tick_params(rotation =0, labelsize = 15 )

#Preparing the sample data [[Link](alpha = 0.5) for ticks in ax.get_xticklabels()]


a=[Link]([[4,8,5,7,6], [[Link](alpha = 0.5) for ticks in ax.get_yticklabels()]
[2,3,4,2,6],
[4,7,4,7,8], #Setting figure properties
[2,6,4,8,6], fig = [Link]()
[2,4,3,3,2]]) [Link](figheight = 5, figwidth =8)

df=DataFrame(a, columns=['a','b','c','d','e’], [Link](“[Link]”,dpi=300, format=‘png’,


index=['Jan','Feb','March','April','May']) bbox_inches=‘tight’)

r = [Link](kind='bar’) #Using Pandas plotting function [Link]()

320 DT Ch ChCh Ch Ch Ch Ch
#[Link]
matplotlib-in-python
a = range(1,10)
b = range(4,13)
ind = [Link](len(a))

fig = [Link]()
ax = fig.add_subplot(111)
[Link](x=ind, height=a, width=0.35,align='center')
[Link](x=ind, height=b, width=0.35/3, align='center')

[Link](ind, a)

plt.tight_layout()
[Link]('Bullet_chart.png', dpi = 300, transparent = True)
[Link]()

Matplotlib • Bars of different widths


Bar Plots

321 DT Ch ChCh Ch Ch Ch Ch
Code Start

from datetime import datetime


import [Link] as plt
from [Link] 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([
#[Link]
[Link](day, '%d/%m/%Y').strftime('%m/%d/%Y')
plot-ticks-with-matplotlib-dates-and-datestr2num for day in days
])
Context : datestr2num calls (wrapper) dateutil but currently does not
forward all keyword arguments. For instance, dayfirst is not forwarded #Alternately instead of datestr2num, we can directly use date2num on the datettime object
by datestr2num. Instead, month first is always considered. This creates #x = date2num([ [Link](day, '%d/%m/%Y') for day in days]
problems incase of ambiguous date formats. The date2strnum is not w = 0.10
robust to deal consistently with dates in string form. We therefore first
fig = [Link](figsize=(8, 4))
parse date string with Python datetime module functions, convert back ax = fig.add_subplot(111)
to date string with month first string format ('%m/%d/%Y') and then
pass it to datestr2num to convert to matplotlib dates. #align:'center' is important and width as 2*w in conjunction with x value ensures adjacent bars
[Link](x - w, adata, width=2 * w, color='g', align='center', tick_label=days, label = 'adata')
• To process strings, use datetime functions [Link](x + w, bdata, width=2 * w, color='r', align='center', tick_label=days, label = 'bdata')
Matplotlib • Convert string to datetime object using
strptime [Link].set_major_locator(AutoDateLocator(minticks=3, interval_multiples=False))
Parsing Date • Convert datetime object back to string
using strftime
[Link].set_major_formatter(DateFormatter("%d/%m/%y"))
[Link]() #Adding legend - automatic detection of handles, labels
strings • Convert the string from previous step to
[Link]('Processing_dates_in_string_as_xaxis.png', dpi = 300)
[Link] using datestr2num
[Link]()
Code End
322 DT Ch ChCh Ch Ch Ch Ch
Code Start

import [Link] as plt


from [Link] import date2num
import datetime
import [Link] as mdates

x=[
[Link](2011, 1, 4, 0, 0),
[Link](2011, 1, 5, 0, 0),
[Link](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 = [Link](figsize = (8,5))
bars1 = [Link](x-2*w, y, width=2*w, color='b', align='center', label = 'y')
#Check the above discussion link. bars2 = [Link](x, z, width=2*w, color='g', align='center', label = 'z')
I have proposed a solution over here and extended the solution to bars3 = [Link](x+2*w, k, width=2*w, color='r', align='center', label = 'k')
handle any number of groups. Follow the next few slides.
[Link].set_major_locator([Link]())
Context : formatter = [Link]('%d %b\n%a')
• Three Bar plots plotted for the same datetime index [Link].set_major_formatter(formatter)
• Each Bar plot corresponds to values for a a single group against [Link]()
date values
• Ensure no overlap between the bars def autolabel(rects):
• Bars should be symmetrically positioned in either side of date tick for rect in rects:
h = rect.get_height()
value
[Link](rect.get_x()+rect.get_width()/2., 1.01*h, '%d'%int(h),
ha='center', va='bottom')
Matplotlib autolabel(bars1)
• Date Formatting in x axis autolabel(bars2)
Multiple Group • No overlapping between bars autolabel(bars3)
Bar plots Code End
[Link]()
323 DT Ch ChCh Ch Ch Ch Ch
Scalable Solution for multiple scenarios Slide 1 of 4
Matplotlib User Defined function for multiple scenarios import [Link] as plt
Code start
import numpy as np
Multiple Group • Date Formatting in x axis from matplotlib import cm
Bar plots • No overlapping between bars from [Link] import ListedColormap
from [Link] import LinearSegmentedColormap
from itertools import cycle, islice #Using iterator to cycle colors and boxstyle

def get_colors(y_list, cmap = 'tab20c'): User defined function


"""
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)([Link](0,0.6,len(y_list))))
color_list = islice(cycle([Link]),0,None)

return color_list Code continue Next slide

#Check the discussion link


Context :
• Three Bar plots plotted for the same datetime index User 1. Use cycler object to access colors in loop
Defined Call the
• Each Bar plot corresponds to values for a a single group against date 3.make_barplots(
values Functions function
2. Make bar plots for proper placement of bars *args)
• Ensure no overlap between the bars and date time formatting of x axis
• 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.

324 DT Ch ChCh Ch Ch Ch Ch
Scalable Solution for multiple scenarios Slide 2 of 4
# 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'): Code continues from Prev slide
"""
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([Link])
y_list = [Link]().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

User Defined cycler object to access colors in Call the


in a loop make_barplots
Functions function (*args)
To Make bar plots
325 DT Ch ChCh Ch Ch Ch Ch
Scalable Solution for multiple scenarios Slide 3 of 4
Logic for consistent placement of the bars LOGIC [Link](style) Code continues from Prev slide
Defining the problem : fig, ax = [Link](figsize = (10,6))
• A bar belonging to a group should appear at fixed position versus its xtick bars_list = []
• Bars of different group should not overlap
# Creating color_list as a cycler object for cycling through the colors
What we want ? color_list = get_colors(y_list, cmap = cmap)
Symmetrical distribution of bar groups across a tick location.
# Creating bar plots iteratively from the list y_list containing the list of values
The group of bar at each of the x axis value should be symmetrically for i,y in enumerate(y_list) :
distributed around the tick. There should be equal no. of bar groups on either n = len(y_list) # number of groups
side of the x-axis tick location. 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
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 # Plotting the bar plot for a single group
time. # The start location will be further offset
# by bar widths multiplied by the index position (i) of the group within the y_list
Let’s further narrow it down to a single tick location. bars = [Link](x-x_offset+i*2*w, y, width=2*w, color=next(color_list), align='edge’)
Three locations are important :
§ Tick Location bars_list.append(bars)
§ Start of the first bar patch corresponding to a tick location
§ Actual x value of a bar patch belonging to a group # Creating a function to add data labels above bars
def autolabel(rects):
1. Tick Location is matplotlib date( a floating point) and is fixed in our context. for rect in rects:
2. Start Location will be offset from Tick location depending on number of h = rect.get_height()
bars (number of groups) for a single tick [Link](rect.get_x()+rect.get_width()/2., 1.01*h, '%d'%int(h),
3. For even number of groups lets consider 4, the start location will be offset ha='center', va='bottom’)
by the width of 2 bars.
4. For odd number of groups lets consider 5, the start location will be offset # Adding data labels to bars by enumerating over bar containers and
by the width of 2.5 bars. for i, bars in enumerate(bars_list) :
5. Actual x value will be offset from the Start location depending on the autolabel(bars)
group sequence
# 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
User cycler object to access colors in Call the make_barplots bars.set_label(y_labels[i]) Code continues to Next slide
Defined in a loop function (*args)
Functions Make bar plots
326 DT Ch ChCh Ch Ch Ch Ch
Scalable Solution for multiple scenarios Slide 4 of 4
locator = [Link]() #Creating locator object
formatter = [Link]('%d %b\n%a’) #Creating locator object
[Link].set_major_locator(locator)
[Link].set_major_formatter(formatter) Code continues form prev slide

[Link](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)
[Link](loc = 'best')
else :
print('labels for the bars not entered. Hence, Legend missing.')

[Link]('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 [Link] as plt
from [Link] import date2num
import datetime
import [Link] as mdates

x = [ [Link](2011, 1, 4, 0, 0), [Link](2011, 1, 5, 0, 0),


[Link](2011, 1, 6, 0, 0)]

x = date2num(x)
y = [4, 9, 2]
z = [1, 2, 3]
k = [11, 12, 13]
User cycler object to access colors in Call the
in a loop make_barplots
Defined function fig, ax, bars_list = make_barplots(x, y_list, y_labels, w = 0.1, style = 'default') Code End
(*args)
Functions Make bar plots
327 DT Ch ChCh Ch Ch Ch Ch
Code Start

import pandas as pd
import numpy as np

#Fixing state for reproducibility


[Link](123456)

#Creating sample data for plotting using numpy and pandas Create sample data
index = pd.date_range("1/1/2000", periods=8)
s = [Link]([Link](5), index=["a", "b", "c", "d", "e"])
df = [Link]([Link](20,100, (8,4)), index=index, columns=["A", "B", "C", "D"])

#Make bar plots using the userdefined function make_barplots


make_barplots(x = date2num([Link]), y_list = [Link]().to_numpy().tolist() , \
y_labels = [Link], w = 0.1, style = ‘default’)
Code End

Creating DataFrames [Link]


[Link] line no. 1595
[Link]

Alternate way from DataFrame to List DataFrame DataFrame - Transpose


Pandas DataFrame columns are Pandas Series when A B C D 2000-01-01 2000-01-02 2000-01-03 2000-01-04 2000-01-05 2000-01-06 2000-01-07 2000-01-08
you pull them out, which you can then call [Link]() on
to turn them into a Python list.
2000-01-01 88 82 69 87 A 88 35 47 51 31 29 48 84
( df 2000-01-02 35 47 88 24 B 82 47 93 56 41 84 66 96
.filter(['column_name’])
.values
2000-01-03 47 93 66 31 C 69 88 66 65 94 60 81 75
.reshape(1, -1) 2000-01-04 51 56 65 71 D 87 24 31 71 25 91 99 52
.ravel()
.tolist()
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

Matplotlib Demo using the User defined function make_barplots

Multiple Group • Date Formatting in x axis


• No overlapping between bars
Bar plots • Number of bar groups = 4
328 DT Ch ChCh Ch Ch Ch Ch
Code Start

import pandas as pd
import numpy as np

#Fixing state for reproducibility


[Link](123456)

#Creating sample data for plotting using numpy and pandas Create sample data
index = pd.date_range("1/1/2000", periods=8)
s = [Link]([Link](5), index=["a", "b", "c", "d", "e"])
df = [Link]([Link](20,100, (8,4)), index=index, columns=["A", "B", "C", "D"])

#Make bar plots using the userdefined function make_barplots


make_barplots(x = date2num([Link]), y_list = [Link]().to_numpy().tolist() , \
y_labels = [Link], w = 0.1, style = ‘default’)
Code End
Creating DataFrames [Link]

style : ‘seaborn-whitegrid’
[Link](False)

style : ‘seaborn-whitegrid’

329 DT Ch ChCh Ch Ch Ch Ch
Code Start

import [Link] as plt


import numpy as np
import [Link] as ticker

[Link]()
fig = [Link]()

# generate sample data for this example


count = 12
xs = list(range(1,count+1))
prng = [Link].default_rng(123456)
ys_bars = [Link](loc=3.0,size=len(xs))
ys_lines = [Link](loc=5.0,size=len(xs),scale=0.5)

# this is the axes on the top


ax1= [Link](211)

# plot the same numbers but multiplied by 20


[Link](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([Link](0,101,10))
[Link].set_major_locator([Link](10))

# set ticks for the common x axis (bottom)


[Link].set_ticks(xs)
[Link](True, alpha = 0.5)
ax1.fill_between(xs,y1 = ys_lines*20, alpha = 0.5, color = 'xkcd:carolina blue')
[Link](x=xs, ymin = 0, ymax = ys_lines*20, alpha = 0.5, lw = 0.3 )

ax2= [Link](212)
[Link](xs,ys_bars,color='red')
[Link](True, alpha = 0.5)
ax2.set_axisbelow(True)
• Categorical Bar and Line plot across ax2.set_xticks(xs)
Multiple Subplots multiple subplots [Link].set_major_locator([Link](1))
Line plot + Bar plot • [Link](10) plt.subplots_adjust(hspace = 0.4)
Shared Axis • Axes.fill_between() [Link]('Simple LinePlots_BarPlots_sharedxaxis.png', dpi = 300, transparent = True)
• [Link]() Code End

330 DT Ch ChCh Ch Ch Ch Ch
• Categorical Bar and Line plot on same Axes
Overlaying Charts • X axis is numeric but non datetime
• Twin Axes with shared x axis
[Link](xs,ys_bars,color = 'xkcd:lichen', alpha = 0.8)
• Aligning yaxis of the twin axes

# order is important when setting ticks.


# Ticks must be set after the plot has been drawn
ax1.set_yticks([Link](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
[Link].set_major_locator([Link](numticks=NUM_TICKS))

# create the 'twin’ axes with the y axis on right


ax2=[Link]()

# plot the same numbers but multiplied by 20


[Link](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
[Link].set_major_locator([Link](numticks=NUM_TICKS))
Code Start

import [Link] as plt # set ticks for the common x axis (bottom)
import numpy as np [Link].set_ticks(xs)

fig, ax1 = [Link]() #Show yaxis gridlines


[Link](True)
# generate sample data for this example ax1.set_axisbelow(True)
xs = list(range(1,13))
prng = [Link].default_rng(123456) [Link]('Barplot_Lineplot.png', dpi = 300, transparent = False)
ys_bars = [Link](loc=3.0,size=12)
ys_lines = [Link](loc=5.0,size=12,scale=0.5)

Check [Link] Code End


Note : Changes have been done visavis the code in above link.
331 DT Ch ChCh Ch Ch Ch Ch
• Categorical Bar and Line plot on same Axes
Overlaying Charts • X axis is numeric but non datetime [Link](xs,ys_bars,color = 'xkcd:lichen')
• Twin Axes with a shared x axis
• [Link] from the line plot # order is important when setting ticks.
# Ticks must be set after the plot has been drawn
ax1.set_yticks([Link](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
[Link].set_major_locator([Link](numticks=NUM_TICKS))

# create the 'twin’ axes with the y axis on the right


ax2=[Link]()
# Setting zorder of the twin axes below original axes. Check link1, link2 on zorder
ax2.set_zorder(ax2.get_zorder()-1)
[Link](visible = False)
# plot the same numbers but multiplied by 20
[Link](xs,ys_lines*20,color='red')

# set the ticks for the twin axis


#ax2.set_yticks([Link](0,101,10))
Code Start
ymax = (ax2.get_ylim()[1]//100+0.5)*100
import [Link] as plt ax2.set_ylim(0, ymax)
import numpy as np # change the tick locator for this axis and set the desired number of ticks
[Link].set_major_locator([Link](numticks=NUM_TICKS))
fig, ax1 = [Link]()
# set ticks for the common x axis (bottom)
# generate sample data for this example [Link].set_ticks(xs)
xs = list(range(1,13))
prng = [Link].default_rng(123456) [Link](True)
ys_bars = [Link](loc=3.0,size=12) ax1.set_axisbelow(True)
ys_lines = [Link](loc=5.0,size=12,scale=0.5)
[Link](x = xs,ymin = 0, ymax = ys_lines*20) #[Link] from line plot
Check [Link] [Link]('Barplot_Lineplot_2.png', dpi = 300, transparent = False) Code End
Note : Changes have been done visavis the code in above link.
332 DT Ch ChCh Ch Ch Ch Ch
COMBO CHARTS
(OVERLAY CHARTS)

333 DT Ch ChCh Ch Ch Ch Ch
[Link]
beautiful-plots-with-pandas-and-matplotlib/

334 DT Ch ChCh Ch Ch Ch Ch
335 DT Ch ChCh Ch Ch Ch Ch
The plot and data is inspired from the below link :
[Link]

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 :


[Link]

336 DT Ch ChCh Ch Ch Ch Ch
Code Start
[Link]['[Link]']=(20,10) # set the figure size
[Link]('fivethirtyeight') # using the fivethirtyeight matplotlib theme

path1 = "[Link]
#path = '/Users/bhaskarroy/Files/Data Science/PYTHON/Visualisation/Matplotlib/Overlaying charts/[Link]'

sales = pd.read_csv(path1) # Read the data in


[Link] = pd.to_datetime([Link]) #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[([Link] >= '2009-10-01') & ([Link] < '2010-10-01')]
fy11_all = sales[([Link] >= '2010-10-01') & ([Link] < '2011-10-01')]
fy12_all = sales[([Link] >= '2011-10-01') & ([Link] < '2012-10-01')]
fy13_all = sales[([Link] >= '2012-10-01') & ([Link] < '2013-10-01')]
fy14_all = sales[([Link] >= '2013-10-01') & ([Link] < '2014-10-01')]
fy15_all = sales[([Link] >= '2014-10-01') & ([Link] < '2015-10-01')]

The plot and data is inspired from the link.


Below changes have been done in the code : Code continues to Next slide
• 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

337 DT Ch ChCh Ch Ch Ch Ch
Code continues from Prev slide

# Let's build our plot

fig, ax1 = [Link]()


ax2 = [Link]() # set up the 2nd axis

df = [Link]()
[Link] = mdates.date2num([Link])
[Link](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 [Link] 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 = [Link]()
[Link] = mdates.date2num([Link])
[Link]([Link], [Link], **kwargs, color = next(color_list), label = 'Monthly Quantity')

[Link](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 :
Code continues to Next slide
• 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

338 DT Ch ChCh Ch Ch Ch Ch
Code continues from Prev slide

#Creating locator/formatter
from [Link] import rrule, MONTHLY,YEARLY
from datetime import datetime, timedelta #timedelta holds days, seconds, microseconds internally
import [Link] as mdates
import [Link] as ticker

start_date = datetime(2009,10,1)

#Let's do ceiling division to get the relevant number of financial years


count = -(-([Link][-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 = [Link](date_list)
date_labels = [[Link](x, "FY %Y") for x in mdates.num2date(date_list)]
formatter = [Link](date_labels)

# Set the x-axis labels to be more meaningful than just some random dates.
[Link].set_major_locator(locator)
[Link].set_major_formatter(formatter)

[Link](b = True, color = 'grey', lw = 1,alpha = 0.5)


[Link].set_tick_params(pad = 15)
[[Link](ha = 'left',rotation = 0, rotation_mode = "anchor") for tkl in [Link].get_ticklabels()]

# Creating Legend with a mix of automatic detection and adding proxy artists
handle, label = ax1.get_legend_handles_labels()
The plot and data is inspired from the link. patch1 = [Link](color='orange',alpha = 0.5, label='Monthly Qty',zorder = 100)
Below changes have been done in the code : patch2 = [Link](color='grey',alpha = 0.5, label='Monthly Qty', zorder = 100)
• Locator/formatter pairs created for placement of x-axis tick labels [Link]([patch1, patch2])
• For loop to create the bar plots iteratively
• Cycler object with islice from itertools to assign colors to bars [Link](handles = handle,bbox_to_anchor = (1,1), loc = 'upper right', bbox_transform = [Link])
• Addition of legend by mix of automatic detection and proxy artists
#ax2.set_ylim(0,ax2.get_ylim()[1]*10//3) # To restrict the bar plots to only 30% of the height of line plots
For the revised code, go to the link
[Link]()
Code End

339 DT Ch ChCh Ch Ch Ch Ch
[Link]
beautiful-plots-with-pandas-and-matplotlib/
Go to the above link for detailed run-through of
the context and the code for creating the plot.

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

340 DT Ch ChCh Ch Ch Ch Ch
Code Start

import [Link] as plt


import pandas as pd
import numpy as np
import matplotlib as mpl
from [Link] import LinearSegmentedColormap
from [Link] 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,
[Link] 49035,45228,43094,41543,30528]
beautiful-plots-with-pandas-and-matplotlib/ 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,
Go to the above link for comprehensive run-
16.8,10.8]
through of the context and the code for
life_expectancies = [81.8,82.1,81.8,80.7,80.5,76.4,82.4,80.5,73.8,80.8,73.5,
creating the plot.
74.6,79.9,81.1,77.7,80.7,72.1,72.2,77,75.4,74.4,79.4,81,80.5]
The code has been reproduced from the link data = {'extension' : [Link](extensions, index=countries),
with all the comments intact. There is only a 'population' : [Link](populations, index=countries),
minor change owing to API changes as the link 'life expectancy' : [Link](life_expectancies, index=countries)}
is dated. The change is : [Link]
has been used for instantiation of norm as df = [Link](data)
required for Scalar Mappable. df = df.sort_values(by = 'life expectancy')

See also Code continues to Next slide


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

341 DT Ch ChCh Ch Ch Ch Ch
Code continues from Prev slide

# Create a figure of given size


fig = [Link](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)

[Link] # Remove grid lines (dotted lines inside plot)


beautiful-plots-with-pandas-and-matplotlib/ [Link](False)
Go to the above link for comprehensive run- # Remove plot frame
through of the context and the code for ax.set_frame_on(False)
creating the plot. # Pandas trick: remove weird dotted line on axis
#[Link][0].set_visible(False)
The code has been reproduced from the link
with all the comments intact. There is only a # Customize title, set position, allow space on top of plot for title
minor change owing to API changes as the link ax.set_title(ax.get_title(), fontsize=26, alpha=a, ha='left')
is dated. The change is : [Link] plt.subplots_adjust(top=0.9)
has been used for instantiation of norm as [Link].set_position((0,1.08))
required for Scalar Mappable.
# Set x axis label on top of plot, set label text
See also [Link].set_label_position('top')
Colormap reference for a list of builtin colormaps.
Creating Colormaps in Matplotlib for examples of how to make colormaps. xlab = 'Population (in millions)'
Choosing Colormaps in Matplotlib an in-depth discussion of choosing
colormaps.
ax.set_xlabel(xlab, fontsize=20, alpha=a, ha='left')
Colormap Normalization for more details about data normalization. [Link].set_label_coords(0, 1.04)
Customized Colorbars tutorials Code continues to Next slide

342 DT Ch ChCh Ch Ch Ch Ch
Code continues from Prev slide

# Position x tick labels on top


[Link].tick_top()
# Remove tick lines in x and y axes
[Link].set_ticks_position('none')
[Link].set_ticks_position('none')

# Customize x tick labels


xticks = [5,10,20,50,80]
[Link].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)
[Link].set_tick_params(pad=12)

[Link] # Set bar height dependent on country extension


beautiful-plots-with-pandas-and-matplotlib/ # Set min and max bar thickness (from 0 to 1)
Go to the above link for comprehensive run- hmin, hmax = 0.3, 0.9
through of the context and the code for xmin, xmax = min(df['extension']), max(df['extension'])
creating the plot. # Function that interpolates linearly between hmin and hmax
f = lambda x: hmin + (hmax-hmin)*(x-xmin)/(xmax-xmin)
The code has been reproduced from the link # Make array of heights
with all the comments intact. There is only a hs = [f(x) for x in df['extension']]
minor change owing to API changes as the link
is dated. The change is : [Link] # Iterate over bars
has been used for instantiation of norm as for container in [Link]:
required for Scalar Mappable. # Each bar has a Rectangle element as child
for i,child in enumerate(container.get_children()):
See also # Reset the lower left point of each bar so that bar is centered
Colormap reference for a list of builtin colormaps.
Creating Colormaps in Matplotlib for examples of how to make colormaps. child.set_y(child.get_y()- 0.125 + 0.5-hs[i]/2)
Choosing Colormaps in Matplotlib an in-depth discussion of choosing
colormaps.
# Attribute height to each Recatangle according to country's size
Colormap Normalization for more details about data normalization. [Link](child, height=hs[i])
Customized Colorbars tutorials
Code continues to Next slide

343 DT Ch ChCh Ch Ch Ch Ch
Code continues from Prev slide

# 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']), [Link](df['extension']),


max(df['extension'])]]
# Position legend in lower right part
# Set ncol=3 for horizontally expanding legend
leg = [Link]([l1, l2, l3], labels, ncol=3, frameon=False, fontsize=16,
[Link] bbox_to_anchor=[1.1, 0.11], handlelength=2,
beautiful-plots-with-pandas-and-matplotlib/ handletextpad=1, columnspacing=2, title='Size (in km2)')
Go to the above link for comprehensive run-
through of the context and the code for # Customize legend title
creating the plot. # Set position to increase space between legend and labels
[Link](leg.get_title(), fontsize=20, alpha=a)
The code has been reproduced from the link leg.get_title().set_position((0, 10))
with all the comments intact. There is only a # Customize transparency for legend labels
minor change owing to API changes as the link [[Link](label, alpha=a) for label in leg.get_texts()]
is dated. The change is : [Link]
has been used for instantiation of norm as Code continues to Next slide
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

344 DT Ch ChCh Ch Ch Ch Ch
Code continues from Prev slide

# Create a fake colorbar


ctb = LinearSegmentedColormap.from_list('custombar', customcmap, N=2048)
# Trick from [Link]
# matplotlib-add-colorbar-to-a-sequence-of-line-plots
# Used [Link] for Scalar Mappable
sm = [Link](cmap=ctb, norm=[Link](vmin=72, vmax=84))
# Fake up the array of the scalar mappable
sm._A = []

# Set colorbar, aspect ratio


cbar = [Link](sm, alpha=0.05, aspect=16, shrink=0.4)
[Link].set_edgecolor("face")

# Remove colorbar container frame


[Link].set_visible(False)

[Link] # Fontsize for colorbar ticklabels


beautiful-plots-with-pandas-and-matplotlib/ [Link].tick_params(labelsize=16)
Go to the above link for comprehensive run-
through of the context and the code for # Customize colorbar tick labels
creating the plot. mytks = range(72,86,2)
cbar.set_ticks(mytks)
The code has been reproduced from the link [Link].set_yticklabels([str(a) for a in mytks], alpha=a)
with all the comments intact. There is only a
minor change owing to API changes as the link # Colorbar label, customize fontsize and distance to colorbar
is dated. The change is : [Link] cbar.set_label('Age expectancy (in years)', alpha=a, rotation=270, fontsize=20,
has been used for instantiation of norm as labelpad=20)
required for Scalar Mappable.
# Remove color bar tick lines, while keeping the tick labels
See also cbarytks = [Link]([Link], 'yticklines')
Colormap reference for a list of builtin colormaps.
Creating Colormaps in Matplotlib for examples of how to make colormaps. [Link](cbarytks, visible=False)
Choosing Colormaps in Matplotlib an in-depth discussion of choosing
colormaps.
Colormap Normalization for more details about data normalization. [Link]("beautiful [Link]", bbox_inches='tight')
Customized Colorbars tutorials Code End

345 DT Ch ChCh Ch Ch Ch Ch
Data Source : [Link]

Check out the github repository for the shape files and notebook:
GeoSpatial Data - Chloropeth Map
[Link] Using Geopandas, Matplotlib

346 DT Ch ChCh Ch Ch Ch Ch
Data Source : [Link]

Check out the github repository for the shape files and notebook: GeoSpatial Data - Chloropeth Map
[Link]
Using Geopandas, Matplotlib

347 DT Ch ChCh Ch Ch Ch Ch
REFERENCES

348 DT Ch ChCh Ch Ch Ch Ch
References Start
Category Description Link
Matplotlib overview, Succinct overview of matplotlib [Link]
tutorials What does it mean when they say 'stateful’? [Link]
Summary of Matplotlib for python developers [Link]
Very Interesting Article ”Matplotlib and the Future of Visualization in [Link]
Python”by Jake VanderPlas
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 [Link]
Nicola P Rougier [Link]
Beautiful tutorial using Pandas and Matplotlib in conjunction : [Link]
axes-and-subplots/
Blog Post - Python, Machine Learning, and Language Wars [Link]
Python related Understanding [Link] in Pandas : [Link]
Discussion to wrap all of the matplotlib getter and setter methods [Link]
with python properties, allowing them to be read and written like
class attributes
On *args and **kwargs [Link]
On purpose of kwargs in [Link]
Decorator basics [Link]
Sorting a dictionary by value [Link]
Using ax parameter [Link]
parameter?
Infinite Iterating always from a fixed position : [Link]
Difference between property and attribute : [Link]
attribute/7377013#7377013
Introduction to Probability Using Python : [Link]
Discussion on Array Programming with Numpy : [Link]
On Random Number [Link]
Fluent Python by Luciano Ramalho. Chapter - ‘Iterables, Iterators, and Also check [Link]
Generators’ is relevant for many operations in matplotlib, pandas,
numpy
‘The NumPy array: a structure for efficient numerical computation’ by [Link]
Stefan van der Walt provides indepth understanding of Numpy Arrays

349 DT Ch ChCh Ch Ch Ch Ch
Category Description Link
Figures, subplots, Relationship between DPI and figure size [Link]
gridspec
Change the figure size [Link]
matplotlib/638443#638443
Using bbox_inches = ‘tight’ in [Link]() to ensure all artists are in [Link]
saved output | behaviour of jupyter "inline" backend (%matplotlib
inline)
On Multiple Subplots [Link]

Combining different figures and putting in single subplot [Link]


sharing-a?noredirect=1&lq=1
Brilliant Link on adding subplots - [Link]
Excellent link on using GridSpec [Link]

Usecase Scenarios of [Link], [Link], [Link] : [Link]


Fig patch, axes patch, Superb Example on setting fc of fig patch and axes patch [Link]
zorder Zorder demo : [Link]

Ticks, tick labels On removing ticks [Link]


Rotating Axis Labels in Matplotlib [Link]
Tick Label Rotation and spacing [Link]
each-xtic?noredirect=1&lq=1
Making tick label font size smaller [Link]
Formatter for large tick values - [Link]
Discussion on tick labels zorder being above axes and patches [Link]
whereas the grid lines below the axes :
Tick locator and formatter for timeseries without using [Link]
[Link]
Time Series AR, MA and ARIMA Models in Python [Link]
Time Series Related Books 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

350 DT Ch ChCh Ch Ch Ch Ch
Category Description Link
Legend Go to reference for understanding the purport of Legend arguments [Link]
On adding text artists in Legend [Link]
Drawing and Animating Shapes with Matplotlib [Link]
Tuple Arguments in bbox_to_anchor [Link]
matplotlib/39806180#39806180
Annotation resources - • Adding value labels on a matplotlib bar chart
from matplotlib v3.4.2 • 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 : [Link]
Fonts demo object oriented style : [Link]
List of font families or name : [Link]
set([[Link] for f in []
matplotlib.font_manager.[Link]]) and set([[Link] for f
in matplotlib.font_manager.[Link]])
Data Visualizations Ten Simple Rules for Better Figures [Link]
Excellent link on beautiful data viz [Link]
Superb data viz [Link]
A Dramatic tour through Python’s Visualisation landscape including [Link]
ggplot2 and Altair altair/
Superb link covering different kind of plots. Can serve as the base
code which can be modified as per requirement [Link]
Superb blog on wide ranging topics related to data
wrangling/viz/programming with Pandas, matplotlib and python - [Link]
Pie vs dot plots [Link]
Visualizing Data by William Cleveland []
Cleveland dot plots in excel [Link]
plots
Styling the plots [Link]
Extensive coverage of code for multiple aspects of plotting [Link]

351 DT Ch ChCh Ch Ch Ch Ch
Category Description Link
Pandas Plotting, Legend in plotting with Pandas [Link]
Seaborn Seaborn : [Link]
Superb Article on using Matplotlib, pandas and seaborn together : [Link]
f619c8e8baa3
Time Series in Seaborn : [Link]
On limitation of plotting time series data in Seaborn : [Link]
Plotting time in python: [Link]
Time Series in Matplotlib : [Link]
types-in-pandas-python/customize-dates-matplotlib-plots-python/
Comprehensive coverage of datetime in Pandas Time series : [Link]
Converting between datetime, timestamp and datetime64 [Link]
R ggplot2 An Introduction to ggplot2 by Joey Stanley [Link]
“ggplot2 Elegant Graphics for Data Analysis” by Hadley Wickham - [Link]
Highly recommended for ggplot2
“R Graphics E ssentials for Great Data Visualization” by Alboukadel Highly recommended for creating great graphics for the right data using either the ggplot2 package and extensions or
KASSAMBAR 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 [Link]
experienced and new users
Bar charts in R ggplot : [Link]
Manipulation of tabular data in R [Link]
Comparison of viz tools in Python and R : [Link]
Covering baseline R vocabulary and knowledge for the primary data “Data Wrangling with R” by Bradley C. Boehmke
wrangling processes.
“R for Data Science” by Hadley Wickham and Garrett Grolemund – [Link]
Highly recommended
“Building a plot layer by layer” by Hadley Wickham - [Link]
geopandas Shapely documentation – Page No. 27 - [Link]
Creating another layer to take care of Nan values [Link]
Labeling polygons in geopandas plotting [Link]
Placement of colorbars using cax [Link]
Fixing colorbars [Link]
Setting single common colorbar for two subplots [Link]
Delhi Map Geopandas discussion [Link]
Interesting Case study Airbnb Paris analysis [Link]
detailed-map-with/63646936#63646936
References End

352 DT Ch ChCh Ch Ch Ch Ch

Common questions

Powered by AI

While both 'pcolormesh' and 'pcolor' create pseudocolor plots with non-regular rectangular grids, 'pcolormesh' is generally preferred due to its faster rendering performance, which is especially beneficial for large arrays. Additionally, 'pcolormesh' results in plots that are directly compatible with image backends, reducing interpolation artifacts and allowing for smoother visualizations .

When using 'numpy.random.RandomState', you utilize the legacy core generator Mersenne Twister, which gets shared between modules that use RandomState. In contrast, 'numpy.random.default_rng' introduces the approach using BitGenerators, allowing independent seeding and the use of different state-of-the-art algorithms like PCG64. This offers improved performance and reproducibility when deployed over several modules or packages .

The 'choice' function selects a single random element from the provided non-empty sequence, allowing the distribution over the entire sequence with replacement. On the other hand, 'sample' draws a specified number of unique elements from a sequence without replacement, ensuring that each sampled element only appears once, which is recommended for sampling subsets without redundancy .

'df.groupby()' is used to split DataFrame objects into groups based on certain criteria and then apply a function such as aggregation or transformation on these subsets, allowing for detailed data analysis by category. The 'df.pivot_table()', however, aggregates data and provides more complex reshaping and analysis capabilities by allowing calculations with specified functions at intersections of rows and columns given multi-level indexed data .

A Styler object in Pandas enhances the presentation of a DataFrame by offering formatting and custom styling options like highlighting, and color coding, and even adding custom CSS classes. It is instrumental in creating visually appealing tables for presentation purposes in Jupyter Notebooks or when exporting to formats like HTML .

The 'df.to_latex' method is used to render a DataFrame object to a LaTeX tabular, longtable, or nested table/tabular format, intended for use in documents prepared with LaTeX. In contrast, 'df.to_stata' is used to export a DataFrame object to a Stata .dta format, facilitating data analysis in the Stata statistical software environment .

The 'random.seed' function initializes the random number generator, enabling reproducibility by ensuring that the same sequence of pseudo-random numbers is generated each time. This behavior is consistent across Python's random module and NumPy, as both employ seeded algorithms like Mersenne Twister, allowing for consistent and predictable simulations, crucial in testing and debugging .

'MultipleLocator' is used to place ticks on integer multiples of a base within the plotting axis, which is useful for data having repetitive or cyclical patterns. In contrast, 'LinearLocator' is designed to automatically select a fixed number of tick locations to evenly distribute them across the given range of the axis, suiting more for generic fitting of ticks over a diverse data span .

Using 'tight_layout' in Matplotlib optimizes the positioning of subplots or plots within a figure to prevent overlap of plot elements, such as axis labels, titles, or tick labels, thus enhancing the readability and visual appeal of the plot. This automated adjustment negates the need for manual customization and ensures visual elements do not clash .

The 'imshow' method in matplotlib is used to display data as an image on a 2D regular raster, displaying pixel values directly with optional colormap support. Meanwhile, 'matshow' is specifically tailored to plot the values of a 2D matrix or array as a color-coded image, which is particularly useful for visualizing correlations or Confusion matrices .

You might also like