In [1]: 1 # Name = Yash Pardeshi
2 # class = sybsc (cs)
3 # Roll no = 75
4 # practical 2 = 3D graph
In [2]: 1 # Q.1) plot graph of f(x) = sin(sqrt(x^2+y^2)) in -6<x,y<6.
In [53]: 1 from mpl_toolkits import mplot3d
2 import numpy as np
3
4 from pylab import*
5 def f(x,y):
6 return np.sin(np.sqrt(x**2+y**2))
7 x=np.linspace(-6,6,30)
8 y=np.linspace(-6,6,30)
9 x,y=np.meshgrid(x,y)
10 z=f(x,y)
11 ax=axes(projection='3d')
12 ax.contour3D(x,y,z,50)
13 xlabel('x')
14 ylabel('y')
15 title('3D contour')
16 legend()
17 show()
No artists with labels found to put in legend. Note that artists whose label start with an underscore are ignored when le
gend() is called with no argument.
In [1]: 1 # Q.2) plot parabola of z = (x^2+y^2) in -6<x,y<6.
In [2]: 1 from mpl_toolkits import mplot3d
2 import numpy as np
3
4 from pylab import*
5 def f(x,y):
6 return x**2+y**2
7 x=np.linspace(-6,6,30)
8 y=np.linspace(-6,6,30)
9 x,y=np.meshgrid(x,y)
10 z=f(x,y)
11 ax=axes(projection='3d')
12 ax.contour3D(x,y,z,50)
13 xlabel('x')
14 ylabel('y')
15 title('3D parabola')
16 legend()
17 show()
No artists with labels found to put in legend. Note that artists whose label start with an underscore are ignored when le
gend() is called with no argument.
In [3]: 1 # Q.3) plot graph of f(x) = e^(-x^2-y^2) in -1<x,y<1.
In [11]: 1 from pylab import*
2 def f(x,y):
3 return np.exp(-x**2-y**2)
4 x=np.linspace(-1,1,30)
5 y=np.linspace(-1,1,30)
6 x,y=np.meshgrid(x,y)
7 z=f(x,y)
8 ax=axes(projection='3d')
9 ax.contour3D(x,y,z,50)
10 xlabel('x')
11 ylabel('y')
12 title('$z=e^(-x^2-y^2)$')
13 legend()
14 show()
No artists with labels found to put in legend. Note that artists whose label start with an underscore are ignored when le
gend() is called with no argument.
In [5]: 1 # Q.4) plot the graph of f(x)= (sin(4x)-cos(5y))/5 in -1<x,y<1.
In [16]: 1 from pylab import*
2 def f(x,y):
3 return (np.sin(4*x)-np.cos(5*y))/5
4 x=np.linspace(-1,1,30)
5 y=np.linspace(-1,1,30)
6 x,y=np.meshgrid(x,y)
7 z=f(x,y)
8 ax=axes(projection='3d')
9 ax.contour3D(x,y,z,50)
10 xlabel('x')
11 ylabel('y')
12 title('$f(x,y)= (sin(4x)-cos(5y))/5$')
13 legend()
14 show()
No artists with labels found to put in legend. Note that artists whose label start with an underscore are ignored when le
gend() is called with no argument.
In [7]: 1 # Q.5) plot parabola of f(x)=x^2+y^2 in -6<x,y<6.
In [17]: 1 from pylab import*
2 def f(x,y):
3 return x**2+y**2
4 x=np.linspace(-6,6,30)
5 y=np.linspace(-6,6,30)
6 x,y=np.meshgrid(x,y)
7 z=f(x,y)
8 ax=axes(projection='3d')
9 ax.plot_surface(x,y,z)
10 xlabel('x')
11 ylabel('y')
12 title('$ f(x,y)=x^2+y^2$')
13 legend()
14 show()
No artists with labels found to put in legend. Note that artists whose label start with an underscore are ignored when le
gend() is called with no argument.
In [9]: 1 # Q.6) plot parabola of f(x)=xe^(-x^2-y^2) in -6<x,y<6.
In [27]: 1 from pylab import*
2 from math import*
3 def f(x,y):
4 return x*np.exp(-x**2-y**2)
5 x=np.linspace(-6,6,30)
6 y=np.linspace(-6,6,30)
7 x,y=np.meshgrid(x,y)
8 z=f(x,y)
9 ax=axes(projection='3d')
10 ax.plot_surface(x,y,z)
11 xlabel('x')
12 ylabel('y')
13 title('$z=xe(-x^2-y^2)$')
14 legend()
15 show()
No artists with labels found to put in legend. Note that artists whose label start with an underscore are ignored when le
gend() is called with no argument.
In [18]: 1 # Q.7) plot function of z = cos(|x|+|y|) in -1<x,y<1.
In [19]: 1 from pylab import*
2 from math import*
3 def f(x,y):
4 return np.cos(abs(x)+abs(y))
5 x=np.linspace(-1,1,30)
6 y=np.linspace(-1,1,30)
7 x,y=np.meshgrid(x,y)
8 z=f(x,y)
9 ax=axes(projection='3d')
10 ax.plot_surface(x,y,z)
11 xlabel('x')
12 ylabel('y')
13 title('$z=cos(|x|+|y|)$')
14 legend()
15 show()
No artists with labels found to put in legend. Note that artists whose label start with an underscore are ignored when le
gend() is called with no argument.
In [20]: 1 # Q.8) plot the function of z = cos(x^2+y^2-0.5)-0.5 in -1<x,y<1.
In [23]: 1 from pylab import*
2 from math import*
3 def f(x,y):
4 return np.cos(x**2+y**2-0.5)-0.5
5 x=np.linspace(-1,1,30)
6 y=np.linspace(-1,1,30)
7 x,y=np.meshgrid(x,y)
8 z=f(x,y)
9 ax=axes(projection='3d')
10 ax.plot_surface(x,y,z)
11 xlabel('x')
12 ylabel('y')
13 title('$z=cos(x^2+y^2-0.5)-0.5$')
14 legend()
15 show()
No artists with labels found to put in legend. Note that artists whose label start with an underscore are ignored when le
gend() is called with no argument.
In [24]: 1 # Q.9) # Q.6) plot parabola of f(x)=xe^(x^2+y^2) in -6<x,y<6.
In [26]: 1 from pylab import*
2 from math import*
3 def f(x,y):
4 return x*np.exp(x**2+y**2)
5 x=np.linspace(-6,6,30)
6 y=np.linspace(-6,6,30)
7 x,y=np.meshgrid(x,y)
8 z=f(x,y)
9 ax=axes(projection='3d')
10 ax.plot_wireframe(x,y,z)
11 xlabel('x')
12 ylabel('y')
13 title('$z=xe(x^2+y^2)$')
14 legend()
15 show()
No artists with labels found to put in legend. Note that artists whose label start with an underscore are ignored when le
gend() is called with no argument.
In [28]: 1 # Q.10) plot function z = sin(x) + cos(y) in -5<x,y<5.
In [29]: 1 from pylab import*
2 from math import*
3 def f(x,y):
4 return x*np.sin(x)+np.cos(y)
5 x=np.linspace(-5,5,30)
6 y=np.linspace(-5,5,30)
7 x,y=np.meshgrid(x,y)
8 z=f(x,y)
9 ax=axes(projection='3d')
10 ax.plot_wireframe(x,y,z,rstride=2,cstride=2)
11 xlabel('x')
12 ylabel('y')
13 title('$z=sin(x)+cos(y)$')
14 legend()
15 show()
No artists with labels found to put in legend. Note that artists whose label start with an underscore are ignored when le
gend() is called with no argument.
In [30]: 1 # Q.11) plot 3D graph f(x) = e^(-x^2) in [-5,5] with gren dashed points line with upward pointing triangle.
In [35]: 1 import numpy as np
2 import matplotlib.pyplot as plt
3 from mpl_toolkits.mplot3d import Axes3D
4
5 #generate x values and corresponding f(x) values
6 x =np.linspace(-5,5,100)
7 y =np.zeros_like(x) #y=0 for 2d graph
8 z =np.exp(-x**2)
9
10 #create the plot fig
11 fig =plt.figure()
12 ax =fig.add_subplot(111,projection='3d')
13
14 #plot the data with a green dashed line and upword pointing tringle marker
15 ax.plot(x,y,z,linestyle='--',color='g',marker='^',markersize=8)
16
17 #labelling axes
18 ax.set_xlabel('x')
19 ax.set_ylabel('y')
20 ax.set_zlabel('f(x)')
21
22 #title of the graph
23 ax.set_title('z=e^(-x^2)')
24 plt.show()
In [33]: 1 # Q.12) PLOT 3D graph whose parametric equation (cos(2x),sin(2x),x) for 10<=x<=20 in red colour with title to the graph
In [36]: 1 import numpy as np
2 import matplotlib.pyplot as plt
3 from mpl_toolkits.mplot3d import Axes3D
4
5 #generate x values and corresponding f(x) values
6 x =np.linspace(10,20,50)
7
8 y =np.sin(2 * x)
9 z =x
10 x_vals = np.cos(2 * x)
11
12 #create the plot fig
13 fig =plt.figure()
14 ax =fig.add_subplot(111,projection='3d')
15
16 #plot the data with a red color line
17 ax.plot(x_vals, y, z, color='r')
18
19 #labelling axes
20 ax.set_xlabel('x')
21 ax.set_ylabel('y')
22 ax.set_zlabel('f(x)')
23
24
25 #title of the graph
26 ax.set_title('3D parametric curve: (cos(2x), sin(2x),x')
27 plt.show()
In [39]: 1 # Q.13) write python program in 3D to rotate the point(1,0,0) through xy plane in clockwise direction(rotation through
2 # by angle of 90 deg.
In [48]: 1 import numpy as np
2 import matplotlib.pyplot as plt
3 from mpl_toolkits.mplot3d import Axes3D
4
5 # original point
6 point = np.array([1,0,0])
7
8 # Rotation matrix for 90 deg(clockwise rotation around z-axis)
9 theta = np.radians(90) # convert deg to rad.
10 rotation_matrix = np.array([[np.cos(theta), -np.sin(theta),0], [np.sin(theta), np.cos(theta),0],[0,0,1]])
11
12 # Apply rotation
13 rotated_point = rotation_matrix.dot(point)
14
15 #create the plot fig
16 fig =plt.figure()
17 ax =fig.add_subplot(111,projection='3d')
18
19 #plot the original and rotated points
20 ax.scatter([point[0],rotated_point[0]],[point[1],rotated_point[1]],[point[2],rotated_point[2]], color = 'r')
21
22
23 #plot the lines connecting the points(original and rotated)
24 ax.plot([point[0],rotated_point[0]],[point[1],rotated_point[1]],[point[2],rotated_point[2]], color = 'b')
25
26
27
28 #set labels for axes
29 ax.set_xlabel('x')
30 ax.set_ylabel('y')
31 ax.set_zlabel('f(x)')
32
33 # title for graph
34 ax.set_title('rotation of point (1,0,0) by 90 degree around the z-axis')
35
36 #labelling axes
37 ax.set_xlim([-1.5, 1.5])
38 ax.set_ylim([-1.5, 1.5])
39 ax.set_zlim([-1.5, 1.5])
40
41 plt.show()
In [49]: 1 # Q.14) to plot 3D axes with labels x-axis,y-axis and z-axis and also plot the following points with given coordinates
2 #same graph (70.-25,15) as a diamond in black colour.
In [51]: 1 import numpy as np
2 import matplotlib.pyplot as plt
3 from mpl_toolkits.mplot3d import Axes3D
4
5 # coordinates of point
6 point = np.array([70,-25,15])
7
8 #create the plot fig
9 fig =plt.figure()
10 ax =fig.add_subplot(111,projection='3d')
11
12 #plot the point as a diamond in black colour.
13 ax.scatter(point[0], point[1], point[2] ,color = 'Black', marker='D', s=100)
14
15
16
17
18
19 #set labels for axes
20 ax.set_xlabel('x')
21 ax.set_ylabel('y')
22 ax.set_zlabel('f(x)')
23
24 # title for graph
25 ax.set_title('3D plot axes and a point')
26
27 #labelling axes
28 ax.set_xlim([0,100])
29 ax.set_ylim([-30, 30])
30 ax.set_zlim([0, 30])
31
32 plt.show()
In [54]: 1 # Q.15) To plot 3D contours for the function f(x,y) = log(x^2*y^2) when -5<=x,y<=5 with green colour map.
In [57]: 1 import numpy as np
2 import matplotlib.pyplot as plt
3 from mpl_toolkits.mplot3d import Axes3D
4
5 # Define function
6 def f(x,y):
7 return np.log(x**2 * y**2)
8
9
10 #generate x and y values in range [-5,5].
11 x =np.linspace(-5,5,100)
12 y =np.linspace(-5,5,100)
13
14 #create meshgrid for 3d grid.
15 X,Y = np.meshgrid(x,y)
16
17 # compute the function values on the grid,handling the log of zero
18 Z = f(X,Y)
19
20 # Mask out values where f(X,Y) is undefined (log of zero or negative no.)
21 Z = np.ma.masked_where(np.isnan(Z),Z)
22
23
24 #create the figure and axis for the 3D plot
25 fig =plt.figure()
26 ax =fig.add_subplot(111,projection='3d')
27
28 # plot the 3D contour
29 contour = ax.contour3D(X,Y,Z, 50,cmao = 'Greens')
30
31 #labelling axes
32 ax.set_xlabel('x-axis')
33 ax.set_ylabel('y-axis')
34 ax.set_zlabel('f(x,y)')
35
36 #title of the graph
37 ax.set_title('3D contour of f(x,y)= log(x^2 * y^2)')
38 plt.show()
C:\Users\Adminisrator\AppData\Local\Temp\ipykernel_6764\3418528442.py:29: UserWarning: The following kwargs were not used
by contour: 'cmao'
contour = ax.contour3D(X,Y,Z, 50,cmao = 'Greens')
In [ ]: 1