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.

Monday, December 24, 2012

Global value on Python

Nice to meet you!
This is my first article in my blog.
I study Robotics at University in Japan.
So I write mainly about robots and programs.

Summary:
This page is written about global value on Python.
You have to declare a 'global' to use global variables in Python.
I show some example ,and in last describe how to use global variables in class.

I show how to use simple global variables.
You can use a global variables by declaring a 'global' in the function
g1 = 0
def gl_test1():
    global g1
  print g1

gl_test1()

EXECUTION RESULT
0
Next I try to change the value of a global variables.
You can recognize that saved the value adding one to every call.
g2 = 0

def gl_test2():
    global g2
    g2 += 1
    print g2

for i in range(10):
    gl_test2()

EXECUTION RESULT
1
2
3
4
5
6
7
8
9
10
Last, I show example of using global variables at Class.
You can access a global variables from the class by putting 'self' as an example.
I recommend that you access to the global variables via the method.
class GlTest(object):
    g = 0
   
    def get(self):
        global g
        return self.g
        
  def add(self, x):
      global g
       self.g += x

gc = GlTest()
gc.get()

EXECUTION RESULT
0

gc.add(100)
gc.get()

EXECUTION RESULT
100
Thanks you for read my page.