在 Matplotlib 中繪製 3d 中的 imshow() 影像


要在 Matplotlib 中繪製 3D 中的 imshow() 影像,我們可以執行以下步驟 −

  • 使用 numpy 建立 xxyy 資料點。

  • 使用 X、YZ 獲取資料 (2D)

  • 使用 figure() 方法建立新圖形或啟用現有圖形。

  • 'ax1' 作為子圖排列的一部分新增到圖形中。

  • 以影像形式顯示資料,即在具有資料的 2D 常規柵格上。

  • 'ax2' 作為子圖排列的一部分新增到圖形中。

  • 建立並存儲一組等值線或填充區域。

  • 要顯示圖形,請使用 show() 方法。

示例

import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

xx, yy = np.meshgrid(np.linspace(0, 1, 10), np.linspace(0, 1, 10))

X = xx
Y = yy
Z = 10 * np.ones(X.shape)

data = np.cos(xx) * np.cos(xx) + np.sin(yy) * np.sin(yy)
fig = plt.figure()

ax1 = fig.add_subplot(121)
ax1.imshow(data, cmap="plasma", interpolation='nearest', origin='lower', extent=[0, 1, 0, 1])

ax2 = fig.add_subplot(122, projection='3d')
ax2.contourf(X, Y, data, 100, zdir='z', offset=0.5, cmap="plasma")

plt.show()

輸出

更新於:2021 年 8 月 10 日

4K+ 瀏覽

開啟你的 職業生涯

完成課程以獲得認證

立即開始
廣告
© . All rights reserved.