如何在Matplotlib中更新3D箭头动画 [英] How to update 3D arrow animation in matplotlib

查看:630
本文介绍了如何在Matplotlib中更新3D箭头动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用matplotlib在python中重现



我能够生成矢量箭头使用3D颤动功能,但由于我



所以,问题是:如何生成许多不同长度的3D箭头?重要的是,我是否可以通过这种方式生成它们,以便可以轻松地为动画的每一帧进行修改?



到目前为止,这是我的代码,有希望的3D颤动方法:

  import numpy as np 
import matplotlib.pyplot as plt
import mpl_toolkits。 mplot3d.axes3d

ax1 = plt.subplot(111,projection ='3d')

t = np.linspace(0,10,40)

y = np.sin(t)
z = np.sin(t)
行,= ax1.plot(t,y,z,color ='r',lw = 2)

ax1.quiver(t,y,z,t * 0,y,z)
plt.show()


解决方案

就像Azad所建议的那样,一个不大但有效的解决方案是简单地编辑mpl_toolkits / mplot3d / axes3d.py来删除规范化。因为我不想弄乱我的实际matplotlib安装,所以我只是将axes3d.py文件复制到其他脚本所在的目录中,并修改了行

  norm = math.sqrt(u ** 2 + v ** 2 + w ** 2)

 范数= 1 

(请务必更改正确的行。 norm的另一种用法是高出几行。)此外,还可以使axes3d.py在运行时正确运行在mpl目录之外,我从更改了

 。从导入art3d 
。从导入proj3d
。导入axis3d

 来自mpl_toolkits.mplot3d导入art3d 
来自mpl_toolkits.mplot3d导入proj3d
来自mpl_toolkits.mplot3d导入axis3d

这是我能够生成的漂亮动画(不确定颜色出了什么问题,在上传到SO之前看起来还不错)。





生成动画的代码:

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

ax1 = plt.subplot(111,projection ='3d')
plt.ion()
plt.show()

t = np.linspace(0,10 ,40)

的索引,枚举延迟(np.linspace(0,1,20)):
y = np.sin(t + delay)
z = np。如果延迟> sin(t + delay)

0:
line.remove()
ax1.collections.remove(linecol)

行= ax1.plot(t,y,z,color ='r', lw = 2)
linecol = ax1.quiver(t,y,z,t * 0,y,z)

plt.savefig('images / Frame%03i.gif'% index)
plt.draw()

plt.ioff()
plt.show()

现在,如果我只能让那些箭头看上去更漂亮,并且头部充满魅力。但这是一个单独的问题...



编辑:将来,matplotlib不会自动根据此拉出请求


I am trying to reproduce the left plot of this animation in python using matplotlib.

I am able to generate the vector arrows using the 3D quiver function, but as I read here, it does not seem possible to set the lengths of the arrows. So, my plot does not look quite right:

So, the question is: how do I generate a number of 3D arrows with different lengths? Importantly, can I generate them in such a way so that I can easily modify for each frame of the animation?

Here's my code so far, with the not-so-promising 3D quiver approach:

import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d.axes3d

ax1 = plt.subplot(111,projection='3d')

t = np.linspace(0,10,40)

y = np.sin(t)
z = np.sin(t)
line, = ax1.plot(t,y,z,color='r',lw=2)

ax1.quiver(t,y,z, t*0,y,z)
plt.show() 

解决方案

As Azad suggests, an inelegant, but effective, solution is to simply edit the mpl_toolkits/mplot3d/axes3d.py to remove the normalization. Since I didn't want to mess with my actual matplotlib installation, I simply copied the axes3d.py file to the same directory as my other script and modified the line

norm = math.sqrt(u ** 2 + v ** 2 + w ** 2)

to

norm = 1

(Be sure to change the correct line. There is another use of "norm" a few lines higher.) Also, to get axes3d.py to function correctly when it's outside of the mpl directory, I changed

from . import art3d
from . import proj3d
from . import axis3d

to

from mpl_toolkits.mplot3d import art3d
from mpl_toolkits.mplot3d import proj3d
from mpl_toolkits.mplot3d import axis3d

And here is the nice animation that I was able to generate (not sure what's going wrong with the colors, it looks fine before I uploaded to SO).

And the code to generate the animation:

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

ax1 = plt.subplot(111,projection='3d')
plt.ion()
plt.show()

t = np.linspace(0,10,40)

for index,delay in enumerate(np.linspace(0,1,20)):
    y = np.sin(t+delay)
    z = np.sin(t+delay)

    if delay > 0:
        line.remove()
        ax1.collections.remove(linecol)

    line, = ax1.plot(t,y,z,color='r',lw=2)
    linecol = ax1.quiver(t,y,z, t*0,y,z)

    plt.savefig('images/Frame%03i.gif'%index)
    plt.draw()

plt.ioff()
plt.show()

Now, if I could only get those arrows to look prettier, with nice filled heads. But that's a separate question...

EDIT: In the future, matplotlib will not automatically normalize the arrow lengths in the 3D quiver per this pull request.

这篇关于如何在Matplotlib中更新3D箭头动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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