Home >Backend Development >Python Tutorial >Can python draw 3D pictures?
3D graphics are widely used in fields such as data analysis, data modeling, graphics and image processing. The following will introduce to you how to use python to draw 3D graphics, including 3D scatter points, 3D surfaces, 3D Drawing of outlines, 3D straight lines (curves) and 3D text, etc.
Preparation work:
To draw 3D graphics in python, you still use the commonly used drawing module matplotlib, but you need to install the mpl_toolkits toolkit. The installation method is as follows: windows From the command line, go to the Scripts folder in the python installation directory and execute: pip install --upgrade matplotlib; execute this command directly in the Linux environment.
After installing this module, you can call the mplot3d class under mpl_tookits to draw 3D graphics.
Related recommendations: "python video tutorial"
The following takes the drawing process of a sphere as an example
1. 3D Drawing of surface shape
from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt import numpy as np fig = plt.figure() ax = fig.add_subplot(111, projection='3d') # Make data u = np.linspace(0, 2 * np.pi, 100) v = np.linspace(0, np.pi, 100) x = 10 * np.outer(np.cos(u), np.sin(v)) y = 10 * np.outer(np.sin(u), np.sin(v)) z = 10 * np.outer(np.ones(np.size(u)), np.cos(v)) # Plot the surface ax.plot_surface(x, y, z, color='b') plt.show()
Spherical surface, the results are as follows:
The above is the detailed content of Can python draw 3D pictures?. For more information, please follow other related articles on the PHP Chinese website!