0% found this document useful (0 votes)
9 views85 pages

Matplotlib: Python Data Visualization Guide

Uploaded by

ibrahimalvasec
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)
9 views85 pages

Matplotlib: Python Data Visualization Guide

Uploaded by

ibrahimalvasec
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

1

Matplotlib

(Code: Subhajit Das)

Data Visualization:

The process of finding trends and correlations in data by representing it pictorially is called Data
Visualization.
To perform data visualization in python, we can use various python data visualization modules such
as Matplotlib, Seaborn, Plotly, Ggplot.

What is Matplotlib:

➤ Matplotlib is a plotting library for the Python programming language and its numerical mathematics
extension NumPy.

➤ Matplotlib is a python library used for Data Visualization.

➤ Matplotlib is 2D and 3D plotting python library.

➤ It was introduced by john hunter in the year 2002.

Matplotlib Graphs:

1. Pairwise data: Plots of pairwise (x,y), tabular (var_0,...var_n), and functional f(x) = y data:

➤ Line plot: [Link](x, y)

➤ Scatter plot: [Link](x, y)

➤ Bar plot: [Link](x, y)

➤ Stem plot: [Link](x, y)

➤ Filled area between two horizontal curves: plt.fill_between(x, y1, y2)

➤ Stack plot: [Link](x, y)

➤ Step / Stairs plot: [Link](x) / [Link](x)


2

2. Statistical distributions: Plots of the distribution of at least one variable in a dataset. Some of
these methods also compute the distributions.

➤ Histogram: [Link](X)

➤ Box plot: [Link](X)

➤ Error bar plot: [Link](x, y, yerr, xerr)

➤ Violin plot: [Link](D)

➤ Event plot: [Link](D)

➤ Hist2D plot: plt.hist2D(x, y)

➤ Hexbin plot: [Link](x, y, C)

➤ Pie plot: [Link](x)

➤ Empirical Cumulative Distribution Function: [Link](x)


3

3. Gridded data: Plots of arrays and images Z i,j and fields U i,j, Vi,j on regular grids and
corresponding coordinate grids X i,j , Y i,j

➤ Display an image on the axes: [Link](Z)

➤ Create a pseudocolor plot of a 2-D array: [Link]([X, Y, Z]) / [Link](Z)

➤ Contour plot: [Link]([X, Y, Z]) / [Link](Z)

➤ Filled contour plot: [Link]([X, Y, Z]) / [Link](Z)

➤ Step plot: [Link](x, y)

➤ Barbs plot: [Link](X, Y, U, V)

➤ Quiver plot: [Link](X, Y, U, V)


4

4. Irregularly gridded data: Plots of data Z x,y on unstructured grids, unstructured coordinate grids
(x,y) and 2D functions f(x,y) = z

➤ Tricontour Plot: [Link](x, y, z)

➤ Tricontour filled with Plot: [Link](x, y, z)

➤ Tri Pcolor Plot: [Link](x, y, z)

➤ Tri Plot: [Link](x, y)

5. 3D and volumetric data: Matplotlib also supports 3D plots using the mpl_toolkits.mplot3d library.

➤ Scatter 3D: scatter(xs, ys, yz)


➤ Surface 3D: plot_surface(X, Y, Z) 5
➤ Trisurf 3D: plot_trisurf(x, y, z)

➤ Voxels 3D: voxels([x, y, z], field)

➤ Wireframe 3D: plot wireframe(X, Y, Z)

Install Matplotlib

In [1]: # pip install matplotlib

Classes of Matplotlib:

1. Figure Class: This is the top-level container in this hierarchy. It is the overall window/page that
everything is drawn on. Syntax: import [Link]
2. Axes Class: This is what we commonly think of as 'a plot'. A given figure can contain many Axes,
but a given Axes object can only be in one Figure¹. The Axes contains two (or three in the case of
3D) Axis objects which take care of the data limits. Syntax: import [Link]
3. Axis Class: These are the number-line-like objects and take care of setting the graph limits and
generating the ticks (the marks on the axis) and ticklabels (strings labeling the ticks). Syntax:
import [Link]
4. Artist Class: Basically, everything visible on the figure is an artist (even the Figure, Axes, and Axis
objects). This includes Text objects, Line2D objects, collections objects, Patch objects. Syntax:
import [Link]
5. Pyplot: Pyplot is a Matplotlib module that provides a MATLAB-like interface. Pyplot provides
functions that interact with the figure i.e., creates a figure, decorates the plot with labels, etc.
Syntax: import [Link]
6. Backend Layer Classes: These include FigureCanvas (the surface on which the figure will be
drawn), Renderer (the class that takes care of drawing on the surface), and Event (handles mouse
and keyboard events). Syntax: import matplotlib
6
Import Matplotlib

In [2]: import [Link] as plt


# from matplotlib import pyplot as plt
from mpl_toolkits import mplot3d

import pandas as pd
import numpy as np

Linking the dataset for Data Visualizations

