0% found this document useful (0 votes)
14 views50 pages

UNIT-5 Detailed Notes

This document covers three main topics: Regular Expressions in Python, Linear Regression, and Matplotlib for data visualization. It explains how to use the 're' module for pattern matching and string manipulation, provides an overview of linear regression techniques including simple and multiple regression, and illustrates various plotting techniques using Matplotlib. The document includes code examples for each topic to demonstrate their practical applications.

Uploaded by

abithhussain033
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)
14 views50 pages

UNIT-5 Detailed Notes

This document covers three main topics: Regular Expressions in Python, Linear Regression, and Matplotlib for data visualization. It explains how to use the 're' module for pattern matching and string manipulation, provides an overview of linear regression techniques including simple and multiple regression, and illustrates various plotting techniques using Matplotlib. The document includes code examples for each topic to demonstrate their practical applications.

Uploaded by

abithhussain033
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
You are on page 1/ 50

UNIT-5

REGULAR EXPRESSION,MATPLOTLIB,LINEAR REGRESSION

REGULAR EXPRESSION:
Python RegEx

A RegEx,or Regular Expression, is a


sequence of characters that forms a search pattern.

RegEx can be used to check if a string contains the specified search pattern.

RegEx Module

Python has a built-in package called re, which can be used to work with Regular
Expressions.

Import the re module:

import re
RegEx in Python

When you have imported the re module, you can start using regular expressions:

Example:

import re

#Check if the string starts with "The" and ends with "Spain": txt = "The rain in

Spain"

x = re.search("^The.*Spain$", txt)

if x:
print("YES! We have a match!") else:

print("No match")

Output:

YES! We have a match!


