HOME/Articles/

matplotlib example Firefly03a (snippet)

Article Outline

Python matplotlib example 'Firefly03a'

Functions in program:

  • def update(i):
  • def foo(A,i,j,r):

Modules used in program:

  • import matplotlib.animation as anim
  • import matplotlib.axes as axes
  • import matplotlib.pyplot as plt
  • import matplotlib.pylab as pyl
  • import matplotlib
  • import numpy as np
  • import random

python Firefly03a

Python matplotlib example: Firefly03a

####### PROJECT : FIREFLIES SYNCHRONIZATION 

####### MODEL SYNCHRONISED WITH + 10% BOOST OF EACH FASTER FIREFLY IN THE NEIGHBORHOOD 
####### RADIUS OF INTERACTION : 1-2-3-...
####### POSITION STEADY

#######  LIBRARIES   ###########

import random
import numpy as np
import matplotlib
import matplotlib.pylab as pyl
import matplotlib.pyplot as plt
import matplotlib.axes as axes
import matplotlib.animation as anim

####### FUNCTIONS

####### FUNCTION OF THE NEIGHBORHOOD DEPENDING ON RADIUS
def foo(A,i,j,r):
    return A[max(i-r,0):max(i+r+1,0),max(j-r,0):max(j+r+1,0)]

####### FUNCTION FOR DYNAMICAL PLOT
def update(i):
    return [plt.imshow(Z[i],cmap='plasma',vmin=0,vmax=1)] 

#######  DEFAULTS   ##############
N=int(input('Dwse diastasi plythismou pygolampidwn NXN : ')) # MEGETHOS
Rmin,Rmax = 100,300  # RANGE PAYSEWN

neighbor=int(input('Dwste emveleia radar 1,2,3 : '))

######  RANGE MATRIX

x=[]
for i in range(N):
    x_temp=[]
    for j in range(N):
        x_temp.append(np.random.randint(Rmin,Rmax))
    x.append(x_temp)

x0=np.array(x)
x=np.array(x)

###### SYNCHRONIZATION
count=0
SUM=[]

while True :
    count+=1
    m=[]
    for i in range(N):
        m_temp=[]
        for j in range(N):
            boost=(foo(x,i,j,neighbor)<foo(x,i,j,0)).astype(int).sum()
            temp=(foo(x,i,j,0)-np.multiply(foo(x,i,j,0),0.001*int(boost))).tolist()  # neighborhood matrix 
            if temp[0][0]>x0.min() :
                m_temp.append(temp[0][0])                                              # boost from the fastest of neighborhood * 0.1*Aij 
            else : m_temp.append(x0.min())
        m.append(m_temp)                                                           # (increase 10% for each faster)

    m=np.array(m)

    maxm=m.max()
    print(count,m.max()-m.min())


###### THE BINARY SIGNAL

    S=N*[N*[(maxm+1)*[0]]] # 3d array
    S=np.array(S)

    for i in range(N):
        for j in range(N):
            S[i][j][m[i,j]]=1

##### THE FINAL 3D ARRAY OF BINARY ON OFF LIGHT AND TIME

    if count>1 :    # an yparxei idi o SUM
        SUM.append(S.T)
    else :          # tin prwti fora pou den yparxei o SUM ton dimiourgei
        SUM.append(S.T)

###### CHECKING FOR THE STATE OF SYNCHRONIZATION

    if (m.max()-m.min())<0.05:
        print('Epilthe sygxronismos se '+str(count)+' vimata')
        break

    x=m

###### VISUALIZATION OF THE PHAINOMENO
imcount=0

Z=[]
im=[]

for p in range(len(SUM)):
    SUM[p].tolist()
    for t in range(len(SUM[p])):
        Z.append(SUM[p][t].tolist())
        imcount+=1

Z=np.array(Z)

j=0

fig=plt.figure('Pygolampides')

ani=anim.FuncAnimation(fig,update,frames=np.array(list(range(imcount))),interval=1,blit=True,repeat=False)

plt.show()