python的pandas庫,python做動畫的庫_用matplotlib動畫庫制作等分法動畫

 2023-10-05 阅读 24 评论 0

摘要:經過反復試驗,我找到了解決問題的辦法。在import matplotlib.pyplot as pltfrom matplotlib import animationpython的pandas庫、import numpy as npdef sgn(x):if x > 0:matplotlib可以交互嗎。return 1elif x < 0:return -1pythonmatplotlib、else:return 0def

經過反復試驗,我找到了解決問題的辦法。在import matplotlib.pyplot as plt

from matplotlib import animation

python的pandas庫、import numpy as np

def sgn(x):

if x > 0:

matplotlib可以交互嗎。return 1

elif x < 0:

return -1

pythonmatplotlib、else:

return 0

def bisect(f,a,b):

matplotlib在線,fa = f(a)

fb = f(b)

p = a+(b-a)/2

fp = f(p)

if sgn(fa) == sgn(fp):

return p, b

else:

return a, p

def bisection_method(f,a,b,n):

for i in range(n):

a,b = bisect(f,a,b)

return a,b

def f(x):

return x**2-3

xmin, xmax = 1, 2

yrange = f(xmin), f(xmax)

ymin, ymax = min(yrange), max(yrange)

vf = np.vectorize(f)

x = np.linspace(xmin,xmax)

y = vf(x)

epsilon = 0.1

# Initialize figure

fig = plt.figure()

ax = plt.axes(xlim=(xmin-epsilon,xmax+epsilon), ylim=(ymin,ymax))

curve, = ax.plot([],[], color='blue')

left, = ax.plot([],[],color='red')

right, = ax.plot([],[],color='red')

# Figure reset between frames

def init():

left.set_data([],[])

right.set_data([],[])

curve.set_data([],[])

return left, right, curve,

# Animation of bisection

def animate(i):

a, b = bisection_method(f,xmin,xmax,i)

left.set_data([a,a],[ymin,ymax])

right.set_data([b,b],[ymin,ymax])

curve.set_data(x,y)

return left, right, curve,

anim = animation.FuncAnimation(fig, animate, init_func=init, frames=15, interval=700, blit=True)

plt.grid()

plt.show()

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/3/115035.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息