Phần 26: 3D Surface plot – Matplotib Basic

Phần 26: 3D Surface plot                        – Matplotib Basic

1. Khái niệm :

Biểu đồ mặt cho ta thấy mối quan hệ tính năng giữa 1 biến phụ thuộc được chỉ định (Y) và 2 biến độc lập (X và Z). Biểu đồ là kết hợp cùng với biểu đồ đường viền. Biểu đồ mặt giống biểu đồ wireframe, nhưng mỗi mặt của wireframey là 1 đa giác được lấp đầy. Điều đó 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.determine()
ax = plt.axes(projection='3d')

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

2. Ví dụ :

'''
======================
3D floor (colour map)
======================

Demonstrates plotting a 3D floor coloured with the coolwarm colour map.
The floor is made opaque through the use of antialiased=False.

Also demonstrates utilizing the LinearLocator and customized 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.determine()
ax = fig.gca(projection='3d')

# Make knowledge.
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 floor.
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 colour bar which maps values to colours.
fig.colorbar(surf, shrink=0.5, side=5)

plt.present()

admin

Leave a Reply

Your email address will not be published. Required fields are marked *