我正在嘗試繪制一個通過lambda. 我總是收到錯誤訊息:
x and y must have the same first dimension but have shapes (20,) and (1,)
但我不知道為什么。
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-4,8,20)
print(x)
fx = lambda x: x - 10 * np.exp(-1/10*((x-2)**2))
plt.plot(x, fx)
plt.show()
uj5u.com熱心網友回復:
fx是一個lambda函式,x是一個串列。您應首先計算所有結果,然后將其傳遞給繪圖:
x = np.linspace(-4,8,20)
fx = lambda x: x - 10 * np.exp(-1/10*((x-2)**2))
y = [fx(val) for val in x] # Not sure, maybe you need to use Numpy too instead ? Not a expert of Numpy
plt.plot(x, y)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/324263.html
標籤:Python 麻木的 matplotlib 拉姆达