In [3]: file = pd.read_csv('/content/drive/MyDrive/ML and DL DataSets/3.2_NSCA_DC Matplotlib



[Link](4)
Out[3]: Not_AvailableME_of_Responder NUMBER_OF_MEMBERS_IN_HOUSEHOLD AVG_HOUSEHOLD_INCOME

206 Ashish Chetri 3 50000

207 Ramesh BalakrishNot_Availablen 5 90000

208 Aaditya Varma 7 40000

209 Akhil Sharma 3 25000

4 rows × 25 columns
7
In [4]: # Getting the datatype of column
print([Link]())

<class '[Link]'>
RangeIndex: 210 entries, 0 to 209
Data columns (total 25 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Not_AvailableME_of_Responder 210 non-null object
1 NUMBER_OF_MEMBERS_IN_HOUSEHOLD 210 non-null int64
2 AVG_HOUSEHOLD_INCOME 210 non-null int64
3 HOUSE_ADDRESS 210 non-null object
4 AGE_OF_MEMBERS 210 non-null object
5 MALE_MEMBERS 210 non-null int64
6 FEMALE_MEMBERS 210 non-null int64
7 NO_OF_CHILDREN 210 non-null int64
8 STUDYING_CHILDREN 210 non-null int64
9 NON_STUDYING_CHILDREN 210 non-null int64
10 FOOD_CONSUMPTION_PATTERNS 208 non-null object
11 EXPENDITURES 210 non-null int64
12 HOUSEHOLD_ASSETS 210 non-null object
13 Fridge 210 non-null object
14 AC 210 non-null object
15 Personal_Vehicle 210 non-null object
16 Washing_Machine 210 non-null object
17 TV 210 non-null object
18 SOURCES_OF_WATER 209 non-null object
19 AVAILABILITY_OF_SMART_DEVICES 210 non-null object
20 Desktop_Laptop 210 non-null object
21 Phone 210 non-null object
22 Other_SmartDevices 210 non-null object
23 ACCESS_TO_INTERNET 210 non-null object
24 ANY_PREDOMINANT_AILMENT 210 non-null object
dtypes: int64(8), object(17)
memory usage: 41.1+ KB
None
8
In [5]: #to check if the dataset has any missing value
[Link]().sum()
Out[5]: Not_AvailableME_of_Responder 0
NUMBER_OF_MEMBERS_IN_HOUSEHOLD 0
AVG_HOUSEHOLD_INCOME 0
HOUSE_ADDRESS 0
AGE_OF_MEMBERS 0
MALE_MEMBERS 0
FEMALE_MEMBERS 0
NO_OF_CHILDREN 0
STUDYING_CHILDREN 0
NON_STUDYING_CHILDREN 0
FOOD_CONSUMPTION_PATTERNS 2
EXPENDITURES 0
HOUSEHOLD_ASSETS 0
Fridge 0
AC 0
Personal_Vehicle 0
Washing_Machine 0
TV 0
SOURCES_OF_WATER 1
AVAILABILITY_OF_SMART_DEVICES 0
Desktop_Laptop 0
Phone 0
Other_SmartDevices 0
ACCESS_TO_INTERNET 0
ANY_PREDOMINANT_AILMENT 0
dtype: int64

In [6]: # Male members in every household and that numbers


male = file.MALE_MEMBERS.value_counts()
print(male)

2 88
1 65
3 36
4 9
0 7
5 4
6 1
Name: MALE_MEMBERS, dtype: int64

In [7]: # Max male members in household


max = file.MALE_MEMBERS.value_counts().max()
print(max)

88

In [8]: # Viewing the particular number of male member


par = (file.MALE_MEMBERS == 4).sum()
print(par)

Linking another dataset for visualization


9
In [9]: df_car = pd.read_csv('/content/drive/MyDrive/ML and DL DataSets/3.3_CarPriceData_Matp

df_car.head(4)
Out[9]: Horsepo
ID Company Model Type Fuel Transmission Engine Mileage Kms_driven Buyers
(

0 1 Maruti Alto Hatchback Petrol Manual 796 19.7 45000 2

Wagon
1 2 Maruti Hatchback Petrol Manual 998 20.5 40005 2
R

Wagon
2 3 Maruti Hatchback Petrol Manual 998 20.5 40005 2
R

3 4 Maruti Ertiga MUV Petrol Automatic 1462 18.5 28000 2

Here are some of the functions in [Link] that are used to set properties and
customizing the appearance of plot:

1. [Link](): This function is used to set a title for the plot.


2. [Link](): This function is used to set a label for the x-axis.
3. [Link](): This function is used to set a label for the y-axis.
4. [Link](): This function is used to get or set some axis properties.
5. [Link](): This function is used to get or set the current tick locations and labels of the x-axis.
6. [Link](): This function is used to get or set the current tick locations and labels of the y-axis.
7. [Link](): This function is used to configure the grid lines.
8. [Link](): This function places a legend on the axes.
9. [Link](): This function creates a new figure, or activates an existing figure.
10. [Link](): This function displays a figure.
11. [Link](): This will show a color pallate in scatter plot

Line Plot

Syntax: [Link](x,y), where x and y can be list or tuple

The [Link]() function in [Link] has the following parameters:

1. args: This is a variable length argument, allowing for multiple x, y pairs with an optional format
string.
2. scalex, scaley: These are optional boolean values that, when set to True (default), will autoscale
the x and/or y axis.
3. data: This is an optional parameter where you can pass a DataFrame.
4. color: color or list of color, optional. The colors of the line faces.
5. linewidth: float or array-like, optional. Width of the line edges.
6. label: str or list of str, optional. A label for the whole dataset or individual lines.
10
In [10]: fig = [Link](figsize=(3, 2), dpi=150)
name = ('Subha', 'Subhajit Das', 'Subhajit')
marks = [96, 80, 60]

[Link](name, marks,
color = 'y',
label = 'Marks',
linewidth = 4,
scalex=True, # If, it's falese x axis line will not visible, by default it's
scaley=True) # If, it's falese x axis line will not visible, by default it's

[Link]()

Out[10]: <[Link] at 0x7931c58fdf60>


11
In [11]: # Using *args concept

# evenly sampled time at 200ms intervals
t = [Link](0., 5., 0.2)

# red dashes, blue squares and green triangles
[Link](t, t, 'r--',
t, t**2, 'bo',
t, t**3, 'g^')

''' The first character ('r', 'b', 'g') specifies the color: red, blue, green.
The second character ('--', 's', '^', 'o') specifies the line type or marker style: d

[Link]()
12
In [12]: # Using data function
fig = [Link](figsize=(3, 2), dpi=150)
# Define data
data = {'a': [1, 2, 3, 4], 'b': [2, 3, 5, 7]}

# Plot 'b' against 'a'
[Link]('a', 'b', data=data)
[Link]()

<ipython-input-12-bbdd178fa8c6>:7: RuntimeWarning: Second argument 'b' is ambiguous:


could be a format string but is in 'data'; using as data. If it was intended as dat
a, set the format string to an empty string to suppress this warning. If it was int
ended as a format string, explicitly pass the x-values as well. Alternatively, rena
me the entry in 'data'.
[Link]('a', 'b', data=data)
13
In [13]: # Using multiple line plot in a single graph
fig = [Link](figsize=(3, 2), dpi=150)
# Define data
sub = ["Machine Learning", "Maths", "Deep Learning", "NLP"]
std1 = [92, 88, 60, 72]
std2 = [65, 78, 96, 85]

[Link](sub, std1, label='Student 1')
[Link](sub, std2, label='Student 2')
[Link]()
Out[13]: <[Link] at 0x7931c3736d40>
14
In [14]: date = ('21/11/2022', '14/03/2022', '26/08/2023')
Cholestoral_Total = [181, 99, 86]
Trigllycarides = [109, 92, 69]
HDL_Cholestoral = [51, 39, 35]
LDL_Cholestoral = [109, 41, 37]
VLDL_Cholestoral = [22, 18, 14]
Non_HDL_Cholestoral = [131, 60, 51]

[Link](date, Cholestoral_Total, marker='o', color = 'y', label = 'Cholestoral, Tota
[Link](date, Trigllycarides, marker='o', color = 'r', label = 'Trigllycarides')
[Link](date, HDL_Cholestoral, marker='o', color = 'g', label = 'HDL Cholestoral')
[Link](date, LDL_Cholestoral, marker='o', color = 'b', label = 'LDL Cholestoral')
[Link](date, VLDL_Cholestoral, marker='o', color = 'deeppink', label = 'VLDL Choles
[Link](date, Non_HDL_Cholestoral, marker='o', color = 'purple', label = 'Non-HDL Ch

[Link]()
Out[14]: <[Link] at 0x7931c35b7d00>
15
In [15]: fig = [Link](figsize=(3, 2), dpi=150)
# Viewing male and female in line graph

x = file.MALE_MEMBERS.value_counts().values
y = file.FEMALE_MEMBERS.value_counts().values
num = [0, 1, 2, 3, 4, 5, 6]

[Link](num, x, color='b', label= 'Male')
[Link](num, y, color='r', label= 'Female')

[Link]()
Out[15]: <[Link] at 0x7931c3657910>

Scatter Plot

Syntax: [Link](x,y), where x and y can be list or tuple

The [Link]() function in [Link] has the following parameters:

1. x, y: These are arrays or sequences of n numbers representing the data positions. This defines dot
size
2. s: This is the marker size in points**2. It can be a scalar or an array-like shape (n,), and is optional.
3. c or color: This represents the marker colors. It can be an array-like or list of colors or color, and is
optional.
4. marker: This is the marker style. It can be either an instance of the class or the text shorthand for a
particular marker.
5. cmap: This is the Colormap instance or registered colormap name used to map scalar data to
colors.
6. norm: This is the normalization method used to scale scalar data to the [0, 1] range before mapping
to colors using cmap.
7. vmin, vmax: These are used in conjunction with norm to normalize luminance data.
8. alpha: This is the alpha blending value, between 0 (transparent) and 1 (opaque).
9. linewidths: This is the linewidth of the marker edges.
10. edgecolors: This is the color or sequence of colors of the marker edges.
11. plotnonfinite: This boolean value is used to indicate whether to plot the non-finite x, y values.
12. data: This is an optional parameter that can be a DataFrame.
16
In [16]: fig = [Link](figsize=(3, 2), dpi=150)

name = ('Subha', 'Subhajit Das', 'Subhajit')
marks = [96, 80, 60]

[Link](name, marks)
Out[16]: <[Link] at 0x7931c34e3040>
17
In [17]: # Using the parameters of [Link]()
fig = [Link](figsize=(3, 2), dpi=150)

name = ('Subha', 'Subhajit Das', 'Subhajit')
marks = [96, 80, 60]

c = ['r', 'g', 'b']# pass tuple or list for color


[Link](name, marks,
color = c,
s = 80, # Can also pass a specific color
marker='*', # ‘o’ for a circle, ‘s’ for a square, ‘*’ for a star marker, etc
alpha = 0.6,
edgecolor = 'black',
linewidth = 2,
plotnonfinite=True) # not that much imp
[Link]()
Out[17]: <[Link] at 0x7931c3562f50>
18
In [18]: # Using vmin, vmax and norm parameter
import [Link] as mcolors

fig = [Link](figsize=(3, 2), dpi=150)

N = 40
x = [Link](N)
y = [Link](N)

colors = [Link](N)
n = [Link](vmin=-2.,vmax=2.) # We have to use, mcolors to use, vmin and vm

[Link](x, y, c=colors, norm=n, cmap='viridis') # In cmap we have huge color comb
[Link]()
[Link]()
19
In [19]: # Using data parameter
fig = [Link](figsize=(3, 2), dpi=150)
df = [Link]({
'a': [Link](1000),
'b': [Link](1000),
'c': [Link](1000)
})

[Link]('a', 'b', c='c', data=df) # The x and y coordinates of the points are the
# and the colors of the points are specified by
[Link]()

In [20]: # Using multiple scatter plot in a single graph


fig = [Link](figsize=(3, 2), dpi=150)
# Define data
sub = ["Machine Learning", "Maths", "Deep Learning", "NLP"]
std1 = [92, 88, 60, 72]
std2 = [65, 78, 96, 85]

# Plot std1 and std2 against sub
[Link](sub, std1, label='Student 1')
[Link](sub, std2, label='Student 2')

[Link]()
Out[20]: <[Link] at 0x7931c3321d20>
20
Bar Plot

Syntax: [Link](x,y), where x and y can be list or tuple

The [Link]() function in [Link] has the following parameters:

1. x: Float or array-like. The x coordinates of the bars.


2. height / y: Float or array-like. The heights of the bars.
3. width: Float or array-like, default: 0.8. The widths of the bars.
4. bottom: Float or array-like, default: 0. The y coordinates of the bottom sides of the bars.
5. align: {'center', 'edge'}, default: 'center'. Alignment of the bars to the x coordinates.
6. color: color or list of color, optional. The colors of the bar faces.
7. edgecolor: color or list of color, optional. The colors of the bar edges.
8. linewidth: float or array-like, optional. Width of the bar edges.
9. linestyle: Style of the bar graph. It can be "solid", "dotted", "dashed" or "dashdot". By default it's
solid.
10. tick_label: str or list of str, optional. The tick labels of the bars.
11. label: str or list of str, optional. A label for the whole dataset or individual bars.
12. alpha: It's control the opacity of bar graph
13. xerr, yerr: float or array-like of shape (N,) or shape (2, N), optional. Add horizontal/vertical errorbars
to the bar tips.

In [21]: fig = [Link](figsize=(3, 2), dpi=150)


name = ('Subha', 'Subhajit Das', 'Subhajit')
marks = [96, 80, 60]

[Link](name, marks)
Out[21]: <BarContainer object of 3 artists>
21
In [22]: # Using the parameters of [Link]()
name = ('Subha', 'Subhajit Das', 'Subhajit')
marks = [96, 80, 60]

b = (2, 1, 2) # we have to pass tuple or list for buttom
c = ['r', 'g', 'b']# pass tuple or list for color
tl = ('Name: Subha', 'Name: Subhajit Das', 'Name: Subhajit')
l = ('Subha', 'Subhajit Das', 'Subhajit')

[Link](name, marks,
bottom= b,
align = 'edge',
width = 0.6, # Can also pass width of tuple or list
color = c, # Can also pass a specific color
alpha = 0.6, # Can also pass alpha of tuple or list
edgecolor = 'black', # Can also pass color of tuple or list
linewidth = 2, # Can also pass linewidth of tuple or list
linestyle = "dotted",
tick_label = tl,
label = l)

[Link](loc = 'center') # We have to use this, when we are using label, Center is f
# Also we have 'upper left', 'upper right', 'lower left', 'lower right', 'upper cente

Out[22]: <[Link] at 0x7931c33e3e20>


22
In [23]: fig = [Link](figsize=(3, 2), dpi=150)
# Using xerr and yerr
name = ('Subha', 'Subhajit Das', 'Subhajit')
marks = [96, 80, 60]

x = (1, 2, 3)
y = (3, 2, 1)

[Link](name, marks, xerr = x, yerr = y)
Out[23]: <BarContainer object of 3 artists>

In [24]: fig = [Link](figsize=(3, 2), dpi=150)


# Creating multiple bar graph which are overlaping
name = ('Subha', 'Subhajit Das', 'Subhajit')
marks = [96, 80, 60]
total = [450, 420, 375]

[Link](name, total, color = 'y', width = 0.6, label = 'Total Marks')
[Link](name, marks, color = 'r', width = 0.6, label = 'Computer Marks')
[Link]()
Out[24]: <[Link] at 0x7931c3404ee0>
23
In [25]: fig = [Link](figsize=(3, 2), dpi=150)
# Creating multiple horizontal bar graph, which are separate
name = ('Subha', 'Subhajit Das', 'Subhajit')
marks = [96, 80, 60]
total = [450, 420, 375]

width = 0.4
n1 = [Link](len(name))
n2 = [i+width for i in n1]

[Link](n1+width/2, name, rotation = 10) # To print the names

[Link](n1, total, width, color = 'y', label = 'Total Marks')
[Link](n2, marks, width, color = 'r', label = 'Computer Marks')
[Link]()
Out[25]: <[Link] at 0x7931c1959360>
24
In [26]: # Creating multiple horizontal bar graph, which are separate
date = ('21/11/2022', '14/03/2022', '26/08/2023')
Cholestoral_Total = [181, 99, 86]
Trigllycarides = [109, 92, 69]
HDL_Cholestoral = [51, 39, 35]
LDL_Cholestoral = [109, 41, 37]
VLDL_Cholestoral = [22, 18, 14]
Non_HDL_Cholestoral = [131, 60, 51]

width = 0.14
n1 = [Link](len(date))
n2 = [i+width for i in n1]
n3 = [i+width for i in n2]
n4 = [i+width for i in n3]
n5 = [i+width for i in n4]
n6 = [i+width for i in n5]

[Link](n1+width/2, date, rotation = 10) # To print the dates

[Link](n1, Cholestoral_Total, width, color = 'y', label = 'Cholestoral, Total')
[Link](n2, Trigllycarides, width, color = 'r', label = 'Trigllycarides')
[Link](n3, HDL_Cholestoral, width, color = 'g', label = 'HDL Cholestoral')
[Link](n4, LDL_Cholestoral, width, color = 'b', label = 'LDL Cholestoral')
[Link](n5, VLDL_Cholestoral, width, color = 'deeppink', label = 'VLDL Cholestoral')
[Link](n6, Non_HDL_Cholestoral, width, color = 'purple', label = 'Non-HDL Cholestor

[Link]().invert_yaxis() # To reverse the graph
[Link]()
[Link](color = 'gray')
25
In [27]: # Calculate Total income and expenses and create the bar graph
fig = [Link](figsize=(2, 3), dpi=150) # DPI stands for “Dots Per Inch”. It is us

total_income = file['AVG_HOUSEHOLD_INCOME'].sum()
total_expense = file['EXPENDITURES'].sum()
money = 'Income and Expense in Rs.'

print(f'Total income is: {total_income}')
print(f'Total expense is: {total_expense}')
print()

[Link](money, total_income, color = 'b', width = 0.4, label = 'Total Income')
[Link](money, total_expense, color = 'g', width = 0.4, label = 'Total Expense')

[Link](loc = 'lower center')

Total income is: 11743000


Total expense is: 7285500

Out[27]: <[Link] at 0x7931c1958cd0>


26
In [28]: fig = [Link](figsize=(3, 2), dpi=150)
# Create income and expense chart separetly
money = ['Income', 'Expenses']
total = [total_income, total_expense]
c = ['b', 'g']

[Link](money,total, color = c)
Out[28]: <BarContainer object of 2 artists>

Stem Plot

Syntax: [Link](x,y) / [Link](y,x), where x and y can be list or tuple

The [Link]() function in [Link] has the following parameters:

1. locs, x/y: The x or y positions of the stem heads.


2. heads, x/y: The y or x positions of the stem heads.
3. linefmt: A string defining the color and style of the vertical lines.
4. markerfmt: A string defining the color and style of markers.
5. basefmt: A format for the baseline.
6. bottom: The y-position of the baseline.
7. use_line_collection: A boolean value indicating whether to create the stem lines as a
LineCollection instead of individual lines.
8. orientation: If 'vertical', will produce a plot with stems oriented vertically, If 'horizontal', the stems
will be oriented horizontally.
27
In [29]: fig = [Link](figsize=(3, 2), dpi=150)
# Horizontal stem plot
sub = ["Machine Learning", "Maths", "Deep Learning", "NLP"]
std1 = [92, 88, 60, 72]

[Link](sub, std1)
Out[29]: <StemContainer object of 3 artists>

In [30]: fig = [Link](figsize=(3, 2), dpi=150)


# Vertical stem plot
sub = ["Machine Learning", "Maths", "Deep Learning", "NLP"]
std1 = [92, 88, 60, 72]

[Link](std1, sub)
Out[30]: <StemContainer object of 3 artists>
28
In [31]: fig = [Link](figsize=(3, 2), dpi=150)
# With negative plotting
num = [0, 1, 2, 3]
x = [20, 40, 60, 35]
y = [-40, -45, -30, -55]

[Link](num, x)
[Link](num, y)
Out[31]: <StemContainer object of 3 artists>
29
In [32]: fig = [Link](figsize=(3, 2), dpi=150)

# Parameters in stem plot
sub = ["Machine Learning", "Maths", "Deep Learning", "NLP"]
std1 = [92, 88, 60, 72]

[Link](sub, std1,
linefmt='r-.', # The first character ('r', 'b', 'g') specifies the color:
markerfmt='bs', # The second character ('--', 's', '^', 'o', '-.', ':') spe
bottom=1, # Ploting from 1
use_line_collection=True, # By default it's true. If it's false it will chan
basefmt='k--', # By default it's 'C3-'. 'k--', which means that the baseli
orientation ='vertical', # By default it's 'vertical'
label = 'Marks')

[Link]()

<ipython-input-32-e8cfc6e2531b>:7: MatplotlibDeprecationWarning: The 'use_line_colle


ction' parameter of stem() was deprecated in Matplotlib 3.6 and will be removed two
minor releases later. If any parameter follows 'use_line_collection', they should be
passed as keyword, not positionally.
[Link](sub, std1,

Out[32]: <[Link] at 0x7931c371b1c0>


30
In [33]: fig = [Link](figsize=(3, 2), dpi=150)

# Viewing male and female stem graph
x = file.MALE_MEMBERS.value_counts().values
y = file.FEMALE_MEMBERS.value_counts().values
num = [0, 1, 2, 3, 4, 5, 6]

# Create a vertical stem plot.
[Link](num, x)
[Link](num, y)
Out[33]: <StemContainer object of 3 artists>

Filled area between two horizontal curves:

plt.fill_between(x, y1, y2)/ [Link](x,y) plt.fill_between(), where x and y can be list or tuple

The plt.fill_between() function in [Link] has the following parameters:

1. x: An array of length N representing the x-coordinates of the nodes defining the curves.
2. y1: An array of length N or a scalar representing the y-coordinates of the nodes defining the first
curve.
3. y2: An array of length N or a scalar (default: 0) representing the y-coordinates of the nodes defining
the second curve.
4. color: Color or array-like of color, default: None.
5. where: An array of boolean values of length N (optional) used to exclude some horizontal regions
from being filled.
6. interpolate: An optional boolean parameter (default: False) that is relevant if where is used and the
two curves are crossing each other.
7. step: An optional parameter that accepts one of the three values: 'pre', 'post', or 'mid'. It specifies
where the steps will occur.
31
In [34]: fig = [Link](figsize=(3, 2), dpi=150)

# Filled area between
sub = ["Machine Learning", "Maths", "Deep Learning", "NLP"]
std1 = [92, 88, 60, 72]

[Link](sub, std1)
plt.fill_between(sub, std1)
Out[34]: <[Link] at 0x7931c1718670>

In [35]: fig = [Link](figsize=(3, 2), dpi=150)



# parameters used inside plt.fill_between()
sub = ["Machine Learning", "Maths", "Deep Learning", "NLP"]
std1 = [92, 88, 60, 72]

[Link](sub, std1, color='b')
plt.fill_between(sub, std1,
color= 'orange',
step='mid') # We have: pre, post and mid
Out[35]: <[Link] at 0x7931c1762b30>
32
In [36]: fig = [Link](figsize=(3, 2), dpi=150)

# Using where function
x = [Link]([1, 2, 3, 4, 5])
y = [Link]([1, 2, 3, 4, 5])

[Link](x,y, color='r')

plt.fill_between(x, y,
color='g',
where = (x>=2) & (x<=4)) # For this calculation, we can't do it in l
[Link]()

In [37]: fig = [Link](figsize=(3, 2), dpi=150)



# Filled area between two horizontal curves
sub = ["Machine Learning", "Maths", "Deep Learning", "NLP"]
std1 = [92, 88, 60, 72]
std2 = [65, 78, 96, 85]

plt.fill_between(sub, std1, std2)
[Link]()
[Link]()
33
In [38]: fig = [Link](figsize=(3, 2), dpi=150)

# Viewing male and female in fill between graph
x = file.MALE_MEMBERS.value_counts().values
y = file.FEMALE_MEMBERS.value_counts().values
num = [0, 1, 2, 3, 4, 5, 6]

[Link](num, x, color='r', label= 'Male')
[Link](num, y, color='b', label= 'Female')

plt.fill_between(num, x, y, color= 'g')
[Link]()
[Link]()
Out[38]: <[Link] at 0x7931c1673790>

Stack Area Plot:

[Link](x, y), where x and y can be list or tuple

The [Link]() function in [Link] has the following parameters:

1. x: a 1-D array of values.


2. y: a 2-D array of values. The data is assumed to be unstacked. Each of the following calls is legal:
stackplot(x, y) where y has shape (M, N) or stackplot(x, y1, y2, y3) where y1, y2, y3, y4 have length
N.
3. labels: A sequence of labels to assign to each data series.
4. colors: A sequence of colors to be cycled through and used to color the stacked areas.
5. baseline: Method used to calculate the baseline. It can be ‘zero’, ‘sym’, ‘wiggle’, 'weighted_wiggle’.
6. data: If given, all parameters also accept a string s, which is interpreted as data [s] (unless this
raises an exception).

The baseline parameter is used to denote the method used to calculate the baseline:

zero: Constant zero baseline, i.e., a simple stacked plot.


sym: Symmetric around zero and is sometimes called 'ThemeRiver’.
wiggle: Minimizes the sum of the squared slopes.
weighted_wiggle: Does the same but weights to account for the size of each layer. It is also called
‘Streamgraph’-layout.
34
In [39]: fig = [Link](figsize=(3, 2), dpi=150)

# Single Stack Plot
sub = ["Machine Learning", "Maths", "Deep Learning", "NLP"]
std1 = [92, 88, 60, 72]

[Link](sub, std1)
Out[39]: [<[Link] at 0x7931c150b190>]

In [40]: fig = [Link](figsize=(3, 2), dpi=150)



# Viewing male and female in Stack plot (multiple plot)
x = file.MALE_MEMBERS.value_counts().values
y = file.FEMALE_MEMBERS.value_counts().values
num = [0, 1, 2, 3, 4, 5, 6]

[Link](num, x, y)
[Link]()
35
In [41]: fig = [Link](figsize=(3, 2), dpi=150)

# Using parameters in the graph
x = file.MALE_MEMBERS.value_counts().values
y = file.FEMALE_MEMBERS.value_counts().values
num = [0, 1, 2, 3, 4, 5, 6]

l = ['Male', 'Female']
c = ['g','r']

[Link](num, x, y,
labels = l,
colors = c,
baseline = 'weighted_wiggle')

[Link]()
[Link]()
Out[41]: <[Link] at 0x7931c13d1b10>

Step Plot / Stair Plot

Syntax: [Link](x,y) / [Link](y,x), where x and y can be list or tuple

The [Link]() function in [Link] has the following parameters:

1. x: An 1-D array of values representing the X-axis.


2. y: An 1-D array of values representing the Y-axis.
3. fmt: A formatting string that specifies the line color, marker type, etc.
4. data: Two iterables containing the label names for labeled data.
5. where: A parameter used to decide the position of the vertical line, which can take one of the
following values: 'pre', 'post', or 'mid'.
6. marker: A string defining the color and style of markers.
7. ms: Will define the marker size
8. mfc: Will define the marker color
36
In [42]: # Step plot
sub = ["Machine Learning", "Maths", "Deep Learning", "NLP"]
std1 = [92, 88, 60, 72]

[Link](sub, std1)
Out[42]: [<[Link].Line2D at 0x7931c143f670>]

In [43]: fig = [Link](figsize=(3, 2), dpi=150)



# With negative plotting
num = [0, 1, 2, 3]
x = [20, 40, 60, 35]
y = [-40, -45, -30, -55]


[Link](num, x)
[Link](num, y)
Out[43]: [<[Link].Line2D at 0x7931c12a7970>]
37
In [44]: fig = [Link](figsize=(3, 2), dpi=150)

# Parameters in stem plot
sub = ["Machine Learning", "Maths", "Deep Learning", "NLP"]
std1 = [92, 88, 60, 72]

[Link](std1, sub,
'r--', # Also we can use other fmt, and can use color parameter also
where='mid', # We have: pre, post and mid
marker= 'o',
ms= 12,
mfc= 'g',
label= 'Marks')

[Link]()
[Link]() # It will show grid in background
38
In [45]: fig = [Link](figsize=(3, 2), dpi=150)

# Viewing male and female step graph
x = file.MALE_MEMBERS.value_counts().values
y = file.FEMALE_MEMBERS.value_counts().values
num = [0, 1, 2, 3, 4, 5, 6]

# Create a step plot.
[Link](num, x, label = 'Male')
[Link](num, y, label = 'Female')

[Link]()
[Link]() # It will show grid in background

2. Statistical distributions

Histogram:

[Link](x)

The [Link]() function in [Link] has the following parameters:

1. x: This is the sequence of data that you want to plot in the histogram.
2. bins: This can be an integer, sequence, or string. If an integer, it defines the number of equal-width
bins in the range. If a sequence, it defines the bin edges.
3. range: This is the lower and upper range of the bins.
4. density: If True, the function will draw and return a probability density.
5. weights: This is an array of weights, of the same shape as x.
6. cumulative: If True, then a histogram is computed where each bin gives the counts in that bin plus
all bins for smaller values.
7. bottom: This is the location of the bottom baseline of each bin.
8. histtype: This parameter is used to draw type of histogram. Options include ‘bar’, ‘barstacked’,
‘step’, 'stepfilled’.
9. align: This controls how the histogram is plotted. Options include ‘left’, ‘mid’, 'right’.
10. orientation: This controls the orientation of the histogram. Options include ‘horizontal’, 'vertical’.
11. rwidth: This is the relative width of the bars as a fraction of the bin width.
12. log: If True, the histogram axis is set to a log scale.
13. edgecolors: This is the color or sequence of colors of the marker edges.
14. color: This is a color spec or sequence of color specs, one per dataset.
15. label: This is a string, or sequence of strings to match multiple datasets.
16. stacked: If True, multiple data are stacked on top of each other If false multiple data are arranged 39
side by side if histtype is ‘bar’ or on top of each other if histtype is 'step’.
17. data: This parameter is used when the x values are a string, in which case this should be a data
object that the string can be looked up in.

In [46]: fig = [Link](figsize=(3, 2), dpi=150)



# Avg household income of the persons in the locality
inc = file.AVG_HOUSEHOLD_INCOME.value_counts().values

[Link](inc)
Out[46]: (array([25., 5., 2., 1., 3., 2., 1., 0., 2., 1.]),
array([ 1. , 3.2, 5.4, 7.6, 9.8, 12. , 14.2, 16.4, 18.6, 20.8, 23. ]),
<BarContainer object of 10 artists>)
40
In [47]: # Using parameters
inc = file.AVG_HOUSEHOLD_INCOME.value_counts().values

[Link]("Avg Household Income")
[Link]("Income")
[Link]("Num of Persons")

[Link](inc,
bins= 8, # means that the range will be divided into 10 equal-width bins
range=(0, 28),
density= True,
cumulative= True, # Default: False, True will reverse the graph
histtype= 'barstacked', # 'bar’, ‘barstacked’, ‘step’, 'stepfilled’ also ava
alpha= 1,
align= 'mid', # 'left’, ‘mid’, 'right’ also avail
orientation= 'horizontal', # Default: Vertical
rwidth= 0.8,
edgecolor= 'r',
color= 'g',
stacked= False)
[Link]()
41
In [48]: # Using the parameters of contour
x = file.MALE_MEMBERS.value_counts().values
y = file.FEMALE_MEMBERS.value_counts().values

[Link](x, label = 'Male')
[Link](y, label = 'Female')

[Link](30, color= 'r', label = 'Gender Line') # Line inside a chart
[Link]()
Out[48]: <[Link] at 0x7931c361ae60>

Box plot:

[Link](X)

The [Link]() function in [Link] has the following parameters:


1. data: array or sequence of array to be plotted 42
2. notch: optional parameter accepts boolean values
3. vert: optional parameter accepts boolean values false and true for horizontal and vertical plot
respectively
4. bootstrap: optional parameter accepts int specifies intervals around notched boxplots
5. usermedians: optional parameter accepts array or sequence of array dimension compatible with
data
6. positions: optional parameter accepts array and sets the position of boxes
7. widths: optional parameter accepts array and sets the width of boxes
8. patch_artist: optional parameter having boolean values
9. labels: sequence of strings sets label for each dataset
10. meanline: optional having boolean value try to render meanline as full width of box
11. order: optional parameter sets the order of the boxplot
12. showmeans: It will show the mean position
13. sym: To change the outliers color
14. boxprops: To change the box color
15. capprops: To change the line color
16. whiskerprops: To change the whisker color

In [49]: # Printing the Reliance share price through box plot


ril = [2345, 2356, 2376, 2384]

[Link](ril, showmeans = True)
Out[49]: {'whiskers': [<[Link].Line2D at 0x7931c135a890>,
<[Link].Line2D at 0x7931c1359060>],
'caps': [<[Link].Line2D at 0x7931c135b4c0>,
<[Link].Line2D at 0x7931c135af50>],
'boxes': [<[Link].Line2D at 0x7931c135a7a0>],
'medians': [<[Link].Line2D at 0x7931c1359ed0>],
'fliers': [<[Link].Line2D at 0x7931c1359fc0>],
'means': [<[Link].Line2D at 0x7931c1359900>]}
43
In [50]: # Printing the Reliance share price with a outlier
ril = [2345, 2356, 2376, 2384, 2200]

[Link](ril,
sym='r+') # To change the color of outlier
Out[50]: {'whiskers': [<[Link].Line2D at 0x7931c13ae740>,
<[Link].Line2D at 0x7931c13ae080>],
'caps': [<[Link].Line2D at 0x7931c13ada50>,
<[Link].Line2D at 0x7931c13ac790>],
'boxes': [<[Link].Line2D at 0x7931c13aea40>],
'medians': [<[Link].Line2D at 0x7931c13adb10>],
'fliers': [<[Link].Line2D at 0x7931c13ad540>],
'means': []}
44
In [51]: # Using the parameters
ril = [2345, 2356, 2376, 2384]

[Link](ril,
notch = True,
vert = False, # Default vertical is True
widths = 0.4, # Default: 0.2
bootstrap = 1000, # confidence intervals around the median for notched bo
patch_artist = True, # Default: False
labels = ["Reliance Industry"],
boxprops = dict(color = 'r', facecolor = 'g'), # Facecolor will change th
capprops = dict(color = 'g'),
whiskerprops = dict(color = 'pink')
)
[Link]()
45
In [52]: # Plotting of multiple box plot

day_1 = [2356, 2382, 2422, 2428]
day_2 = [2360, 2364, 2375, 2391]
day_3 = [2345, 2356, 2376, 2384]

ril = [day_1, day_2, day_3]

[Link]("Reliance Industry Share Price")
[Link](ril,
patch_artist = True,
labels = ['20/09/2023', '21/09/2023', '22/09/2023'],
boxprops = dict(color = 'g', facecolor = 'r'))
[Link]()

Error bar plot:

[Link](x, y, yerr, xerr)


46
In [53]: # Create data
x_data = [0.1, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5]
y_data = [0.1, 0.2, 0.3, 0.5, 0.8, 1.3, 2.1, 3.4]
error = [0.05, 0.08, 0.12, 0.15, 0.20, 0.25, 0.30, 0.35]

# Create error bars
[Link](x_data, y_data, yerr=error, fmt='-o')
Out[53]: <ErrorbarContainer object of 3 artists>

Violin Plot:

[Link](D)
47
In [54]: # Create data
data = [[1, 2, 3, 4, 5], [2, 3, 4, 5, 6], [3, 4, 5, 6, 7], [4, 5, 6, 7, 8]]

# Create violin plot
[Link](data)
Out[54]: {'bodies': [<[Link] at 0x7931c1625db0>,
<[Link] at 0x7931c0fa2710>,
<[Link] at 0x7931c0fa2a70>,
<[Link] at 0x7931c0fa2dd0>],
'cmaxes': <[Link] at 0x7931c1168220>,
'cmins': <[Link] at 0x7931c0fa3430>,
'cbars': <[Link] at 0x7931c0fa3820>}

Hist 2D:

plt.hist2D(x, y)
48
In [55]: # Create data
[Link](0)
x = [Link](0, 1, 1000)
y = [Link](0, 1, 1000)

# Create 2D histogram
plt.hist2d(x, y, bins=(50, 50), cmap=[Link])

[Link]()
Out[55]: <[Link] at 0x7931c102b460>

Hexbin:

[Link](x, y)
49
In [56]: import [Link] as plt

# Sample data
x = [1, 2, 3, 4, 5, 2, 3, 4, 5, 6, 3, 4, 5, 6, 7, 4, 5, 6, 7, 8]
y = [1, 2, 3, 4, 5, 2, 3, 4, 5, 6, 3, 4, 5, 6, 7, 4, 5, 6, 7, 8]

# Create a hexbin plot
[Link](x, y, gridsize=30, cmap='Greens')

# Add a colorbar for reference
[Link]()

# Show the plot
[Link]()

Pie Plot

Syntax: [Link](x), where x can be list or tuple

The [Link]() function in [Link] has the following parameters:

1. x: 1D array-like. The wedge sizes.


2. explode: array-like, default: None. Specifies the fraction of the radius with which to offset each
wedge.
3. labels: list, default: None. A sequence of strings providing the labels for each wedge.
4. colors: color or array-like of color, default: None. A sequence of colors through which the pie chart
will cycle.
5. autopct: None or str or callable, default: None. If not None, autopct is a string or function used to
label the wedges with their numeric value.
6. pctdistance: float, default: 0.6. The relative distance along the radius at which the text generated
by autopct is drawn.
7. labeldistance: float or None, default: 1.1. The relative distance along the radius at which the labels
are drawn.
8. shadow: bool or dict, default: False. If bool, whether to draw a shadow beneath the pie.
9. startangle: float, default: 0 degrees. The angle by which the start of the pie is rotated, 50
counterclockwise from the x-axis.
10. radius: float, default: 1. The radius of the pie.
11. counterclock: bool, default: True. Specify fractions direction, clockwise or counterclockwise.
12. wedgeprops: dict, default: None. Dict of arguments passed to each [Link] of the pie.
13. textprops: dict, default: None. Dict of arguments to pass to the text objects.
14. center: (float, float), default: (0, 0). The coordinates of the center of the chart.
15. frame: bool, default: False. Plot Axes frame with the chart if true.
16. rotatelabels: bool, default: False.
17. hatch: str or list, default: None. Hatching pattern applied to all pie wedges or sequence of patterns
through which the chart will cycle.
18. normalize: bool, default: True .
19. data: array-like, optional.

In [57]: [Link]([1])
[Link]()
51
In [58]: # Number of memberss in household showing through pie chart and percentage
[Link](1, figsize=(4,4))

label=file.MALE_MEMBERS.value_counts().index # Use value_counts to get the frequency
count=file.MALE_MEMBERS.value_counts().values # Use value_counts to get the frequency

[Link](count, labels=label)
[Link]()
52
In [59]: # Using the parameters inside [Link]() graph
[Link](1, figsize=(6,6))

label=file.MALE_MEMBERS.value_counts().index
count=file.MALE_MEMBERS.value_counts().values

[Link]("Male members in the household", fontsize = 15, fontweight ="bold")

ex =[0.2, 0.1, 0, 0, 0, 0, 0]
co = ['r', 'g', 'b', 'c', 'm', 'y', 'k']

[Link](count,
explode= ex,
labels= label,
colors= co,
autopct= '%0.2f%%',
pctdistance= 0.85,
labeldistance= 1.2,
shadow= True,
startangle= 140,
radius= 4, # By default it's 1
counterclock= True,
wedgeprops={'linewidth': 2, 'edgecolor': 'black'}, # This is a dictionary of
textprops={'fontsize': 14}, # This is a dictionary of arguments. For example,
center=(2, 3), # The default value is (0, 0). For example, center = (2, 3) se
frame = True,
rotatelabels = True)

[Link](loc = 'upper right')
[Link]()
53
In [60]: # Number of memberss in household showing through donout chart and percentage
# We can also create donout chart, through [Link]().add_artist
[Link](1, figsize=(4,4))

label=file.FEMALE_MEMBERS.value_counts().index # Use value_counts to get the frequency
count=file.FEMALE_MEMBERS.value_counts().values # Use value_counts to get the frequen

[Link](count, labels=label, radius = 1.2)
[Link]([1], colors = 'w', radius = 0.6)
[Link]()
54
In [61]: # Creating multiple pie graph in a chart
[Link](1, figsize=(4,4))

label= file.MALE_MEMBERS.value_counts().index
male= file.MALE_MEMBERS.value_counts().values
female= file.FEMALE_MEMBERS.value_counts().values

co = ['r', 'g', 'b', 'c', 'm', 'y', 'k']

[Link](male, labels=label, radius = 1.2)
[Link](female, labels=label, radius = 0.8, colors = co)
[Link]()

3. Gridded data

Display an image on the axes:

[Link](Z)

The [Link]() function in [Link] has the following parameters:

1. X: This parameter is the data of the image.


2. cmap: This parameter is a colormap instance or registered colormap name.
3. norm: This parameter is the Normalize instance scales the data values to the canonical colormap
range [0, 1] for mapping to colors
4. vmin, vmax: These parameter are optional in nature and they are colorbar range.
5. alpha: This parameter is a intensity of the color.
6. aspect: This parameter is used to controls the aspect ratio of the axes.
7. interpolation: This parameter is the interpolation method which used to display an image.
8. interpolation_stage: controls the stage at which interpolation is performed. The default value is
"data"
9. origin: This parameter is used to place the [0, 0] index of the array in the upper left or lower left
corner of the axes.
10. resample: This parameter is the method which is used for resembling.
11. extent: This parameter is the bounding box in data coordinates.
12. filternorm: This parameter is used for the antigrain image resize filter.
13. filterrad: This parameter is the filter radius for filters that have a radius parameter.
14. url: This parameter sets the url of the created AxesImage.
55
In [62]: # Printing an image
robo = [Link]('/content/drive/MyDrive/ML and DL DataSets/3.1_Robo_img.jpg') # imr
[Link](robo) # to remove the axis, we can use: [Link]('off')

print(type(robo)) # type of the image
print([Link]) # shape of the image

<class '[Link]'>
(360, 544, 3)
56
In [63]: # Printing a cmap image
# In cmapscale, we can only print 2D image, so first we have to convert it into 2D
gray_robo = robo[:, :, 0]

[Link](gray_robo)
[Link]()
[Link]['[Link]'] # Getting default image size
Out[63]: [6.4, 4.8]
57
In [64]: # Showing image in different colors
color_robo = [Link]('/content/drive/MyDrive/ML and DL DataSets/3.1_Robo_img.jpg')
robo_2D = color_robo[:, :, 0] # Converting into 2D array, to use it in cmap

fig, axes = [Link](nrows = 2, ncols = 3, figsize = [Link](1/2)) # Figas
map = ['plasma', 'Wistia', 'gray', 'magma', 'jet', 'inferno']

for i, cmap in zip([Link], map):
[Link](robo_2D, cmap = cmap)
[Link](title = cmap)
[Link]('off')

[Link]()

In [65]: # Matplotlib imshow size


# Default size is [Link]['[Link]'] = [6.4, 4.8] inches

''' 1. [Link](figsize = (8, 6)), 8 inches wide and 6 inches tall
2. [Link](figsize = [Link](2)), twice as tall as it's wide '''
Out[65]: " 1. [Link](figsize = (8, 6)), 8 inches wide and 6 inches tall\n 2. [Link]
e(figsize = [Link](2)), twice as tall as it's wide "

In [66]: # Changing the size


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

[Link](robo) # to remove the axis, we can use: [Link]('off')
Out[66]: <[Link] at 0x7931c0d24430>
58
In [67]: # Create a 2D array with random values
Z = [Link]((10,10))

# Display the array
[Link](Z)
Out[67]: <[Link] at 0x7931c0cbfc40>
59
In [68]: # Using the parameters of imshow
Z = [Link]((10,10))

# Display the array
[Link](Z,
cmap = 'inferno',
norm = 'linear', # "linear", "log", "symlog", "logit", etc are avail
aspect = 'auto',
interpolation = 'blackman', # Default: 'antialiased'. Supports many interp
interpolation_stage = 'rgba', # Default: 'data'
alpha = 1.0,
vmin = 0.2,
vmax = 1.2,
origin = 'upper', # upper and lower. Default: upper
extent = None,
filternorm = True, # Default: True
filterrad = 3.2, # Default 4.0
resample = True) # Default: True
[Link]()
[Link](color = 'white')

Create a pseudocolor plot of a 2-D array:

[Link]([X, Y, Z]) / [Link](Z)

The [Link]() function in [Link] has the following parameters:

1. C: This parameter contains the values in 2D array which are to be color-mapped.


2. X, Y: These parameter are the coordinates of the quadrilateral corners.
3. cmap: This parameter is a colormap instance or registered colormap name.
4. norm: This parameter is the Normalize instance scales the data values to the canonical colormap
range [0, 1] for mapping to colors
5. vmin, vmax: These parameter are optional in nature and they are colorbar range.
6. alpha: This parameter is a intensity of the color.
7. snap: This parameter is used to snap the mesh to pixel boundaries.
8. edgecolors: This parameter is the color of the edges. {‘none’, None, ‘face’, color, color sequence} 60
9. shading: This parameter is the fill style. It can be flat or gouraud.

In [69]: fig = [Link](figsize=(4, 3), dpi=150)


# Create a 2D array with random values
Z = [Link]((10,10))

# Display the array
[Link](Z)
Out[69]: <[Link] at 0x7931c0f18070>
61
In [70]: fig = [Link](figsize=(3, 2), dpi=150)

# Viewing male and female in Colormesh plot
x = file.MALE_MEMBERS.value_counts().values
y = file.FEMALE_MEMBERS.value_counts().values
num = [0, 1, 2, 3, 4, 5, 6]

[Link]([x, y, num])
Out[70]: <[Link] at 0x7931c0ef7e80>
62
In [71]: fig = [Link](figsize=(4, 3), dpi=150)

# Using the parameters of Colormesh Plot
x = file.MALE_MEMBERS.value_counts().values
y = file.FEMALE_MEMBERS.value_counts().values
num = [0, 1, 2, 3, 4, 5, 6]

[Link]([x, y, num],
cmap = 'inferno',
norm = 'linear', # "linear", "log", "symlog", "logit", etc are avail
alpha = 1.0,
vmin = 0.2,
vmax = 1.2,
snap = True, # True means the mesh elements will be snapped to pixel bound
edgecolor = 'black',
shading='gouraud') # Default: 'flat'

[Link](color = 'g')
[Link]()
Out[71]: <[Link] at 0x7931c16015a0>

Contour plot:

[Link]([X, Y, Z]) / [Link](Z)

The [Link]() function in [Link] has the following parameters:

1. X, Y: These are array-like and optional. They represent the coordinates of the values in Z. X and Y
must both be 2D with the same shape as Z (e.g., created via [Link]).
2. Z: This is an array-like parameter that represents the height values over which the contour is drawn.
Color-mapping is controlled by cmap, norm, vmin, and vmax.
3. levels: This is an optional parameter that determines the number and positions of the contour
lines/regions.
4. corner_mask: This is a boolean parameter that enables/disables corner masking, which only has
an effect if Z is a masked array.
5. colors: This is an optional parameter that represents the colors of the levels, i.e., the lines for 63
contour.
6. alpha: This is an optional parameter that represents the alpha blending value, between 0
(transparent) and 1 (opaque).
7. vmin, vmax: These parameter are optional in nature and they are colorbar range.
8. cmap: This is an optional parameter that represents the Colormap instance or registered colormap
name used to map scalar data to colors.
9. linewidths: This parameter is used to specify the line width of the contour lines.
10. linestyles: This parameter is used to specify the style of the contour lines.
11. norm: This is an optional parameter that represents the normalization method used to scale scalar
data to the [0, 1] range before mapping to colors using cmap.

In [72]: fig = [Link](figsize=(3, 2), dpi=150)



# Get the axes object.
ax = [Link]()
# Call the `set_facecolor()` method on the axes object and pass in the desired backgr
ax.set_facecolor('pink') # Change the background color

# Create a 2D array with random values
Z = [Link]((10,10))

# Display the array
[Link](Z)
Out[72]: <[Link] at 0x7931c0ba7fa0>
64
In [73]: fig = [Link](figsize=(4, 3), dpi=150)

# Viewing male and female in Contour plot
x = file.MALE_MEMBERS.value_counts().values
y = file.FEMALE_MEMBERS.value_counts().values
num = [0, 1, 2, 3, 4, 5, 6]

ax = [Link]()
ax.set_facecolor("lightgreen")

[Link]([x, y, num])
Out[73]: <[Link] at 0x7931c0910d30>
65
In [74]: # Using the parameters of contour
x = file.MALE_MEMBERS.value_counts().values
y = file.FEMALE_MEMBERS.value_counts().values
num = [0, 1, 2, 3, 4, 5, 6]

con = [Link]([x, y, num],
levels = 20,
corner_mask= True, # Default: True, Enable/disable corner masking,
alpha = 1,
vmin = 38,
vmax = 98,
cmap = 'magma',
linewidths = 2, # Default: 1.5
linestyles = 'dashed', # 'solid', 'dashed', 'dashdot', 'dotted'
norm = 'log') # "linear", "log", "symlog", "logit", etc

[Link](con, inline= True, fontsize= 8) # Adding label in contour plot

Out[74]: <a list of 18 [Link] objects>

Filled contour plot:

[Link]([X, Y, Z]) / [Link](Z)

The [Link]() function in [Link] has the following parameters:

Parameter in [Link]() and [Link]() are almost same, there is no major differance.
Only, cntourf will help to fill the area.
66
In [75]: fig = [Link](figsize=(4, 2), dpi=150)

# Create a 2D array with random values
Z = [Link]((10,10))

# Display the array
[Link](Z)
Out[75]: <[Link] at 0x7931c08ae200>

In [76]: fig = [Link](figsize=(4, 2), dpi=150)



# Viewing male and female in Contour plot
x = file.MALE_MEMBERS.value_counts().values
y = file.FEMALE_MEMBERS.value_counts().values
num = [0, 1, 2, 3, 4, 5, 6]

[Link]([x, y, num])
Out[76]: <[Link] at 0x7931c0726380>
67
In [77]: fig = [Link](figsize=(4, 3), dpi=150)

#Using the parameters of contour
x = file.MALE_MEMBERS.value_counts().values
y = file.FEMALE_MEMBERS.value_counts().values
num = [0, 1, 2, 3, 4, 5, 6]

con = [Link]([x, y, num],
levels = 20,
corner_mask= True, # Default: True, Enable/disable corner masking,
alpha = 1,
cmap = 'inferno',
norm = 'log') # "linear", "log", "symlog", "logit", etc

[Link](con, inline= True, fontsize= 8) # Adding label in contour plot

Out[77]: <a list of 19 [Link] objects>

Streamplot:

[Link](X, Y, U, V)
68
In [78]: fig = [Link](figsize=(4, 3), dpi=150)

# Create a grid of points
x = [Link](-2, 2, 100)
y = [Link](-2, 2, 100)
X, Y = [Link](x, y)

# Create a simple vector field: a vortex centered at the origin
U = -Y
V = X

# Create a stream plot
[Link](X, Y, U, V, density=2, linewidth=1, arrowsize=1, cmap='viridis')
Out[78]: <[Link] at 0x7931c040e530>

3D and volumetric data


69
In [79]: ax = [Link](projection = '3d')

print("The dtype is: ",type(ax))
The dtype is: <class 'mpl_toolkits.mplot3d.axes3d.Axes3D'>

Scatter 3D Plot:

[Link](x, y, z), Here only numerical value will support. X, Y and Z can be, list, tuple or array

The [Link]() function in [Link] has the following parameters:

1. xs, ys, zs: These are arrays or sequences of n numbers representing the data positions in the x, y,
and z dimensions respectively.
2. s: This is the marker size in points**2. It can be a single value for all markers or a sequence of
values for each marker individually.
3. c: This is the marker color. It can be a single color format string, a sequence of color specifications
of length n, or a sequence of n numbers to be mapped to colors using the cmap and norm
parameters.
4. depthshade: This is a boolean value that determines whether or not to shade the scatter markers
to give the appearance of depth. Default is True.
70
In [80]: ax_eda = [Link](projection = '3d') # We have to specify this in every cell. Otherwi

x = file.MALE_MEMBERS.value_counts().values
y = file.FEMALE_MEMBERS.value_counts().values
num = [0, 1, 2, 3, 4, 5, 6]

ax_eda.scatter (x, y, num)
Out[80]: <mpl_toolkits.mplot3d.art3d.Path3DCollection at 0x7931c0e554e0>
71
In [81]: #3d plotting of Year and Buyers wrt Price
fig = [Link]()
ax_car = fig.add_subplot(111 , projection = '3d') # 111 argument is a 3-digit integer
# the second the number of columns,

x = df_car[['Year']]
y = df_car[['Buyers']]
z = df_car[['Price (Lakhs)']]

# Giving the label
ax_car.set_xlabel ('Year')
ax_car.set_ylabel ('Buyers')
ax_car.set_zlabel ('Price')

# Scatter plotting of graph
ax_car.scatter (x,y,z)

Out[81]: <mpl_toolkits.mplot3d.art3d.Path3DCollection at 0x7931c026e1a0>


72
In [82]: # Using the parameters of 3D scatter plot
fig = [Link]()
ax = fig.add_subplot(111, projection='3d')

x = [1, 2, 3, 4, 5]
y = [2, 3, 4, 5, 6]
z = [3, 4, 5, 6, 7]

[Link](x, y, z,
s = [20, 60, 100, 150, 200], # Size of the circle
c = ['red', 'green', 'blue', 'yellow', 'purple'], # Color of the circle
depthshade = False) # Default:True, it will control the opacity
ax.set_facecolor("pink")

Plot Surface:

plot_surface(X, Y, Z)
73
In [83]: # Create a figure
fig = [Link]()
# Create a 3D plot
ax = fig.add_subplot(111, projection='3d')

x = [1, 2, 3, 4, 5]
y = [1, 2, 3, 4, 5]
x, y = [Link](x, y) # We have to use this otherwise, it will throw error: 'list'
z = x+y

# Plot the data
ax.plot_surface(x, y, z)
ax.set_facecolor("#a5cf97") # For bckground color
74
In [84]: fig = [Link]()
ax = fig.add_subplot(111, projection='3d')

x = [Link](-5, 5, 100)
y = [Link](-5, 5, 100)
x, y = [Link](x, y) # ‘list’ object has no attribute ‘ndim’ typically occurs whe
# instead. The .ndim attribute is used to get the number of
z = [Link]([Link](x**2 + y**2))

ax.plot_surface(x, y, z, cmap='inferno')
ax.set_facecolor("#1CC4AF")
75
In [85]: #3d plotting of Year and Buyers wrt Price
fig = [Link]()
ax_car = fig.add_subplot(111 , projection = '3d') # 111 argument is a 3-digit integer
# the second the number of columns,

x = df_car[['Year']]
y = df_car[['Buyers']]
x, y = [Link](x, y) # meshgrid function is used to create a rectangular grid out
z = df_car[['Price (Lakhs)']]

# Giving the label
ax_car.set_xlabel ('Year')
ax_car.set_ylabel ('Buyers')
ax_car.set_zlabel ('Price')

ax_car.plot_surface (x,y,z, cmap='plasma')

Out[85]: <mpl_toolkits.mplot3d.art3d.Poly3DCollection at 0x7931bfd315a0>

Voxels 3D:

voxels([x, y, z], field)


76
In [86]: fig = [Link]()
ax = fig.add_subplot(projection='3d')

# Random data points between 0 and 1
data = [Link]([0, 1], size=(8, 12, 16), p=[0.80, 0.20]) # Size: Output shap
# p: The probabilit
# Plot the voxels
[Link](data, edgecolor="k", facecolors='green')
[Link]()

Plot Wireframe:

plot_wireframe(X, Y, Z)
77
In [87]: # Create a figure
fig = [Link]()
# Create a 3D plot
ax = fig.add_subplot(111, projection='3d')

x = [1, 2, 3, 4, 5]
y = [1, 2, 3, 4, 5]
x, y = [Link](x, y) # We have to use this otherwise, it will throw error: 'list'
z = x+y

# Plot the data
ax.plot_wireframe(x, y, z)
ax.set_facecolor("#e09696")
78
In [88]: fig = [Link]()
ax = fig.add_subplot(111, projection='3d')

x = [Link](-5, 5, 100)
y = [Link](-5, 5, 100)
x, y = [Link](x, y)
z = [Link]([Link](x**2 + y**2))

ax.plot_wireframe(x, y, z, color='orange')
ax.set_facecolor("#bdb184")
79
In [89]: #3d plotting of Year and Buyers wrt Price
fig = [Link]()
ax_car = fig.add_subplot(111 , projection = '3d')

x = df_car[['Year']]
y = df_car[['Buyers']]
x, y = [Link](x, y)
z = df_car[['Price (Lakhs)']]

# Giving the label
ax_car.set_xlabel ('Year')
ax_car.set_ylabel ('Buyers')
ax_car.set_zlabel ('Price')

ax_car.plot_wireframe(x,y,z)
Out[89]: <mpl_toolkits.mplot3d.art3d.Line3DCollection at 0x7931c004b460>

Subplot

[Link](nrows, ncols, index)


80
In [90]: name = ('Subha', 'Subhajit Das', 'Subhajit')
marks = [96, 80, 60]

# Line Plot in subplot
[Link](2, 3, 1)
[Link](name, marks)

# Stem Plot in subplot
[Link](2, 3, 2)
[Link](name, marks)

# Scatter Plot in subplot
[Link](2, 3, 3)
[Link](name, marks)

# Bar Plot in subplot
[Link](2, 3, 4)
[Link](name, marks)

# Stack Plot in subplot
[Link](2, 3, 5)
[Link](name, marks)

# Step Plot in subplot
[Link](2, 3, 6)
[Link](name, marks)

[Link]()
81
In [91]: # Price of a car wrt [Link] buyers
x = df_car[['Buyers']]
y = df_car[['Price (Lakhs)']]

# Line Plot in subplot
[Link](2, 2, 1)
[Link](x, y)

# Stem Plot in subplot
[Link](2, 2, 2)
[Link](x, y)

# Scatter Plot in subplot
[Link](2, 2, 3)
[Link](x, y)

# Step Plot in subplot
[Link](2, 2, 4)
[Link](x, y)
/usr/local/lib/python3.10/dist-packages/numpy/core/[Link]: FutureWarning:
In a future version, [Link](axis=None) will return a scalar min over the enti
re DataFrame. To retain the old behavior, use '[Link](axis=0)' or just '[Link]
()'
return reduction(axis=axis, out=out, **passkwargs)
/usr/local/lib/python3.10/dist-packages/numpy/core/[Link]: FutureWarning:
In a future version, [Link](axis=None) will return a scalar max over the enti
re DataFrame. To retain the old behavior, use '[Link](axis=0)' or just '[Link]
()'
return reduction(axis=axis, out=out, **passkwargs)

Out[91]: [<[Link].Line2D at 0x7931bf09a290>]

Graph Saving

[Link](file_name)
82
In [92]: fig = [Link](figsize=(3,2), dpi=150)

# Can save 2d and 3d plot
name = ('Subha', 'Subhajit Das', 'Subhajit')
marks = [96, 80, 60]

[Link](name, marks)

[Link]("[Link]", # It will save in pdf format, by default it's png
dpi = 2000, # It will increase the resolution
facecolor = 'g', # It will set a padding in the chart
transparent = True,
bbox_inches = 'tight') # It will reduce the padding size

Axis in Matplotlib
83
In [93]: fig = [Link](figsize=(3,2), dpi=150)

# Adding labels in plot
name = (1, 2, 3)
marks = [96, 80, 60]

[Link](name, labels = ['Subha', 'Subhajit Das', 'Subhajit'])
[Link](marks)

[Link](name, marks)
Out[93]: <BarContainer object of 3 artists>
84
In [94]: fig = [Link](figsize=(3,2), dpi=150)

# Specify limit in plot
name = (1, 2, 3)
marks = [96, 80, 60]

[Link](name, labels = ['Subha', 'Subhajit Das', 'Subhajit'])
[Link](marks)

[Link](0,2)
[Link](45,100)

[Link](name, marks, color = 'orange')
Out[94]: <BarContainer object of 3 artists>

Text in Matplotlib
85
In [95]: fig = [Link](figsize=(3, 2), dpi=150)

name = ('Subha', 'Subhajit Das', 'Subhajit')
marks = [96, 80, 60]

[Link]("Student Marks", fontsize = 12)
[Link]("Names", fontsize = 12)
[Link]("Marks", fontsize = 12)

[Link](0,88,"Calculas", # row position, column position, text
fontsize = 8,
style = 'italic',
bbox = {'facecolor' : 'green'})

[Link]("Deep Learning", xy=(1,76), # text, arrow position
xytext = (0.5, 60), # text position
arrowprops = dict(facecolor = 'black', shrink = 0.1)) # arrow color, arr

[Link](name, marks, label = 'Marks')

[Link](loc = 1,
facecolor = 'orange',
edgecolor = 'red',
framealpha = 0.8,
shadow = True)
Out[95]: <[Link] at 0x7931be390b80>

Common questions

Powered by AI

The 'use_line_collection' parameter in plt.stem() when set to True enhances performance by rendering stem lines as a collection rather than individually, which is computationally more efficient. Matplotlib 3.6 deprecated 'use_line_collection' as a positional parameter, recommending it be passed as a keyword argument due to planned removals in future releases .

The 'corner_mask' parameter in plt.contour() influences whether lines drawn ignore masked regions (when disabled) or interpolate excluding masked corners (when enabled), affecting the shape smoothness around corners. Enabling it may result in smoother and more continuous lines around masked data, improving plot aesthetics and accuracy .

Aspect ratio controls how an image's width and height relate when displayed, which can affect the perception of the image's geometry. Interpolation affects image quality by determining how pixel values are calculated in scaled versions, with options such as 'nearest', 'bilinear', or 'bicubic' altering the smoothness or sharpness of rendered images .

Step plots are beneficial when visualizing changes at discrete intervals with constant periods, as they clearly depict steps or transitions between levels. Real-world examples include stock price movements during trading sessions, where price remains constant between trades, or machinery cycling between states such as on-off in manufacturing processes .

The plt.stackplot() function offers baseline methods: 'zero' provides a simple addition of data from a constant base; 'sym' creates a symmetric baseline around zero; 'wiggle' minimizes the squared slope changes, creating a visually flowing plot; and 'weighted_wiggle', similar to 'wiggle', accounts for each layer's size, enhancing visual balance in stacked plots by spreading out layers uniformly .

The plt.stem() function in Matplotlib allows control over various parameters: 'locs' and 'heads' determine the x/y positions of the stem heads; 'linefmt' and 'markerfmt' define the color and style of the vertical lines and markers, respectively; 'basefmt' specifies the baseline format; 'bottom' sets the y-position of the baseline; 'use_line_collection' is a boolean indicating whether to create stem lines as a LineCollection; and 'orientation' decides if the stems are vertical or horizontal .

Contour plots (plt.contour()) illustrate the boundary lines at distinct levels, useful for showing data gradients, while filled contour plots (plt.contourf()) fill spaces between boundary lines with colors, which can emphasize area and sections between levels. Use plt.contourf() for visual emphasis on areas rather than boundaries .

The 'where' parameter in plt.fill_between() is a boolean array that specifies where to fill between two curves. It's useful for highlighting specific x-ranges where certain conditions are met, such as when data values fall within a particular range, or for visual emphasis in regions of interest on the plot .

plt.pcolormesh() allows the user to specify the grid's corner coordinates, making it useful for non-rectangular grids and irregular spacing, while plt.imshow() is simpler but constrained to uniform grids. plt.pcolormesh() can also efficiently handle large datasets due to its attention to pixel alignment (“snap” parameter) and provides more flexibility with parameters like 'edgecolors' and 'shading' .

Colormaps translate numerical data values into perceptually distinct colors, providing intuitive visual gaps and increments. They are essential for understanding gradients or concentrations in a plot, making data correlations more apparent. In plt.contour() or plt.pcolormesh(), altering colormaps can significantly aid interpretation of data patterns, such as temperature distributions or elevation maps .

You might also like