0% found this document useful (0 votes)
6 views11 pages

Practical 1

This document outlines practical exercises for plotting various mathematical functions using Matplotlib in Python, including logarithmic, trigonometric, inverse, algebraic, and exponential functions. It also covers different graph formats such as line graphs, bar graphs, histograms, pie charts, and scatter plots, providing example code for each type. Additionally, it addresses common runtime warnings and errors encountered during plotting.

Uploaded by

adwaitdorke
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)
6 views11 pages

Practical 1

This document outlines practical exercises for plotting various mathematical functions using Matplotlib in Python, including logarithmic, trigonometric, inverse, algebraic, and exponential functions. It also covers different graph formats such as line graphs, bar graphs, histograms, pie charts, and scatter plots, providing example code for each type. Additionally, it addresses common runtime warnings and errors encountered during plotting.

Uploaded by

adwaitdorke
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/ 11

3/3/25, 11:19 AM Practical 1 - Unit 1 - (1.1 - 1.3) (v1.

1)

PRACTICAL 1 Unit 1 - (1.1 - 1.3)

1.2 Graph Plotting using Matplotlib

1.2.1: Logarithmic Functions


Ex. To Plot the functions f(x) = log_10 (x) on the interval [0,10] . Refer to the code
below

In [1]: import numpy as np


import matplotlib.pyplot as plt

#plot the function f(x) = log_10(x) on the interval of (0, 10)

x = np.linspace(0,10)
y = np.log10(x)
plt.plot(x,y)
plt.show()

C:\Users\MMC\AppData\Local\Temp\ipykernel_3992\1439288947.py:8: RuntimeWarning: di
vide by zero encountered in log10
y = np.log10(x)

1.2.2: Trignometric Functions

file:///C:/Users/MMC/Downloads/SY SEM 4 Maths Practical 1 (.html pub version).html 1/11


3/3/25, 11:19 AM Practical 1 - Unit 1 - (1.1 - 1.3) (v1.1)

Ex. To Plot the functions f(x) = log_10 (x) on the interval [0,2π] . Refer to the code
below

In [12]: import numpy as np


import matplotlib.pyplot as plt

#plot the function f(x) = cos(x) on the interval of (0, 10)

x = np.arange(0, 2*(np.pi), 0.1)


y = np.cos(x)
plt.plot(x,y)
plt.show()

1.2.3: Inverse Functions


Ex. To Plot the functions f(x) = sin**(-1) (x) on the interval [-1,1] . Refer to the
code below:

In [5]: import numpy as np


import matplotlib.pyplot as plt

x = np.arange(-1, 1, 0.01)
y = np.arcsin(x)

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

file:///C:/Users/MMC/Downloads/SY SEM 4 Maths Practical 1 (.html pub version).html 2/11


3/3/25, 11:19 AM Practical 1 - Unit 1 - (1.1 - 1.3) (v1.1)

1.2.4: Algebraic Functions:


Ex. To Plot the functions f(x) = x**2 on the interval [-2,2] . Refer to the code below

In [2]: import numpy as np


import matplotlib.pyplot as plt

x = np.linspace(-2, 2, 100)
y = x**2

fig = plt.figure(figsize = (10,5))


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

file:///C:/Users/MMC/Downloads/SY SEM 4 Maths Practical 1 (.html pub version).html 3/11


3/3/25, 11:19 AM Practical 1 - Unit 1 - (1.1 - 1.3) (v1.1)

1.2.5: Exponential Functions


Ex. To Plot the functions f(x) = e**x on the interval [-1,100] . Refer to the code below

In [8]: import numpy as np


import matplotlib.pyplot as plt

x = np.linspace(-1, 2, 100)
y = np.exp(x)

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

1.2.5 General Combination of LIATE


Function
Ex. To Plot the functions f(x) = sin(x) - e**2 + 3*x**2 - log10(x) on the interval
[0,2π] . Refer to the code below

In [11]: import numpy as np


import matplotlib.pyplot as plt

x = np.arange(0, 2*(np.pi), 0.1)


y = np.sin(x)-np.exp(x)+3*x**2+np.log10(x)

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

file:///C:/Users/MMC/Downloads/SY SEM 4 Maths Practical 1 (.html pub version).html 4/11


3/3/25, 11:19 AM Practical 1 - Unit 1 - (1.1 - 1.3) (v1.1)

C:\Users\MMC\AppData\Local\Temp\ipykernel_5700\2469820470.py:5: RuntimeWarning: di
vide by zero encountered in log10
y = np.sin(x)-np.exp(x)+3*x**2+np.log10(x)

1.2.7: Visualizing a Three Dimensionl


Function in Two dimensions
Ex. Let us first define a function f(x,y) = sin**10(x) + cos(5 + xy)sin(x) . To Plot
the contour responding to the above function, refer to the code below:

In [13]: import numpy as np


import matplotlib.pyplot as plt

plt.style.use('seaborn-white')
def f(x, y):
return np.sin(x) **10 + np.cos(5 + x*y) * np.sin(x)
x = np.linspace(0, 5, 50)
y = np.linspace(0, 5, 50)