Functions of REGEX:
Search()
Findall()
Sub()
Split()
.Span()
.String()
.Group()
Search()
Example program:
import re
txt = "The Beautiful Women" x = re.search("^The.*Women$", txt) if (x):
print("YES! We have a match!") else:
print("No match")
Output:
Yes! We have a match!
findall()
Example program:
import re
txt = "The Beautiful Women"
#Return a list containing every occurrence of “me": x = re.findall("me", txt)
print(x)
Output:
['me']
Sub()
Example program:
import re
txt = "The Beautiful Rose"
x = re.sub("\s", “5", txt)
print("x")
Output:
The5Beautiful5Rose
Split()
Example program:
import re
txt = "The Beautiful Rose"
x = re.split("\s",txt)
print(x)
Output:
['The', Beautiful','Rose']
META CHARACTERS:

Special sequences:
.SPAN()
Returns a tuple containing the start-, and end positions of the match.
.STRING()
It returns a string passed into the function.
.GROUP()
The part of the string is returned where the match is found. .SPAN():
Example program:
import re
txt = "The Beautiful Women"
x = re.search(r"\bW\w+", str)
print(x.span())
Output:
(14, 19)
.String():
Example program:
import re
txt = "The Beautiful Women"
x = re.search(r"\bW\w+",txt)
print(x.string())
Output:
Women
.group()
Example program:
import re
txt = "The Beautiful Women"
x =re.search(r"\bB\w“, “\bW\w”,txt)
print(x.group())
Output:
Beautiful Women
LINEAR REGRESSION:
❖ Linear regression is a statistical method used for modelling the relationship
between a dependent variable and one or more independent variables by fitting a
linear equation to the observed data. The simplest form of linear regression with
one independent variable is called simple linear regression, while multiple linear
regression involves two or more independent variables.
Types of Linear Regression:
Simple Linear Regression:

This is the simplest form of linear


regression, and it involves only one
independent variable and one
dependent variable. The equation for
simple linear regression is:

where:
Y is the dependent variable
X is the independent variable
β0 is the intercept
β1 is the slope
Multiple Linear Regression:
This involves more than one independent variable and one dependent variable. The
equation for multiple linear regression is:

where:
Y is the dependent variable
X1, X2, …, Xp are the independent variables
β0 is the intercept
β1, β2, …, βn are the slopes
Some other regression types are-
∙ Polynomial Regression – Polynomial regression goes beyond simple linear
regression by incorporating higher-order polynomial terms of the independent
variable(s) into the model. It is represented by the general equation:
∙ Ridge Regression – Ridge regression is a regularization technique used to prevent
overfitting in linear regression models, especially when dealing with multiple
independent variables. It introduces a penalty term to the least squares objective
function, biasing the model towards solutions with smaller coefficients. The
equation for ridge regression becomes:
∙ Lasso Regression – Lasso regression is another regularization technique that uses
an L1 penalty term to shrink the coefficients of less important independent
variables towards zero, effectively performing feature selection. The equation for
lasso regression becomes:
∙ Elastic Net Regression – Elastic net regression combines the penalties of ridge and
lasso regression, offering a balance between their strengths. It uses a mixed penalty
term of the form
The goal of the algorithm is to find the best Fit Line equation that can predict the
values based on the independent variables.

In regression set of records are present with X and Y values and these values are
used to learn a function so if you want to predict Y from an unknown X this learned
function can be used. In regression we have to find the value of Y, So, a function is
required that
predicts continuous Y in the case of regression given X as independent features.
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook
plt.show()

In [1]: In [6]:

# Import
import matplotlib.pyplot as plt

# PLOT() - SINGLE LINE

import numpy as np
import matplotlib.pyplot as plt
x = np.array([0,5])
y = np.array([10,40]) # Multiple line
plt.plot(x,y)
localhost:8888/notebooks/Matplotlib_Practise.ipynb 1/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook

In [7]: import numpy as np


import matplotlib.pyplot as plt

x = np.array([1, 2, 6, 8])
y = np.array([3, 8, 1, 10])

plt.plot(x, y)
plt.show()

# Line STYLE
localhost:8888/notebooks/Matplotlib_Practise.ipynb 2/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook

In [8]: import numpy as np


import matplotlib.pyplot as plt

x = np.array([1, 2, 6, 8])
y = np.array([3, 8, 1, 10])

plt.plot(x, y, ls='dotted')
plt.show()

# Line color
localhost:8888/notebooks/Matplotlib_Practise.ipynb 3/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook

In [9]: import numpy as np


import matplotlib.pyplot as plt

x = np.array([1, 2, 6, 8])
y = np.array([3, 8, 1, 10])

plt.plot(x, y, ls='dashed', color='m')


plt.show()

# Line width
localhost:8888/notebooks/Matplotlib_Practise.ipynb 4/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook

In [10]: import numpy as np


import matplotlib.pyplot as plt

x = np.array([1, 2, 6, 8])
y = np.array([3, 8, 1, 10])

plt.plot(x, y, ls='dashed', color='m', lw=2.0)


plt.show()

# PLOT without line


localhost:8888/notebooks/Matplotlib_Practise.ipynb 5/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook

In [12]: import numpy as np


import matplotlib.pyplot as plt

x = np.array([1, 2, 6, 8])
y = np.array([3, 8, 1, 10])

plt.plot(x, y, 'o')
plt.show()

# MATPLOTLIB MARKERS
localhost:8888/notebooks/Matplotlib_Practise.ipynb 6/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook

In [13]: import numpy as np


import matplotlib.pyplot as plt

x = np.array([1, 2, 6, 8])
y = np.array([3, 8, 1, 10])

plt.plot(x, y, marker='D')
plt.show()

# Format strings-(marker|line|color)
localhost:8888/notebooks/Matplotlib_Practise.ipynb 7/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook

In [14]: import numpy as np


import matplotlib.pyplot as plt

x = np.array([1, 2, 6, 8])
y = np.array([3, 8, 1, 10])

plt.plot(x, y, marker='o', linestyle='-', color='r')


plt.show()

# MARKER SIZE
localhost:8888/notebooks/Matplotlib_Practise.ipynb 8/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook

In [15]: import numpy as np


import matplotlib.pyplot as plt

x = np.array([1, 2, 6, 8])
y = np.array([3, 8, 1, 10])

plt.plot(x, y, marker='o', ms=10)


plt.show()

# MARKER COLOR:
localhost:8888/notebooks/Matplotlib_Practise.ipynb 9/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook

In [16]: import numpy as np


import matplotlib.pyplot as plt

x = np.array([1, 2, 6, 8])
y = np.array([3, 8, 1, 10])

plt.plot(x, y, marker='o', ms=20, mec='k', mfc='none')


plt.show()

# markerfacecolor or mfc
localhost:8888/notebooks/Matplotlib_Practise.ipynb 10/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook

In [17]: import numpy as np


import matplotlib.pyplot as plt

x = np.array([1, 2, 6, 8])
y = np.array([3, 8, 1, 10])

plt.plot(x, y, marker='o', ms=20, mec='c', mfc='r')


plt.show()

# Matplotlib labels
localhost:8888/notebooks/Matplotlib_Practise.ipynb 11/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook

In [18]: import numpy as np


import matplotlib.pyplot as plt

x = np.array([80, 85, 90, 95, 120, 125])


y = np.array([240, 250, 260, 310, 320, 330])

plt.plot(x, y)
plt.xlabel("Temperature")
plt.ylabel("Gradient")
plt.show()
# Title for a plot

localhost:8888/notebooks/Matplotlib_Practise.ipynb 12/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook

In [19]: import numpy as np


import matplotlib.pyplot as plt

x = np.array([80, 85, 90, 95])


y = np.array([240, 250, 260, 310])

plt.plot(x, y)
plt.xlabel("Temperature")
plt.ylabel("Gradient")
plt.title("Comparison") # Fixed indentation here
plt.show()
# Matplotlib grids

localhost:8888/notebooks/Matplotlib_Practise.ipynb 13/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook

In [20]: import numpy as np


import matplotlib.pyplot as plt

x = np.array([80, 85, 90, 95])


y = np.array([240, 250, 260, 310])

plt.plot(x, y)
plt.xlabel("Temperature")
plt.ylabel("Gradient")
plt.grid()
plt.show()
# Grid lines for x axis

localhost:8888/notebooks/Matplotlib_Practise.ipynb 14/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook

In [21]: import matplotlib.pyplot as plt


import numpy as np

x = np.array([0, 5])
y = np.array([10, 40])

plt.plot(x, y)
plt.grid(axis='x') # You may also use plt.grid(True, axis='x')
plt.show()
# Grid lines for y axis

localhost:8888/notebooks/Matplotlib_Practise.ipynb 15/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook

In [22]: import matplotlib.pyplot as plt


import numpy as np

x = np.array([0, 5])
y = np.array([10, 40])

plt.plot(x, y)
plt.grid(axis='y')
plt.show()
# Subplots

localhost:8888/notebooks/Matplotlib_Practise.ipynb 16/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook

In [23]: import matplotlib.pyplot as plt


import numpy as np

x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])

plt.subplot(1, 2, 1)
plt.plot(x, y)

x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])

