簡體   English   中英

Plot a 3d plot in Python 來自二維矩陣中的元素

[英]Plot a 3d plot in Python from elements in a 2D matrix

我想 plot a function f(x 1, x 2 ) 的兩個變量 x 1和 x 2在 3D plot 中。function 包含在二維矩陣中,x 1構成行,x 2構成列。 我 go 如何繪制此圖?

我嘗試將我的 function f 定義為

x1_axis = np.arange(0, 10, 0.1)
x2_axis = np.arange(0, 10, 0.1)

f = [fun[x1, x2] for x1 in x1_axis and x2 in x2_axis]

其中“樂趣”是我存儲值的矩陣。 這會引發錯誤“ValueError:具有多個元素的數組的真值不明確。”

還有其他方法可以實現嗎?

您可以通過將網格傳遞給函數來創建 2d function 矩陣。

import numpy as np
import matplotlib.pyplot as plt

def fun(x1, x2):
    # Define your function here
    return x1 + x2

x1_axis = np.arange(0, 10, 0.1)
x2_axis = np.arange(0, 10, 0.1)

X1, X2 = np.meshgri

d(x1_axis, x2_axis)
F = fun(X1, X2)

你可以比 plot 使用

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X1, X2, F)

plt.show()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM