Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Cách tạo đồ thị 4D với Matplotlib dùng dữ liệu tùy ý

Biểu diễn 4D: x,y,z không gian + c màu (color). Dùng scatter 3D với c=c.

  • fig = plt.figure(), ax = add_subplot(111, projection='3d').
  • x,y,z,c = np.random.standard_normal(100) mỗi cái.
  • img = ax.scatter(x, y, z, c=c, cmap='YlOrRd', alpha=1).
  • show().

Ví dụ

from matplotlib import pyplot as plt
import numpy as np
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x = np.random.standard_normal(100)
y = np.random.standard_normal(100)
z = np.random.standard_normal(100)
c = np.random.standard_normal(100)
img = ax.scatter(x, y, z, c=c, cmap='YlOrRd', alpha=1)
plt.show()

Kết quả

Scatter 3D 100 điểm, màu theo giá trị c (colormap YlOrRd), thể hiện chiều thứ 4.