plt.subplot(1, 2, 2)
plt.plot(x, y)

plt.show()

localhost:8888/notebooks/Matplotlib_Practise.ipynb 17/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook

In [24]: import matplotlib.pyplot as plt


import numpy as np

x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])

plt.subplot(2, 3, 1)
plt.plot(x, y)

x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])

plt.subplot(2, 3, 2)
plt.plot(x, y)

x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])

plt.subplot(2, 3, 3)
plt.plot(x, y)

x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])

plt.subplot(2, 3, 4)
plt.plot(x, y)

x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])

plt.subplot(2, 3, 5)
plt.plot(x, y)

x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])

plt.subplot(2, 3, 6)
plt.plot(x, y)

plt.show()

localhost:8888/notebooks/Matplotlib_Practise.ipynb 18/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook
# Title for subplot

localhost:8888/notebooks/Matplotlib_Practise.ipynb 19/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook
In [25]: import matplotlib.pyplot as plt
import numpy as np

x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])

plt.subplot(1, 2, 1)
plt.plot(x, y)
plt.title("SALES")

x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])

plt.subplot(1, 2, 2)
plt.plot(x, y)
plt.title("INCOME")

plt.show()

# Super title

localhost:8888/notebooks/Matplotlib_Practise.ipynb 20/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook
In [26]: import matplotlib.pyplot as plt
import numpy as np

x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])

plt.subplot(1, 2, 1)
plt.plot(x, y)
plt.title("SALES")

x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])

plt.subplot(1, 2, 2)
plt.plot(x, y)
plt.title("INCOME")

plt.suptitle("MY SHOP")

plt.show()

# Scatter plot

localhost:8888/notebooks/Matplotlib_Practise.ipynb 21/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook
In [27]: import matplotlib.pyplot as plt
import numpy as np

x = np.array([5, 7, 8, 7, 2, 17, 2, 9, 4, 11, 12, 9, 6])


y = np.array([99, 86, 87, 88, 111, 86, 103, 87, 94, 78, 77, 85, 86])

plt.scatter(x, y)
plt.show()

# Multiple plots

localhost:8888/notebooks/Matplotlib_Practise.ipynb 22/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook
In [28]: import matplotlib.pyplot as plt
import numpy as np

x1 = np.array([5, 7, 8, 7, 2, 17, 2, 9, 4, 11, 12, 9, 6])


y1 = np.array([99, 86, 87, 88, 111, 86, 103, 87, 94, 78, 77, 85, 86])

