To implement 3D plotting in python, mplot3d toolkit from mpl_toolkits is needed to be implemented first, which creates a 3D axis upon passing the keyword projection='3d' to any normal axis creation routine of matplotlib.
The previous example can be visualized in a wireframe plot like below:
It is similar to wireframe plote, but represents the data by a 3D surface. The previous example can be represented by a surface plot via the plot_surface() function
In the above two plottings (surface and wireframe), the input x and y values were evenly spaced. In many cases, the values can be random and thus can limit the suitability of the functions. Surface triangulation plotting can be handy in these situations which tries to form triangles among the adjacent points.
Surface triangulation doesn't always produce the perfect surface. But it can be a good indicator of how the surface looks if the grid data are random.
So, lets first import the modules.
Simple line and scattered points are plotted via plot3D() and scatter3D() functions respectively. Axis label, line style, size, etc can be customized in a similar fashion to 2D plot() function. Below an example is given:
This can be performed in a similar fashion to 2D contour plot via the contour3D() function.
from mpl_toolkits import mplot3d import numpy as np import matplotlib.pyplot as plt
Python covers a wide range of 3D plotting. In this article, some of the major plotting functions will be discussed.
- Basic plotting: Line and scatter plotting:
Simple line and scattered points are plotted via plot3D() and scatter3D() functions respectively. Axis label, line style, size, etc can be customized in a similar fashion to 2D plot() function. Below an example is given:fig = plt.figure() ax=plt.axes(projection='3d') zline = np.linspace(0, 15, 100) xline = np.sin(zline) yline = np.cos(zline) ax.plot3D(xline, yline, zline, 'r-') ax.set(xlabel='x values', ylabel='y values', zlabel='z values') fig.set_size_inches(8,7) plt.show()
A similar scatter plot is shown in the next example:
#Scatter plotting fig = plt.figure() ax=plt.axes(projection='3d') zline = np.linspace(0, 15, 100) xline = np.sin(zline) yline = np.cos(zline) ax.scatter3D(xline, yline, zline, c=zline, cmap='Greens') ax.set(xlabel='x values', ylabel='y values', zlabel='z values') fig.set_size_inches(8,7) plt.show()
- Contour plotting
This can be performed in a similar fashion to 2D contour plot via the contour3D() function.#contour plot def f(x, y): return np.sin(np.sqrt(x ** 2 + y ** 2)) x = np.linspace(-6, 6, 30) y = np.linspace(-6, 6, 30) X, Y = np.meshgrid(x, y) Z = f(X, Y) fig = plt.figure() ax = plt.axes(projection='3d') cp=ax.contour3D(X, Y, Z, 50, cmap='hot') ax.set(xlabel='x values', ylabel='y values', zlabel='z values') plt.colorbar(cp) fig.set_size_inches(8,7) plt.show()
Sometimes, the plotted graph's orientation can be inconvenient for observation. The graph can be rotted above with respect to the xy plane and around the z axis. In the following example, the above graph is rotated 60 degree above the xy plane and 35 degree around the z axis in a counterclockwise manner via the view_init() function.
ax.view_init(60, 35) fig
- Wireframe plot
The previous example can be visualized in a wireframe plot like below:#wireframe plotting fig = plt.figure() ax = plt.axes(projection='3d') ax.plot_wireframe(X, Y, Z, color='black') ax.set(xlabel='x values', ylabel='y values', zlabel='z values') fig.set_size_inches(8,7) plt.show()
- Surface plot:
It is similar to wireframe plote, but represents the data by a 3D surface. The previous example can be represented by a surface plot via the plot_surface() function#surface plotting fig=plt.figure() ax = plt.axes(projection='3d') sp=ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap='viridis', edgecolor='none') ax.set(xlabel='x values', ylabel='y values', zlabel='z values') plt.colorbar(sp) fig.set_size_inches(8,7) plt.show()
- Surface Triangulation plotting:
In the above two plottings (surface and wireframe), the input x and y values were evenly spaced. In many cases, the values can be random and thus can limit the suitability of the functions. Surface triangulation plotting can be handy in these situations which tries to form triangles among the adjacent points. In plot_trisurf() function, unlike the previous two methods, x,y,z values are one dimensional arrays.
theta = 2 * np.pi * np.random.random(1000) r = 6 * np.random.random(1000) x = np.ravel(r * np.sin(theta)) y = np.ravel(r * np.cos(theta)) z = f(x, y) fig=plt.figure() ax = plt.axes(projection='3d') ax.plot_trisurf(x, y, z, cmap='viridis', edgecolor='none') ax.set(xlabel='x values', ylabel='y values', zlabel='z values') fig.set_size_inches(8,7) plt.show()
The full code can be found here.
If you don't know about 2D plotting basics used in this article,please see the previous articles of this series:
https://rafe76.blogspot.com/2021/01/python-matplotlib-plotting-series.html
https://rafe76.blogspot.com/2021/01/python-matplotlib-plotting-series.html
Reference book:
Comments
Post a Comment