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 )









share|improve this question




























    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 )









    share|improve this question


























      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 )









      share|improve this question















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 11 at 5:36









      Tân Nguyễn

      1




      1










      asked Nov 11 at 5:21









      Victor

      1




      1
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          There are two main problems with your code.





          1. x,y and z 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: Add global x,y,z in your function.


          2. 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 )







          share|improve this answer





















          • 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











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          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

























          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.





          1. x,y and z 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: Add global x,y,z in your function.


          2. 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 )







          share|improve this answer





















          • 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















          up vote
          0
          down vote













          There are two main problems with your code.





          1. x,y and z 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: Add global x,y,z in your function.


          2. 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 )







          share|improve this answer





















          • 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













          up vote
          0
          down vote










          up vote
          0
          down vote









          There are two main problems with your code.





          1. x,y and z 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: Add global x,y,z in your function.


          2. 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 )







          share|improve this answer












          There are two main problems with your code.





          1. x,y and z 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: Add global x,y,z in your function.


          2. 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 )








          share|improve this answer












          share|improve this answer



          share|improve this answer










          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


















          • 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


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          Full-time equivalent

          さくらももこ

          13 indicted, 8 arrested in Calif. drug cartel investigation