如何更改动画中的线条格式? [英] How to change the line formate in animation?

查看:50
本文介绍了如何更改动画中的线条格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用matplotlib制作了此动画,它可以正常工作,但是,我需要添加一些动画标签以使其对应的点移动.

I made this animation using matplotlib and it is working properly, however, i need to add some animated labels to be moving with its corresponding points.

第一个标签是指圆和从椭圆中心开始的水平线的交点,另一个文本标签是在注释其长度的斜线的中间.

The first label to be referring to the intersection point between the circle and the horizontal line from the centre of the ellipse and the other text label is to be in the middle of the inclined line annotating its length.

我尝试了一些想法,但没有任何效果.有任何想法吗?屏幕截图

I tried some ideas but nothing worked properly. Any ideas? screenshot

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation    

fig = plt.figure()
ax = fig.add_subplot(111, autoscale_on=True, xlim=(-6, 6), ylim=(-7, 17))

# ax.grid()

line, = ax.plot([], [], 'k', lw=1)
line2, = ax.plot([], [], 'k--', lw=1)


a,b = 3,2
x,y = list(),list()

x1 =np.array([item/10 for item in  range(-30,31)])

y1 = np.sqrt(b**2 * (1-(x1**2 / a**2)))

x =list(x1)+[-item for item in list(x1)]
y =list(y1)+[-item for item in list(y1)]
plt.plot(x, y, 'k:')
plt.plot((0,0), (0,15), 'k--')

ax.annotate('$A$', xy=(0,15), xytext=(-10, 10),color='b',
                textcoords='offset points')
ax.annotate('$O$', xy=(0,-1), xytext=(-10, 10),color='b',ha='center',
                textcoords='offset points')

ax.annotate('$4a$', xy=(0,7), xytext=(-10, 10),color='b',ha='center',
                textcoords='offset points', family='sans serif')



def animate(i):
    thisx = [0, x[i]]
    thisy = [15, y[i]]
    xx = [x[i], 0]
    yy = [y[i], 0]

    line.set_data(thisx, thisy)
    line2.set_data(xx, yy)

    return line, line2

ani = animation.FuncAnimation(fig, animate, np.arange(0, len(x)), interval=20, blit=False)


ax.annotate('$P$', xy=(3,0), xytext=(0, 0),color='b',ha='center',
                textcoords='offset points', family='sans serif', style='italic')



plt.show()
# ani.save('circular_motion.mp4', fps=20)
#
plt.close()

推荐答案

您可以像更改线属性一样更改注释属性.只需存储从 ax.annotation 命令返回的 Annotation 对象,然后更新其在 animate 函数中的位置.请注意,函数 set_position 无法正常工作,正如 此处 中所述,因此您必须使用 xy 属性.

You can alter the annotation properties in the same way that you alter the line properties. Just store the Annotation object that is returned from the ax.annotation command and then update its position in the animate function. Note that the function set_position does not work properly, as was also noted in here, therefore you have to use the xy attribute.

此外,我注意到当 y 值接近零时,动画运行得更快.您可以通过定义角度向量并从中计算 xy 坐标来修复(如果需要修复).我可以自由地更改您的代码以显示我的意思.

Furthermore, I noticed that your animation runs faster when you y values are close to zero. You can fix that (if it needs fixing) by defining a vector of angles and computing the xy coordinates from that. I took the freedom to alter your code to show what I mean.

关于倾斜线的长度,我在这里标注为 L ,因为您没有说明距离 OP 的长度是多少,但是我想你可以自己填写.

About the length of the inclined line, I annotated it here as L, as you don't state what the length of the distance OP is, but I guess that you can fill that in yourself.

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

fig = plt.figure()
ax = fig.add_subplot(111, autoscale_on=True, xlim=(-6, 6), ylim=(-7, 17))

# ax.grid()

line, = ax.plot([], [], 'k', lw=1)
line2, = ax.plot([], [], 'k--', lw=1)


a,b = 3,2
x,y = list(),list()
z = 15

phi = np.linspace(0,2*np.pi,100)
x =  a * np.cos(phi)
y = -b * np.sin(phi)

plt.plot(x, y, 'k:')
plt.plot((0,0), (0,z), 'k--')

ax.annotate('$A$', xy=(0,15), xytext=(-10, 10),color='b',
                textcoords='offset points')
ax.annotate('$O$', xy=(0,-1), xytext=(-10, 10),color='b',ha='center',
                textcoords='offset points')

ax.annotate('$4a$', xy=(0,7), xytext=(-10, 10),color='b',ha='center',
                textcoords='offset points', family='sans serif')



def animate(i):
    thisx = [0, x[i]]
    thisy = [z, y[i]]
    xx = [x[i], 0]
    yy = [y[i], 0]

    line.set_data(thisx, thisy)
    line2.set_data(xx, yy)

    P.xy = (x[i]*1.05,y[i])
    L.xy = ((x[i]/2)*1.05, z/2+y[i]/2)
    return line, line2, P, L

ani = animation.FuncAnimation(fig, animate, np.arange(0, len(x)), interval=20, blit=False)


P = ax.annotate('$P$', xy=(a,0), xytext=(0, 0),color='b',ha='center',
                textcoords='offset points', family='sans serif', style='italic')
L = ax.annotate('$L$', xy=(a/2,z/2), xytext=(0, 0),color='b',ha='center',
                textcoords='offset points', family='sans serif', style='italic')



plt.show()
# ani.save('circular_motion.mp4', fps=20)
#
plt.close()

希望这会有所帮助.

这篇关于如何更改动画中的线条格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