Problem trying to animate Brownian Motion with matplotlib
up vote
0
down vote
favorite
Im really new in programming with Python and in my final project I need to create this animation where 10 points are randomly moving in space (Brownian motion).
My teacher gave me some examples but I just cant figure out why my program is not working correctly. The error says:
"_included_frames frame_dir=os.path.dirname(frame_list[0]),
IndexError: list index out of range"
Sorry if I didn´t express myself correctly but also English is not my native language.
from math import *
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
import matplotlib.animation as animation
fig = plt.figure()
ax = plt.axes(projection='3d')
N=10
x=500*np.random.random(N)
y=500*np.random.random(N)
z=500*np.random.random(N)
def frame(w):
ax.clear()
x=x+np.random.normal(0.0,50.0,10)
y=y+np.random.normal(0.0,50.0,10)
z=z+np.random.normal(0.0,50.0,10)
mensaje="Movimiento Browniano"
plt.title(mensaje)
ax.set_xlim3d(-500.0,500.0)
ax.set_ylim3d(-500.0,500.0)
ax.set_zlim3d(-500.0,500.0)
plot=ax.scatter3D(x, y, z, c='r')
return plot
anim = animation.FuncAnimation(fig, frame, frames=100, blit=False)
anim.save( 'MovimientoBrowniano.html', fps=5 )
python matplotlib
add a comment |
up vote
0
down vote
favorite
Im really new in programming with Python and in my final project I need to create this animation where 10 points are randomly moving in space (Brownian motion).
My teacher gave me some examples but I just cant figure out why my program is not working correctly. The error says:
"_included_frames frame_dir=os.path.dirname(frame_list[0]),
IndexError: list index out of range"
Sorry if I didn´t express myself correctly but also English is not my native language.
from math import *
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
import matplotlib.animation as animation
fig = plt.figure()
ax = plt.axes(projection='3d')
N=10
x=500*np.random.random(N)
y=500*np.random.random(N)
z=500*np.random.random(N)
def frame(w):
ax.clear()
x=x+np.random.normal(0.0,50.0,10)
y=y+np.random.normal(0.0,50.0,10)
z=z+np.random.normal(0.0,50.0,10)
mensaje="Movimiento Browniano"
plt.title(mensaje)
ax.set_xlim3d(-500.0,500.0)
ax.set_ylim3d(-500.0,500.0)
ax.set_zlim3d(-500.0,500.0)
plot=ax.scatter3D(x, y, z, c='r')
return plot
anim = animation.FuncAnimation(fig, frame, frames=100, blit=False)
anim.save( 'MovimientoBrowniano.html', fps=5 )
python matplotlib
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Im really new in programming with Python and in my final project I need to create this animation where 10 points are randomly moving in space (Brownian motion).
My teacher gave me some examples but I just cant figure out why my program is not working correctly. The error says:
"_included_frames frame_dir=os.path.dirname(frame_list[0]),
IndexError: list index out of range"
Sorry if I didn´t express myself correctly but also English is not my native language.
from math import *
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
import matplotlib.animation as animation
fig = plt.figure()
ax = plt.axes(projection='3d')
N=10
x=500*np.random.random(N)
y=500*np.random.random(N)
z=500*np.random.random(N)
def frame(w):
ax.clear()
x=x+np.random.normal(0.0,50.0,10)
y=y+np.random.normal(0.0,50.0,10)
z=z+np.random.normal(0.0,50.0,10)
mensaje="Movimiento Browniano"
plt.title(mensaje)
ax.set_xlim3d(-500.0,500.0)
ax.set_ylim3d(-500.0,500.0)
ax.set_zlim3d(-500.0,500.0)
plot=ax.scatter3D(x, y, z, c='r')
return plot
anim = animation.FuncAnimation(fig, frame, frames=100, blit=False)
anim.save( 'MovimientoBrowniano.html', fps=5 )
python matplotlib
Im really new in programming with Python and in my final project I need to create this animation where 10 points are randomly moving in space (Brownian motion).
My teacher gave me some examples but I just cant figure out why my program is not working correctly. The error says:
"_included_frames frame_dir=os.path.dirname(frame_list[0]),
IndexError: list index out of range"
Sorry if I didn´t express myself correctly but also English is not my native language.
from math import *
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
import matplotlib.animation as animation
fig = plt.figure()
ax = plt.axes(projection='3d')
N=10
x=500*np.random.random(N)
y=500*np.random.random(N)
z=500*np.random.random(N)
def frame(w):
ax.clear()
x=x+np.random.normal(0.0,50.0,10)
y=y+np.random.normal(0.0,50.0,10)
z=z+np.random.normal(0.0,50.0,10)
mensaje="Movimiento Browniano"
plt.title(mensaje)
ax.set_xlim3d(-500.0,500.0)
ax.set_ylim3d(-500.0,500.0)
ax.set_zlim3d(-500.0,500.0)
plot=ax.scatter3D(x, y, z, c='r')
return plot
anim = animation.FuncAnimation(fig, frame, frames=100, blit=False)
anim.save( 'MovimientoBrowniano.html', fps=5 )
python matplotlib
python matplotlib
edited Nov 11 at 5:36
Tân Nguyễn
1
1
asked Nov 11 at 5:21
Victor
1
1
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
There are two main problems with your code.
x
,y
andz
are attempted to be changed locally in your function. However you really want to change the variables defined outside of the function scope. You may easily do that by declaring them globally: Addglobal x,y,z
in your function.
You are trying to save the animation to an html file. That is no valid video format. I don't know which format you are targeting here, but a common option would be an animated gif, which could be produced by
anim.save('MovimientoBrowniano.gif', writer = "pillow", fps=5 )
Than you for your answer, I already solved the problem changing x,y,z for a,b,c in my frame función. But I'll try yours and see what happens :)
– Victor
Nov 12 at 16:32
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
There are two main problems with your code.
x
,y
andz
are attempted to be changed locally in your function. However you really want to change the variables defined outside of the function scope. You may easily do that by declaring them globally: Addglobal x,y,z
in your function.
You are trying to save the animation to an html file. That is no valid video format. I don't know which format you are targeting here, but a common option would be an animated gif, which could be produced by
anim.save('MovimientoBrowniano.gif', writer = "pillow", fps=5 )
Than you for your answer, I already solved the problem changing x,y,z for a,b,c in my frame función. But I'll try yours and see what happens :)
– Victor
Nov 12 at 16:32
add a comment |
up vote
0
down vote
There are two main problems with your code.
x
,y
andz
are attempted to be changed locally in your function. However you really want to change the variables defined outside of the function scope. You may easily do that by declaring them globally: Addglobal x,y,z
in your function.
You are trying to save the animation to an html file. That is no valid video format. I don't know which format you are targeting here, but a common option would be an animated gif, which could be produced by
anim.save('MovimientoBrowniano.gif', writer = "pillow", fps=5 )
Than you for your answer, I already solved the problem changing x,y,z for a,b,c in my frame función. But I'll try yours and see what happens :)
– Victor
Nov 12 at 16:32
add a comment |
up vote
0
down vote
up vote
0
down vote
There are two main problems with your code.
x
,y
andz
are attempted to be changed locally in your function. However you really want to change the variables defined outside of the function scope. You may easily do that by declaring them globally: Addglobal x,y,z
in your function.
You are trying to save the animation to an html file. That is no valid video format. I don't know which format you are targeting here, but a common option would be an animated gif, which could be produced by
anim.save('MovimientoBrowniano.gif', writer = "pillow", fps=5 )
There are two main problems with your code.
x
,y
andz
are attempted to be changed locally in your function. However you really want to change the variables defined outside of the function scope. You may easily do that by declaring them globally: Addglobal x,y,z
in your function.
You are trying to save the animation to an html file. That is no valid video format. I don't know which format you are targeting here, but a common option would be an animated gif, which could be produced by
anim.save('MovimientoBrowniano.gif', writer = "pillow", fps=5 )
answered Nov 11 at 12:42
ImportanceOfBeingErnest
120k10121193
120k10121193
Than you for your answer, I already solved the problem changing x,y,z for a,b,c in my frame función. But I'll try yours and see what happens :)
– Victor
Nov 12 at 16:32
add a comment |
Than you for your answer, I already solved the problem changing x,y,z for a,b,c in my frame función. But I'll try yours and see what happens :)
– Victor
Nov 12 at 16:32
Than you for your answer, I already solved the problem changing x,y,z for a,b,c in my frame función. But I'll try yours and see what happens :)
– Victor
Nov 12 at 16:32
Than you for your answer, I already solved the problem changing x,y,z for a,b,c in my frame función. But I'll try yours and see what happens :)
– Victor
Nov 12 at 16:32
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53246064%2fproblem-trying-to-animate-brownian-motion-with-matplotlib%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown