foreword
The official website address of the Matplotlib drawing tool is http://matplotlib.org/
The third-party library that implements the Matlab graphics function in the Python environment requires the support of the numpy library to support users to easily design graphic displays of two-dimensional and three-dimensional data, and the produced graphics meet publication-level standards.
Other matplotlib articles
python--matplotlib(1) - Programmer Sought
lab environment
Pycharm2020.2.5 Community Edition, win11
text
Three-dimensional graphics:
In addition to referencing matplotlib, you also need to refer to the mpl_toolkits.mplot3d library (from mpl_toolkits.mplot3d import Axes3D), you need to set its drawing mode to 3d (fig .add_subplot(111, projection='3d')).
1. Simple 3D graph
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
sd = fig.add_subplot(111, projection='3d')#111,221,222,223,224
plt.show()
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
sd = fig.add_subplot(111, projection='3d')#111,221,222,223,224
X = [0, 1, 2, 1, 2, 4]
Y = [0, 4, 4, 1, 3, 4]
Z = [0, 4, 0, 0, 2, 4]
sd.plot_trisurf(X, Y, Z)
plt.show()
This 3D map can be rotated for easy observation;
The fourth line of code: 111, is the full screen or the middle, and the remaining (221, 222, 223, 224) correspond to the four corners. Let me take screenshots in turn:
plot_trisurf (z, y, z,...): A function to draw a 3d curved plane.
x, y, and z should be viewed vertically, and a column corresponds to the coordinates of a point.
2. Three-dimensional surface plot_trisurf (potato chips)
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
radius = np.linspace(0.1, 1, 25)
#np.linspace(start = 0.1, stop = 1, num = 25)
#stop 参数数值范围的终止点。通常其为结果的最后一个值,但如果修改endpoint = False, 则结果中不包括该值
angle = np.linspace(0, 2 * np.pi, 60, endpoint=False)
angle = np.repeat(angle[..., np.newaxis],25, axis=0)
#angles[..., np.newaxis]将每个元素转化为列表,np.repeat(a,repeats,axis=None);
# repeats:复制次数;axis=None,flatten当前矩阵,axis=0,增加行数,列数不变,axis=1,增加列数,行数不变
x = np.append(1, (radius * np.cos(angle)).flatten())
y = np.append(0, (radius * np.sin(angle)).flatten())
#flatten()是对多维数据的降维函数
y=y/2
x=x/2
z = np.sin(x * y)
z=z/2
fig = plt.figure()
sd = fig.add_subplot(projection='3d')
sd.plot_trisurf(x, y, z, cmap=plt.get_cmap('YlOrRd'), linewidth=0.1)
plt.show()
a. Import library
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
b. Data preparation
radius = np.linspace(0.1, 1, 25)
angle = np.linspace(0, 2 * np.pi, 60, endpoint=False)
angle = np.repeat(angle[..., np.newaxis],25, axis=0)
x = np.append(1, (radius * np.cos(angle)).flatten())
y = np.append(0, (radius * np.sin(angle)).flatten())
y=y/2
x=x/2
z = np.sin(x * y)
z=z/2
linspace() function
np.linspace(start = 0.1, stop = 1, num = 25)
The starting point of the value range of the start parameter.
The stop point of the value range of the stop parameter. Usually it is the last value of the result, but if endpoint = False is modified, the value is not included in the result
num: the number of data, 25 are selected in this article.
The flatten() function is a dimensionality reduction function for multidimensional data, connecting the rows of the matrix end to end to form a one-dimensional matrix;
repeat() function
np.repeat(a,repeats,axis=0)
repeats: number of copies;
axis=None, turn the matrix into a one-dimensional matrix [1,2,3,4];
axis=0, increase the number of rows and keep the same number of columns;
axis=1, increase the number of columns, the number of rows remains the same
angles[..., np.newaxis] converts each element into a list,
c. Drawing
fig = plt.figure()
sd = fig.add_subplot(projection='3d')
sd.plot_trisurf(x, y, z, cmap=plt.get_cmap('YlOrRd'), linewidth=0.1)
plt.show()
cmap: the role of swapping colors
linewidth: line width
3. Settings such as the title of the 3D surface
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
plt.rcParams["font.sans-serif"] = ["SimHei"]# 正确显示中文和负号
plt.rcParams["axes.unicode_minus"] = False
fig = plt.figure()
sd = fig.add_subplot(111, projection='3d')#111,221,222,223,224
X = [0, 1, 2, 1, 2, 4]
Y = [0, 4, 4, 1, 3, 4]
Z = [0, 4, 0, 0, 2, 4]
sd.set_xlabel('x轴')
sd.set_ylabel('y轴')
sd.set_zlabel('z轴')
plt.title('这是标题')
sd.plot_trisurf(X, Y, Z)
plt.show()
I just used the code in Title 1 to process it directly.
sd.set_xlabel('x axis')#x axis function
sd.set_ylabel('y axis')#y axis function
sd.set_zlabel('z axis')#z axis function
plt.title('This is the title')#Add title function
Because I use Chinese,
plt.rcParams["font.sans-serif"] = ["SimHei"]# Correctly display Chinese and negative signs
plt.rcParams["axes.unicode_minus"] = FalseSo you have to use these two lines of code, if you only use English, just delete it.
4. Summary
My first experience with the numpy library: use pip install matplotlib to install the matplotlib library, and I use pip install in the directory of c:\users\yonghuming\appdata\local\programs\python\python39\scripts for the numpy library numpy-1.22.4-cp39-cp39-win_amd64.whl was installed successfully.
Now I use anaconda, and install most of the libraries directly, saving time and effort.