plt.scatter(x1, y1)

x2 = np.array([2, 2, 8, 1, 15, 8, 12, 9, 7, 3, 11, 4, 7, 14, 12]) y2 =


np.array([100, 105, 84, 105, 90, 99, 90, 95, 94, 100, 79, 112, 91, 80,

plt.scatter(x2, y2)

plt.show()

# color

localhost:8888/notebooks/Matplotlib_Practise.ipynb 23/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook
In [29]: import matplotlib.pyplot as plt
import numpy as np

x1 = np.array([5, 7, 8, 7, 2, 17, 2, 9, 4, 11, 12, 9, 6])


y1 = np.array([99, 86, 87, 88, 111, 86, 103, 87, 94, 78, 77, 85, 86])

plt.scatter(x1, y1, color='hotpink')

x2 = np.array([2, 2, 8, 1, 15, 8, 12, 9, 7, 3, 11, 4, 7, 14, 12]) y2 =


np.array([100, 105, 84, 105, 90, 99, 90, 95, 94, 100, 79, 112, 91, 80,

plt.scatter(x2, y2, color='#88c999')

plt.show()

# Color each dot

localhost:8888/notebooks/Matplotlib_Practise.ipynb 24/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook
In [30]: import matplotlib.pyplot as plt
import numpy as np

x = np.array([5, 7, 8, 7, 2, 17, 2, 9, 4, 11, 12, 9, 6])


y = np.array([99, 86, 87, 88, 111, 86, 103, 87, 94, 78, 77, 85, 86]) colors
= np.array(["red", "green", "blue", "yellow", "pink", "black", "oran

plt.scatter(x, y, c=colors)
plt.show()

# Color map

localhost:8888/notebooks/Matplotlib_Practise.ipynb 25/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook
In [31]: import matplotlib.pyplot as plt
import numpy as np

x = np.array([5, 7, 8, 7, 2, 17, 2, 9, 4, 11, 12, 9, 6])


y = np.array([99, 86, 87, 88, 111, 86, 103, 87, 94, 78, 77, 85, 86])
colors = np.array([0, 10, 20, 30, 40, 45, 50, 55, 60, 70, 80, 90, 100])

plt.scatter(x, y, c=colors, cmap='viridis')

plt.show()

# DOT SIZE

localhost:8888/notebooks/Matplotlib_Practise.ipynb 26/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook
In [32]: import matplotlib.pyplot as plt
import numpy as np

x = np.array([5, 7, 8, 7, 2, 17, 2, 9, 4, 11, 12, 9, 6])


y = np.array([99, 86, 87, 88, 111, 86, 103, 87, 94, 78, 77, 85, 86]) sizes
= np.array([20, 50, 100, 200, 500, 1000, 60, 90, 10, 300, 600, 800, 7

plt.scatter(x, y, s=sizes)
plt.show()

# ALPHA

localhost:8888/notebooks/Matplotlib_Practise.ipynb 27/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook
In [33]: import matplotlib.pyplot as plt
import numpy as np

x = np.array([5, 7, 8, 7, 2, 17, 2, 9, 4, 11, 12, 9, 6])


y = np.array([99, 86, 87, 88, 111, 86, 103, 87, 94, 78, 77, 85, 86]) sizes
= np.array([20, 50, 100, 200, 500, 1000, 60, 90, 10, 300, 600, 800, 7

plt.scatter(x, y, s=sizes, alpha=0.5)


plt.show()

# COMBINATION OF COLOR SIZE & ALPHA

localhost:8888/notebooks/Matplotlib_Practise.ipynb 28/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook
In [34]: import matplotlib.pyplot as plt
import numpy as np

x = np.random.randint(100, size=(100))
y = np.random.randint(100, size=(100))
colors = np.random.randint(100, size=(100))
sizes = 10 * np.random.randint(100, size=(100))

plt.scatter(x, y, c=colors, s=sizes, alpha=0.5, cmap='nipy_spectral')


plt.colorbar()
plt.show()

# Matplotlib Bars:

localhost:8888/notebooks/Matplotlib_Practise.ipynb 29/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook
In [35]: import matplotlib.pyplot as plt
import numpy as np

x = np.array(["A", "B", "C", "D"])


y = np.array([3, 8, 1, 10])

plt.bar(x, y)
plt.show()

# Horizontal Bars

localhost:8888/notebooks/Matplotlib_Practise.ipynb 30/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook
In [36]: import matplotlib.pyplot as plt
import numpy as np

x = np.array(["A", "B", "C", "D"])


y = np.array([3, 8, 1, 10])

plt.barh(x, y)
plt.show()

# Bar Color

localhost:8888/notebooks/Matplotlib_Practise.ipynb 31/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook
In [37]: import matplotlib.pyplot as plt
import numpy as np

x = np.array(["A", "B", "C", "D"])


y = np.array([3, 8, 1, 10])

plt.bar(x, y, color="red")
plt.show()

# Color Names

localhost:8888/notebooks/Matplotlib_Practise.ipynb 32/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook
In [38]: import matplotlib.pyplot as plt
import numpy as np

x = np.array(["A", "B", "C", "D"])


y = np.array([3, 8, 1, 10])

plt.bar(x, y, color="hotpink")
plt.show()

# Bar Width

localhost:8888/notebooks/Matplotlib_Practise.ipynb 33/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook
In [39]: import matplotlib.pyplot as plt
import numpy as np

x = np.array(["A", "B", "C", "D"])


y = np.array([3, 8, 1, 10])

plt.bar(x, y, width=0.5) # Adjust the width as needed


plt.show()

# Bar Height

localhost:8888/notebooks/Matplotlib_Practise.ipynb 34/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook
In [40]: import matplotlib.pyplot as plt
import numpy as np

x = np.array(["A", "B", "C", "D"])


y = np.array([3, 8, 1, 10])

plt.barh(x, y, height=0.5) # Adjust the height as needed


plt.show()

# Histogram

localhost:8888/notebooks/Matplotlib_Practise.ipynb 35/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook
In [41]: import matplotlib.pyplot as plt
import numpy as np

x = np.random.normal(170, 10, 250)


plt.hist(x)
plt.show()

# Matplotlib Pie Charts

localhost:8888/notebooks/Matplotlib_Practise.ipynb 36/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook
In [42]: import matplotlib.pyplot as plt
import numpy as np

y = np.array([35, 25, 25, 15])


plt.pie(y)
plt.show()

# Labels:

localhost:8888/notebooks/Matplotlib_Practise.ipynb 37/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook
In [43]: import matplotlib.pyplot as plt
import numpy as np

y = np.array([35, 25, 25, 15])


mylabels = ["Apples", "Bananas", "Cherries", "Dates"]

plt.pie(y, labels=mylabels)
plt.show()

# Start Angle

localhost:8888/notebooks/Matplotlib_Practise.ipynb 38/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook
In [44]: import matplotlib.pyplot as plt
import numpy as np

y = np.array([35, 25, 25, 15])


mylabels = ["Apples", "Bananas", "Cherries", "Dates"]

plt.pie(y, labels=mylabels, startangle=90)


plt.show()

# Explode

localhost:8888/notebooks/Matplotlib_Practise.ipynb 39/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook
In [45]: import matplotlib.pyplot as plt
import numpy as np

y = np.array([35, 25, 25, 15])


mylabels = ["Apples", "Bananas", "Cherries", "Dates"]
myexplode = [0.2, 0, 0, 0]

plt.pie(y, labels=mylabels, explode=myexplode)


plt.show()

# Colors

localhost:8888/notebooks/Matplotlib_Practise.ipynb 40/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook
In [46]: import matplotlib.pyplot as plt
import numpy as np

y = np.array([35, 25, 25, 15])


mylabels = ["Apples", "Bananas", "Cherries", "Dates"]
mycolors = ["black", "hotpink", "b", "#4CAF50"]

plt.pie(y, labels=mylabels, colors=mycolors)


plt.show()

# Legend

localhost:8888/notebooks/Matplotlib_Practise.ipynb 41/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook
In [47]: import matplotlib.pyplot as plt
import numpy as np

y = np.array([35, 25, 25, 15])


mylabels = ["Apples", "Bananas", "Cherries", "Dates"]

plt.pie(y, labels=mylabels)
plt.legend()
plt.show()

# Legend With Header:

localhost:8888/notebooks/Matplotlib_Practise.ipynb 42/43
12/7/23, 1:14 PM Matplotlib_Practise - Jupyter Notebook
import matplotlib.pyplot as plt
In [48]: In [ ]: import numpy as np

y = np.array([35, 25, 25, 15])


mylabels = ["Apples", "Bananas",
"Cherries", "Dates"]

plt.pie(y, labels=mylabels)
plt.legend(title="Four Fruits:")
plt.show()

localhost:8888/notebooks/Matplotlib_Practise.ipynb 43/43

You might also like