Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

การวาง contourf plot และ surface_plot ชั้นกันใน Matplotlib


ซ้อน contourf และ surface plot แบบ 3D

วาด contour plot และ surface plot บน axes 3D เดียวกัน

ขั้นตอน

  1. กำหนดตัวแปร delta, xrange, yrange, x, y ด้วย NumPy
  2. สร้าง figure ด้วย figure()
  3. ดึง axes 3D ด้วย projection='3d'
  4. สร้าง 3D contour plot ด้วยข้อมูล x, y
  5. วาด surface plot ด้วยข้อมูล x, y
  6. แสดงด้วย show()

ตัวอย่างโค้ด

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()

ผลลัพธ์

การวาง contourf plot และ surface_plot ชั้นกันใน Matplotlib