08 Python Matplotlib library to draw contour lines and 3D plots

Draw contour map

Use pyplot to draw contour map

x = np.linspace(-10, 10, 100)
y = np.linspace(-10, 10, 100)

# 计算 x 和 y 的相交点 a
X, Y = np.meshgrid(x, y)

# 计算Z的坐标

Z = np.sqrt(X**2 + Y**2)
plt.contourf(X, Y, Z)
#或
plt.contour(X, Y, Z)

plt.show()

Output result

Insert picture description here

Draw 3D

from mpl_toolkits.mplot3d import Axes3D

# 创建X, Y, Z 坐标
X = [1, 1, 2, 2]
Y = [3, 4, 4, 3]
Z = [1, 100, 1, 1]
fig = plt.figure()
# 创建一个Axes3D的子图放在figure画布里边
ax = Axes3D(fig)
ax.plot_trisurf(X, Y, Z)
plt.show()

Output result:

Insert picture description here

Published 36 original articles · praised 0 · visits 623

Guess you like

Origin blog.csdn.net/Corollary/article/details/105401543