如何從資料框繪製曲面圖/3D 圖(Matplotlib)?


要繪製曲面圖/3D 圖,我們需要二維資料點,而不是一維資料框。

步驟

  • 設定圖表的大小並調整子圖之間和周圍的間距。

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

  • 使用 add_subplot() 方法,以子圖佈局的一部分將一個 **'~.axes.Axes'** 新增到圖表中。

  • 初始化一個變數 **'n'**,表示樣本的數量。

  • 使用 numpy 建立 x、y 和 z 資料點。

  • 使用 plot_surface() 方法繪製曲面 3d。

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

示例

import numpy as np
from matplotlib import pyplot as plt

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

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

n = 50
x = np.random.rand(n)
y = np.tan(x)
z = np.random.rand(n, n)

surf = ax.plot_surface(y, x, z, rstride=1, cstride=1, cmap='copper',linewidth=0, antialiased=False)
ax.axis('off')
plt.show()

輸出

更新於: 2021 年 6 月 3 日

3K+ 瀏覽量

開啟你的 職業生涯

完成課程,獲得認證

開始
廣告