X, Y = np.meshgrid(x,y)
Z = f(x,Y)

plt.contourf(X, Y, Z, 10, cmap = 'RdBu')


plt.colourbar()

C:\Users\MMC\AppData\Local\Temp\ipykernel_5700\2431292763.py:9: MatplotlibDeprecat
ionWarning: The seaborn styles shipped by Matplotlib are deprecated since 3.6, as
they no longer correspond to the styles shipped by seaborn. However, they will rem
ain available as 'seaborn-v0_8-<style>'. Alternatively, directly use the seaborn A
PI instead.
plt.style.use('seaborn-white')

file:///C:/Users/MMC/Downloads/SY SEM 4 Maths Practical 1 (.html pub version).html 5/11


3/3/25, 11:19 AM Practical 1 - Unit 1 - (1.1 - 1.3) (v1.1)

---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In[13], line 19
16 Z = f(x,Y)
18 plt.contourf(X, Y, Z, 10, cmap = 'RdBu')
---> 19 plt.colourbar()

AttributeError: module 'matplotlib.pyplot' has no attribute 'colourbar'

1.3 Different formats for Graphs in Python

1.3.1 Line Graphs


In [15]: import numpy as np
import matplotlib.pyplot as plt
x1 = [1, 2, 3]
y1 = [3, 6, 2]
plt.plot(x1, y1, label = "line 1")

x2 = [1, 2, 3]
y2 = [7, 2, 5]
plt.plot(x2, y2, label = "line 2")

plt.xlabel('x-axis')
plt.ylabel('y-axis')

plt.title('Two lines on same graph')


plt.legend()
plt.show()

file:///C:/Users/MMC/Downloads/SY SEM 4 Maths Practical 1 (.html pub version).html 6/11


3/3/25, 11:19 AM Practical 1 - Unit 1 - (1.1 - 1.3) (v1.1)

1.3.2: Bar Graphs


In [3]: #import matplotlib.pyplot as plt

left = [1, 2, 3, 4, 5]
height = [5, 24, 45, 32, 15]

tick_label = ['Pune', 'Mumbai', 'Nagpur', 'Nashik', 'Satara']

plt.bar(left, height, tick_label=tick_label, width=0.8, color=['red', 'green', 'blu


plt.xlabel('Cities')
plt.ylabel('No of covid Patients (in thousands)')

plt.title('Covid-19 Data')
plt.show()

file:///C:/Users/MMC/Downloads/SY SEM 4 Maths Practical 1 (.html pub version).html 7/11


3/3/25, 11:19 AM Practical 1 - Unit 1 - (1.1 - 1.3) (v1.1)

1.3.3: Histogram
In [4]: import numpy as np
import matplotlib.pyplot as plt

ages = [2, 3, 70, 40, 30, 45, 50, 45, 43, 40, 44, 60, 7, 13, 57, 57, 18, 90, 77, 32
range = (0, 100)
bins = 5

plt.hist(ages, bins=bins, range=range, color='blue', histtype='bar', width=15)

plt.xlabel('Age')
plt.ylabel('No. Of People')
plt.title('Histogram Plot')
plt.show()

file:///C:/Users/MMC/Downloads/SY SEM 4 Maths Practical 1 (.html pub version).html 8/11


3/3/25, 11:19 AM Practical 1 - Unit 1 - (1.1 - 1.3) (v1.1)

1.3.4: Pie Chart


In [18]: import numpy as np
import matplotlib.pyplot as plt

left = [1,2,3,4,5]
height = [5,24,45,32,15]

tick_label = ['Pune', 'Mumbai', 'Nagpur', 'Nasik', 'Satara']


fig = plt.figure(figsize = (10, 7))

plt.pie(height, labels = tick_label)

plt.show()

file:///C:/Users/MMC/Downloads/SY SEM 4 Maths Practical 1 (.html pub version).html 9/11


3/3/25, 11:19 AM Practical 1 - Unit 1 - (1.1 - 1.3) (v1.1)

1.3.5: Scatter Plots


In [19]: import numpy as np
import matplotlib.pyplot as plt

girls_score = [81, 90, 70, 89, 100, 80, 90, 100, 80, 34]
boys_score = [30, 29, 49, 48, 100, 48, 34, 45, 20, 30]
grades_range = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]

fig = plt.figure()
ax=fig.add_axes([0,0,1,1])

ax.scatter(grades_range, girls_score, color = 'r') ##Select variables and it' s


ax.scatter(grades_range, boys_score, color = 'b')

ax.set_xlabel('Grades Range') ##Set X label


ax.set_ylabel('Grades Scored') ##Set Y label
ax.set_title('Scatter Plot') ##Set Title

plt.show()

file:///C:/Users/MMC/Downloads/SY SEM 4 Maths Practical 1 (.html pub version).html 10/11


3/3/25, 11:19 AM Practical 1 - Unit 1 - (1.1 - 1.3) (v1.1)

file:///C:/Users/MMC/Downloads/SY SEM 4 Maths Practical 1 (.html pub version).html 11/11

You might also like