Bài 26: 3D Surface plot – Matplotib Cơ Bản

Trang chủ » Training » Bài 26: 3D Surface plot – Matplotib Cơ Bản
25/02/2022 Training 122 viewed

1. Khái niệm :

Biểu đồ mặt cho thấy mối quan hệ chức năng giữa một biến phụ thuộc được chỉ định (Y) và hai biến độc lập (X và Z). Biểu đồ là kết hợp với biểu đồ đường viền. Biểu đồ mặt giống như biểu đồ wireframe, nhưng mỗi mặt của wireframey là một đa giác được lấp đầy. Điều này có thể hỗ trợ nhận thức về cấu trúc liên kết của bề mặt đang được hình dung. Hàm plot_surface () x, y và z làm đối số.
from mpl_toolkits import mplot3d
import numpy as np
import matplotlib.pyplot as plt
x = np.outer(np.linspace(-2, 2, 30), np.ones(30))
y = x.copy().T # transpose
z = np.cos(x ** 2 + y ** 2)

fig = plt.figure()
ax = plt.axes(projection='3d')

ax.plot_surface(x, y, z,cmap='viridis', edgecolor='none')
ax.set_title('Surface plot')
plt.show()
Kết quả :

2. Ví dụ :

'''
======================
3D surface (color map)
======================

Demonstrates plotting a 3D surface colored with the coolwarm color map.
The surface is made opaque by using antialiased=False.

Also demonstrates using the LinearLocator and custom formatting for the
z axis tick labels.
'''

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import numpy as np


fig = plt.figure()
ax = fig.gca(projection='3d')

# Make data.
X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)

# Plot the surface.
surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm,
                       linewidth=0, antialiased=False)

# Customize the z axis.
ax.set_zlim(-1.01, 1.01)
ax.zaxis.set_major_locator(LinearLocator(10))
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))

# Add a color bar which maps values to colors.
fig.colorbar(surf, shrink=0.5, aspect=5)

plt.show()
Chia sẻ:
Tags:
TOP HOME