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

Chồng lớp contourf và surface_plot trong Matplotlib

Vẽ cả contourf (2D) và surface_plot (3D) trên cùng axes 3D.

  • Tạo delta, xrange, yrange, meshgrid x, y.
  • fig = plt.figure(), ax = fig.gca(projection='3d').
  • ax.contour(x, y, sin(x)-cos(y), [0]).
  • ax.plot_surface(x, y, sin(x)-cos(y), cmap="afmhot_r").
  • 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
delta = 0.025
xrange = np.arange(-5.0, 20.0, delta)
yrange = np.arange(-5.0, 20.0, delta)
x, y = np.meshgrid(xrange, yrange)
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.contour(x, y, (np.sin(x) - np.cos(y)), [0])
ax.plot_surface(x, y, (np.sin(x) - np.cos(y)), cmap="afmhot_r")
plt.show()

Kết quả