Wednesday, December 26, 2012

2D Animation in XY plane using Scatter

Hi. I write about matplotlib today.
It is a Python 2D, 3D plotting library which draw high quality figures.

matplotlib
URL: http://matplotlib.org/

Summary:
This article is written about matplotlib.
I show you how to create an animation using the scatter.
In scatter, you can update the coordinates of the points by using the set_offsets method.

I show example python script.
This script is plotting three points in xy coordinates.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np

def _update_plot(i, fig, scat):
    scat.set_offsets(([0, i],[50, i],[100, i]))
    print('Frames: %d' %i)

    return scat,

fig =  plt.figure()                

x = [0, 50, 100]
y = [0, 0, 0]

ax = fig.add_subplot(111)
ax.grid(True, linestyle = '-', color = '0.75')
ax.set_xlim([-50, 200])
ax.set_ylim([-50, 200])

scat = plt.scatter(x, y, c = x)
scat.set_alpha(0.8)

anim = animation.FuncAnimation(fig, _update_plot, fargs = (fig, scat),
                               frames = 100, interval = 100)
              
plt.show()
Flow of a process.
  1. Import the required method.
  2. import matplotlib.pyplot as plt
    import matplotlib.animation as animation
    import numpy as np
    
  3. Define a callback function.
  4. def _update_plot(i, fig, scat):
        scat.set_offsets(([0, i],[50, i],[100, i]))
        print('Frames: %d' %i)
    
        return scat,
    
    Note:
    In instantiate scatter, value of X and Y is set [x1, x2, ..., xN], [y1, y2, ..., yN].
    But in set_offsets method, you set as follows.
    set_offsets(([x1, y1], [x2, y2], ...., [xN, yN]))
    
  5. Instantiate the Figure Object.
  6. fig =  plt.figure()
    
  7. Sets the value of the X and Y.
  8. x = [0, 50, 100]
    y = [0, 0, 0]
    
  9. Instantiate the Subplot Object.
  10. ax = fig.add_subplot(111)
    
  11. Sets a grid and a limits of the X and Y.
  12. ax.grid(True, linestyle = '-', color = '0.75')
    ax.set_xlim([-50, 200])
    ax.set_ylim([-50, 200])
    
  13. Instantiate the Scatter Object.
  14. scat = plt.scatter(x, y, c = x)
    
    x:X-axis values.
    [x1, x2, ..., xN]
    y:Y-axis values.
    [y1, y2, ..., yN]
    c:Color
    It changes according to the value of the X axis.
    set_offsets(([x1, y1], [x2, y2], ...., [xN, yN]))
    
  15. Sets a Alpha Channel of the points.
  16. scat.set_alpha(0.8)
    
  17. Instantiate the FuncAnimation Object.
  18. anim = animation.FuncAnimation(fig, _update_plot, fargs = (fig, scat),
                                   frames = 100, interval = 100)
    
    fig: Figure Object
    _update_plot: Callback Function
    fargs: Value to be passed to the callback function
    frams: Number of frames
    interval: Call interval of the callback function

  19. Start the Animation.
  20. plt.show()
    
Movie:



Thanks you for read my page.

4 comments:

  1. its a very constructive post and helping to read about the information you have shared here. Thanks for the share. 2d animation studios

    ReplyDelete
  2. How do I use it for 3d animation.
    I have modified your code as per my best but it dordoes work.
    Please help..

    ReplyDelete
  3. Thank you so much for this tutorial!!

    ReplyDelete