如何在 Matplotlib 中繪製 z = f(x, y) 的平滑 2D 彩色繪圖?
要在 Matplotlib 中繪製 z = f(x, y) 的平滑 2D 彩色繪圖,我們可以採取以下步驟 -
- 設定圖形大小,並調整子圖之間和周圍的填充。
- 使用 numpy 建立 x 和 y 資料點。
- 使用 f(x, y) 獲取 z 資料點。
- 將資料顯示為影像,即,在具有 z 資料點的 2D 正規光柵上。
- 要顯示圖形,請使用 show() 方法。
示例
import numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True def f(x, y): return np.array([i * i + j * j for j in y for i in x]).reshape(5, 5) x = y = np.linspace(-1, 1, 5) z = f(x, y) plt.imshow(z, interpolation='bilinear') plt.show()
輸出
廣告