Yash Python Practical 2 = 3D Graph - Jupyter Notebook
Yash Python Practical 2 = 3D Graph - Jupyter Notebook
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.
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.
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.
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.
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.
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.
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.
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.
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.
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()
In [ ]: 1