我想在 Matplotlib 中繪制一些方程。但它與 Wolframalpha 的結果不同。
這是等式:
y = 10yt y^2t 20
wolframalpha 中的繪圖結果為:

但是當我想用這些代碼在matplotlib中繪制它時
# Creating vectors X and Y
x = np.linspace(-2, 2, 100)
# Assuming α is 10
y = ((10*y*x) ((y**2)*x) 20)
# Create the plot
fig = plt.figure(figsize = (10, 5))
plt.plot(x, y)
結果是:

有什么建議修改代碼以使其具有與 wolframalpha 相似的繪圖結果嗎?謝謝
uj5u.com熱心網友回復:
正如@Him 在評論中所建議的那樣,y = ((10*y*x) ((y**2)*x) 20)不會描述關系,而是進行分配,因此y出現在等式兩邊的事實使這變得困難。
用 x 清晰地表達 y 并非易事,但用 y 表達 x 相對容易,然后繪制該關系,如下所示:
import numpy as np
import matplotlib.pyplot as plt
y = np.linspace(-40, 40, 2000)
x = (y-20)*(((10*y) (y**2))**-1)
fig, ax = plt.subplots()
ax.plot(x, y, linestyle = 'None', marker = '.')
ax.set_xlim(left = -4, right = 4)
ax.grid()
ax.set_xlabel('x')
ax.set_ylabel('y')
這會產生以下結果:

如果你試圖用一條線而不是點來繪制它,你會得到一個很大的不連續性,因為漸近四肢試圖連接起來

因此,您必須定義相同的函式并在三個不同的范圍內對其進行評估并將它們全部繪制出來,這樣您就不會得到任何交叉。
import numpy as np
import matplotlib.pyplot as plt
y1 = np.linspace(-40, -10, 2000)
y2 = np.linspace(-10, 0, 2000)
y3 = np.linspace(0, 40, 2000)
x = lambda y: (y-20)*(((10*y) (y**2))**-1)
y = np.hstack([y1, y2, y3])
fig, ax = plt.subplots()
ax.plot(x(y), y, linestyle = '-', color = 'b')
ax.set_xlim(left = -4, right = 4)
ax.grid()
ax.set_xlabel('x')
ax.set_ylabel('y')
這會產生您所追求的結果:

轉載請註明出處,本文鏈接:https://www.uj5u.com/net/530248.html
標籤:Python麻木的matplotlib阴谋wolframalpha
