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

Maths Lab 1

The document provides a series of Python code snippets using Matplotlib and NumPy to create various 2D plots, including scatter plots, line plots, exponential curves, sine and cosine functions, and polar plots. It also demonstrates how to plot implicit equations and specific curves like cardioids and cycloids. Each section is labeled and includes comments explaining the purpose of the code.
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 views5 pages

Maths Lab 1

The document provides a series of Python code snippets using Matplotlib and NumPy to create various 2D plots, including scatter plots, line plots, exponential curves, sine and cosine functions, and polar plots. It also demonstrates how to plot implicit equations and specific curves like cardioids and cycloids. Each section is labeled and includes comments explaining the purpose of the code.
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/ 5

LAB1 2D-Plots of Cartesian and Polar Curves

1A # importing the required module


import matplotlib . pyplot as plt
x = [1 ,2 ,3 ,4 ,6 ,7 , 8] # x axis values
y = [2 ,7 ,9 ,1 ,5 , 10 , 3] # corresponding y axis values
plt . scatter (x , y ) # plotting the points
plt . xlabel ('x – axis ') # naming the x axis
plt . ylabel ('y - axis ') # naming the y axis
plt . title ('Scatter points ') # giving a title to my graph
plt . show () # function to show the plot

1B # importing the required module


import matplotlib . pyplot as plt
x = [1 ,2 ,3 ,4 ,6 ,7 , 8] # x axis values
y = [2 ,7 ,9 ,1 ,5 , 10 , 3] # corresponding y axis values
plt . plot (x , y , 'r+--') # plotting the points
plt . xlabel ('x - axis ') # naming the x axis
plt . ylabel ('y - axis ') # naming the y axis
plt . title ('My first graph !') # giving a title to my graph
plt . show () # function to show the plot

1C # importing the required modules


import numpy as np
import matplotlib . pyplot as plt
x = np . arange (-10 , 10 , 0.001 ) # x takes the values
between -10 and 10 with a step length of 0.001
y = np .exp( x ) # Exponential function
plt . plot (x , y ) # plotting the points
plt . title (" Exponential curve ") # giving a title to the graph
plt . grid () # displaying the grid
plt . show () # shows the plot
LAB1 2D-Plots of Cartesian and Polar Curves
1D import numpy as np
import matplotlib . pyplot as plt
x = np . arange (-10, 10 , 0.001 )
y1 = np .sin ( x )
y2=np .cos ( x )
plt . plot (x , y1 ,x , y2 ) # plotting sine and cosine function
together with same values of x
plt . title (" sine curve and cosine curve ")
plt . xlabel (" Values of x")
plt . ylabel (" Values of sin (x) and cos(x) ")
plt . grid ()
plt . show ()

1E # A simple graph
import matplotlib . pyplot as plt
import numpy as np
x = np . linspace (0 , 2 , 100 )
plt . plot (x , x , label ='linear ') # Plot of y=x a linear curve
plt . plot (x , x ** 2 , label ='quadratic ') # Plot of y=x^2 a
quadric curve
plt . plot (x , x ** 3 , label ='cubic ') # Plot of y=x^3 a cubic
curve
plt . xlabel ('x label ') # Add an x- label to the axes .
plt . ylabel ('y label ') # Add a y- label to the axes .
plt . title (" Simple Plot ") # Add a title to the axes .
plt . legend () # Add a legend
plt . show () # to show the complete graph
LAB1 2D-Plots of Cartesian and Polar Curves
1F from sympy import plot_implicit , symbols , Eq
x , y = symbols ('x y')
p1 = plot_implicit ( Eq ( x ** 2 + y ** 2 , 4 ) ,(x ,-4 , 4 ) ,(y ,-4 , 4
) , title = 'Circle : $x^2+y^2=4$ ') # r= 2
p3= plot_implicit ( Eq (( y ** 2 )*( 2-x ) , ( x ** 2 )*( 2+x ) ) , (x ,
-5 , 5 ) , (y , -5 , 5 ) , title = 'Strophoid : $y^2 (a-x)=x^2 (a+x), a>
0$ ') # a=2
p4= plot_implicit ( Eq (( y ** 2 )*( 3-x ) ,x ** 3 ) ,(x ,-2 , 5 ) ,(y ,-
5 , 5 ) ) # a=3
p5= plot_implicit ( Eq ( 4*( y ** 2 ) ,( x ** 2 )*( 4-x ** 2 ) ) ,(x ,-
5 , 5 ) ,(y ,-5 , 5 ) ) # a=2
p6= plot_implicit ( Eq ( x ** 3+y ** 3 , 3*2*x*y ) ,(x ,-5 , 5 ) ,(y
,-5 , 5 ) ) # a=2

1G import numpy as np
import matplotlib . pyplot as plt
plt.axes(projection = 'polar')
r=3
rads = np.arange( 0, (2*np.pi), 0.01 ) # plotting the circle
for i in rads :
plt.polar(i , r , 'g.')
plt.show ()

1H # Plot cardioid r=5(1+cos theta )


from pylab import *
theta = linspace (0 , 2*np .pi , 1000 )
r1=5+5*cos ( theta )
polar ( theta , r1 ,'r')
show ()
LAB1 2D-Plots of Cartesian and Polar Curves
1I # Plot Four Leaved Rose r=2 | cos2x |
from pylab import *
theta = linspace (0 , 2*pi , 1000 )
r=2*abs(cos( 2* theta ) )
polar ( theta ,r ,'r')
show ()
1J import numpy as np
import matplotlib . pyplot as plt
import math
plt.axes ( projection = 'polar')
a=3
rad = np . arange (0 , ( 2 * np .pi) , 0.01 ) # plotting the
cardioid
for i in rad :
r = a + ( a*np .cos( i ) )
plt . polar (i ,r ,'g.')
r1=a-( a*np .cos ( i ) )
plt . polar (i , r1 ,'r.') # display the polar plot
plt . show ()

1K import numpy as np
import matplotlib.pyplot as plt
def circle ( r ):
x = [] # create the list of x coordinates
y = [] # create the list of y coordinates
for theta in np . linspace (-2*np.pi, 2*np.pi, 100): # loop
over a list of theta , which ranges from -2 pi to 2 pi
x.append ( r*np .cos( theta ) ) #add the corresponding
expression of x to the x list
y.append ( r*np .sin( theta ) ) # same for y
plt.plot(x , y ) # plot using matplotlib . piplot
plt.show () # show the plot
circle( 5 ) # call the function
LAB1 2D-Plots of Cartesian and Polar Curves
1L import numpy as np
import matplotlib . pyplot as plt
def cycloid ( r ):
x = [] # create the list of x coordinates
y = [] # create the list of y coordinates
for theta in np.linspace(-2*np.pi, 2*np.pi, 100 ): # loop over
a list of theta , which ranges from -2 pi to 2 pi
x.append(r*(theta - np.sin(theta))) #add the
corresponding expression of x to the x list
y.append(r*(1 - np.cos(theta))) # same for y
plt.plot(x,y) # plot using matplotlib . piplot
plt.show() # show the plot
cycloid(2) # call the function

You might also like