Python Forum
Animation (Python 2.7)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Animation (Python 2.7)
#1
I did an animation for fun :

[Image: tumblr_oyn9qnS29v1s0krmio1_1280.gif]

import numpy as np

import matplotlib.animation as animation
from matplotlib import pyplot


fig, g = pyplot.subplots(figsize=(8,6)) #(7,7)

pyplot.subplots_adjust(left = 0.05, right = 0.95, top = 0.94, bottom = 0.06) 

z = "lightblue" # lightblue
pyplot.rcParams['axes.facecolor'] = z
pyplot.rcParams['savefig.facecolor'] = z

def square(Ymin,Xmin,v,cl):
 
 a = 0.15 # 0.15
 
             
 
 pyplot.plot (Xmin,Ymin, markersize = v, marker = 's', alpha = a, markerfacecolor = cl, markeredgecolor = "black")
 
 


 pyplot.grid(False)
 
 pyplot.yticks([])
 pyplot.xticks([])
 pyplot.ylim(-3.5,3.5)
 pyplot.axis("OFF")
 pyplot.xlim(-3.5,3.5)

 pyplot.show() 

 return


#s = 10

fr = 28
ct = 0
cln = 0

Fs = 70 # 80 , 16
f = 5
sample = 60
x = np.arange(sample)
z = np.sin(2.0 * np.pi * f * x / Fs)/13 # 100

z1 = np.cos(2.0 * np.pi * f * x / Fs)/11 #90

  


def update(*args) :
  pyplot.clf()
  global s, ct, fr, z,z1
  
  
  c1 = "steelblue"
  
  a = range(10,170,26) # 26 ## 10,370,26
    
  d  = np.linspace(0.01,0,len(a))
  d1 = np.linspace(0,0.01,len(a))
  
  
  
  
  
   
  c = 0
  for n in a :
    
    #Up
    square(1.75-z1[c],-1.25-d[0]+z[c]+z1[c],n, c1) 
    square(1.75-z1[c], 1.25-d[0]+z[c]-z1[c],n, c1)
    
    #down
    square(-1.75-z1[c],-1.25-d[0]+z[c]+z1[c],n, c1)
    square(-1.75-z1[c], 1.25-d[0]+z[c]-z1[c],n, c1)
    
    c +=1
   
  z  = np.delete(z,0)
  z1 = np.delete(z1,0)
  
    
#  if ct < fr/2 :
#      z +=0.0055
      
      
#  else :
#      z -=0.0055
          
#  ct +=1
  
  
  return ()
  


def init() :
    print ""
    
    return()

anim = animation.FuncAnimation(fig, update, init_func=init, blit = True, frames=fr, repeat = False) 

                              
sf = "squares.gif"      
Sname = "C:\Users\Vader\Desktop\_" + sf   # Change your directory

anim.save(Sname, writer="imagemagick", fps=20)  # 15


def gr() :
  
  c1 = "steelblue"
  
  a = range(10,370,30)
    
  d  = np.linspace(0.01,1,len(a))
  d1 = np.linspace(1,0.01,len(a)) # reverse
  # Seno
  Fs = 60
  f = 5
  sample = 36
  x = np.arange(sample)
  z = np.sin(2.0 * np.pi * f * x / Fs)/20
  z = np.delete(z,0)
  
  f = 10
  x1 = np.arange(sample)
  z1 = np.cos(2.0 * np.pi * f * x1 / Fs)/20
  z1 = np.delete(z1,0)
  
  
  c = 0
  for n in a :
   
     square(0.5+z1[c],0.5-d[0]+z[c],n, c1)  
     square(0.10+z1[c],0.10-d[0]+z[c],n, c1) 
     
     c+=1
  
  pyplot.show()
  return

#gr()




# You must have imagemagick installed in you computer :
# https://www.imagemagick.org/script/index.php
#
# Mine is installed in C:\program files\
If you like to see it in my blog (there is also a link to download the code) : https://datasoaring.blogspot.com.br/2017...ation.html
Reply
#2
Nice animation, but you should clean up your code.
So I can understand it better.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020