1 回答
TA贡献1826条经验 获得超6个赞
您需要打开交互模式并plt.ion()
替换plt.show()
为fig.canvas.draw()
。检查以下代码:
import matplotlib.pyplot as plt
import keyboard
# from scipy import linspace
from numpy import linspace
import time
r = 3.35
x0 = 0.3
running = True
def cobweb(f, x0):
ax.cla()
t = linspace(0, 1, 100)
l = plt.plot(t, f(t))
plt.plot(t, t)
x, y = x0, f(x0)
for _ in range(100):
fy = f(y)
plt.plot([x, y], [y, y], 'b', linewidth=1)
plt.plot([y, y], [y, fy], 'b', linewidth=1)
x, y = y, fy
plt.xlabel("X n")
plt.ylabel("X n+1")
fig.canvas.draw()
time.sleep(0.01)
plt.ion()
fig, ax = plt.subplots()
while running:
if keyboard.is_pressed('up'):
r += 0.1
if keyboard.is_pressed('down'):
r -= 0.1
cobweb(lambda x: r*x*(1 - x), x0)
按⇧或⇩,您将得到如下图:
注意:如果您使用linspace
by scipy
,您会收到此警告:
DeprecationWarning: scipy.linspace is deprecated and will be removed in SciPy 2.0.0, use numpy.linspace instead
明智的做法是将其替换为
from numpy import linspace
正如我在上面的代码中所做的那样。您的代码的功能不会改变
添加回答
举报