Displaying 3D images in Python
In this article, we will discuss how to display 3D images using different methods, (i.e 3d projection, view_init() method, and using a loop) in Python.
- Matplotlib: It is a plotting library for Python programming it serves as a visualization utility library, Matplotlib is built on NumPy arrays, and designed to work with the broader SciPy stack.
- Numpy: It is a general-purpose array-processing package. It provides a high-performance multidimensional array and matrices along with a large collection of high-level mathematical functions.
- mpl_toolkits: It provides some basic 3d plotting (scatter, surf, line, mesh) tools.
In this example, we created a 3d image of a scatter sin wave. Here we have created an array of points using ‘np.arrange’ and ‘np.sin’.NumPy.sin: This mathematical function helps the user to calculate trigonometric sine for all x(being the array elements), and another function is the scatter() method which is the matplotlib library used to draw a scatter plot.
Syntax: np.arrange(start, stop, step) : It returns an array with evenly spaced elements as per the interval.
- start: start of interval range.
- stop: end of interval range.
- step: step size of the interval.
In this example, we are selecting the 3D axis of the dimension X =5, Y=5, Z=5, and in np.ones() we are passing the dimensions of the cube. The np.ones () function returns a new array of given shape and type, with ones.
Syntax: numpy.ones (shape, dtype = None)
After the above step, we are selecting color opacity as alpha = 0.9 ( vary from 0.0 – 1.0 ). In the next step, we are passing the dimension of axes( i.e 5, 5, 5) + number of faces for the cube ( i.e 0-4 ) in np.empty() function after that we are passing color combination and opacity for each face of the cube and in last Voxels is used to customizations of the sizes, positions, and colors. The np.empty () function return a new array of given shape and type, without initializing entries.
In this example, we use numpy.linspace() that creates an array of 10 linearly placed elements between -1 and 5, both inclusive after that the mesh grid function returns two 2-dimensional arrays, After that in order to visualize an image of 3D wireframe we require passing coordinates of X, Y, Z, color(optional).
In this example, we plot a spiral graph, and we will see its 360-degree view using a loop. Here, view_init(elev=, azim=)This can be used to rotate the axes programmatically.‘elev’ stores the elevation angle in the z plane. ‘azim’ stores the azimuth angle in the x,y plane.D constructor. The draw() function in pyplot module of the matplotlib library is used to redraw the current figure with a pause of 0.001-time interval.