After failed with regular plot() syntax, I have luck with the scatter() one, :)
""" Cluster """ import numpy as np #untuk operasi array import matplotlib.pyplot as plt #untuk gambar grafik from mpl_toolkits.mplot3d import Axes3D import matplotlib.animation as animation #untuk menggerakkan grafik from itertools import cycle randColor = cycle('bgrcmk').next randMarker= cycle('.,ov^<>12348sp*hH+xDd|_').next
import numpy as np #untuk operasi array import matplotlib.pyplot as plt #untuk gambar grafik import matplotlib.animation as animation #untuk menggerakkan grafik
fig, (ax, ay, az) = plt.subplots(3,sharex=True)
x = 1. y = 1. z = 1. #plt.ylim(-43,43) #plt.xlim(-43,43)
#membuat garis/kurva dengan sumbu-x adalah x, sumbu-y adalah y line, = ax.plot(x, y, 'o' ) line, = ay.plot(x, z, 'o' ) line, = az.plot(y, z, 'o' )
I use Conway's model in Python with numpy and matplotlib module.
""" Cluster """ import numpy as np #untuk operasi array import matplotlib.pyplot as plt #untuk gambar grafik import matplotlib.animation as animation #untuk menggerakkan grafik
fig, ax = plt.subplots()
n = 19
plt.ylim(0,n) plt.xlim(0,n) a = np.zeros((n,n)) a0 = np.zeros((n,n)) #buat nilai awal for i in np.arange(n): for j in np.arange(n): r = np.random.randint(100) if r<50: a0[i,j] = 1 line, = ax.plot(i,j,'o') else: a0[i,j] = 0 line, = ax.plot(i,j,'wo')
a[:,:]=a0[:,:]
def animate(i): global line for i in np.arange(1,n-1): for j in np.arange(1,n-1): #hitung tetangga t = a0[i-1,j-1]+a0[i-1,j]+a0[i-1,j+1]+\ a0[i,j-1]+a0[i,j+1]+\ a0[i+1,j-1]+a0[i+1,j]+a0[i+1,j+1] #hidup atau mati? if a0[i,j]==1: if t<2 or t>3: a[i,j] = 0
else: if t==3: a[i,j] =1 #gambar if a[i,j]==1: line, = ax.plot(i,j,'o') else: line, = ax.plot(i,j,'wo') a0[:,:] = a[:,:] return line,
ani = animation.FuncAnimation(fig, animate, frames=2000, interval=100, blit=False) #ani.save('cluster.mp4',bitrate=1024) plt.show()
Planned to coding tree branch of tree, fractal mode, using turtle module on python. Has some trouble on backward rule. It become flower, by definition, it's still tree, :)
import turtle
#buat pola di sini #kura-kura menghadap ke atas turtle.shape("turtle") turtle.left(90)
s = 37
lv = 7
l = 100 turtle.forward(l)
def mundur(l,level): l = 4./3.*l turtle.backward(l) turtle.right(3*s) maju(l,level)
def maju(l,level): l = 3./4.*l turtle.left(s) turtle.forward(l) level +=1 if level<=lv: maju(l,level) else: mundur(l,level)
maju(l,2)
#agar gambar tak langsung hilang turtle.exitonclick()
It's from previous code, I use it to simulate even function.
from pylab import *
figure(3) n = 39 psi0= zeros(n) # psi = psi0 # x = linspace(0,1,n) V = zeros(n) #V = 39*pow(x,2)
for i in arange(n): if i<n/2.: V[i] = 73. else: V[i] = 0.
psi0[0] = 1. psi0[1] = psi0[0] #for odd function, use another value,
#but psi0[0] must be zero
t = 0 dx = 1./n E = 1. dE = .1 err = .05 while t< 771: #k = 2*dx*dx*(E-V) for i in range (1,n-1): k = 2*dx*dx*(E-V[i]) psi[i+1] = 2*psi0[i]-psi0[i-1]-k*psi0[i] psi0 = psi if abs(psi[n-1])<err: print E plot(x,psi) t += 1 E += dE
Unlike the code before, only chosen energy with psi=0 on both end is plotted.
from pylab import *
n = 59 psi0= zeros(n) psi = psi0 x = linspace(0,1,n)
psi0[0] = 1.
plot(x,psi)
t = 0 dx = 1./16. E = 0. dE = .2 V = 0. while t< 1337: t += 1 E += .01 k = 2*dx*dx*(E-V) for i in range (1,n-1): psi[i+1] = 2*psi0[i]-psi0[i-1]-k*psi0[i]
#psi[2:] = 2*psi0[1:-1]-psi0[:-2]-k*psi0[1:-1] psi0 = psi #print psi[n-1] if abs(psi[n-1])<=dE: #pass print E plot(x,psi)
It's using random parameter, so if it'll show the different result every time it's executed.
""" Cluster """ import numpy as np #untuk operasi array import matplotlib.pyplot as plt #untuk gambar grafik import matplotlib.animation as animation #untuk menggerakkan grafik
fig, ax = plt.subplots()
plt.ylim(0,40) plt.xlim(0,40) #variabel n = 39 x0 = 19 y0 = 19 a = np.zeros((n,n)) a[x0,y0] = 1 #print a rx = [] ry = []
#membuat garis/kurva dengan sumbu-x adalah x, sumbu-y adalah y line, = ax.plot(x0, y0, 'o')
x0 = np.random.randint(n) y0 = np.random.randint(n) x = x0 y = y0
def animate(i): global line global x0,y0,rx,ry,n
#menentukan seed baru x = x0 + np.random.randint(-1,2) y = y0 + np.random.randint(-1,2)
#print 'x = ', x, ' y = ', y #apakah keluar batas? #apakah menyentuh cluster utama? if (x>(n-2)) or (y>(n-2)) or (x<1) or (y<1) or\ (a[x,y-1] + a[x,y] + a[x,y+1] + a[x-1,y-1] + a[x-1,y] + a[x-1,y+1] +\ a[x+1,y-1] + a[x+1,y] + a[x+1,y+1]) >= 1: #renew() #update subcluster menjadi bagian dari cluster (0 ke 1) a[x,y] = 1 for i in np.arange(len(rx)): a[rx[i],ry[i]] = 1 #print a ok = 0 while ok==0: x = np.random.randint(n) y = np.random.randint(n) #cek if (a[x,y]==0) and (x>1) and (y>1) and (x<(n-2))and (y<(n-2)): ok = 1 ok = 0 #kosongkan #print 'rx', rx rx = [] ry = [] #print 'rx',rx
#print 'xnew = ', x, ' y = ', y
#setelah mendapatkan seed baru, rekam titiknya rx.append(x) ry.append(y) x0 = x y0 = y
line, = ax.plot(x,y,'o') return line,
ani = animation.FuncAnimation(fig, animate, frames=777, interval=100, blit=False) ani.save('clusterDLA.mp4',bitrate=1024) #plt.show()
""" Cluster """ import numpy as np #untuk operasi array import matplotlib.pyplot as plt #untuk gambar grafik import matplotlib.animation as animation #untuk menggerakkan grafik
fig, ax = plt.subplots()
plt.ylim(0,40) plt.xlim(0,40) #variabel n = 39 x = 19 y = 19 a = np.zeros((n,n)) a0 = np.zeros((n,n)) a0[x,y] = 1 a[:,:]=a0[:,:] #print a
#membuat garis/kurva dengan sumbu-x adalah x, sumbu-y adalah y line, = ax.plot(x, y, 'o')
def animate(i): global line for i in np.arange(1,n-1): for j in np.arange(1,n-1): if a0[i,j]==1: x = i + np.random.randint(-1,2) y = j + np.random.randint(-1,2) if a[x,y]!=1: a[x,y] = 1 line, = ax.plot(x,y,'o') a0[:,:] = a[:,:] return line,
ani = animation.FuncAnimation(fig, animate, frames=2000, interval=100, blit=False) ani.save('cluster.mp4',bitrate=1024) #plt.show()
So, what's the z-axis for? It's represent the u value, in this case, temperature, as function of r and phi (I know I should use rho, but, ...)
import scipy as sp
from mpl_toolkits.mplot3d import Axes3D from matplotlib import cm from matplotlib.ticker import LinearLocator, FormatStrFormatter import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d.axes3d as p3 import matplotlib.animation as animation
It means to compute 2d diffusion equation just like previous post in polar/cylindrical coordinate, and all went to wrong direction, :)
Still trying to understand matplotlib mplot3d behavior
import scipy as sp
from mpl_toolkits.mplot3d import Axes3D from matplotlib import cm from matplotlib.ticker import LinearLocator, FormatStrFormatter import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d.axes3d as p3 import matplotlib.animation as animation
#!/usr/bin/env python """ A program which uses an explicit finite difference scheme to solve the diffusion equation with fixed boundary values and a given initial value for the density.
Two steps of the solution are stored: the current solution, u, and the previous step, ui. At each time- step, u is calculated from ui. u is moved to ui at the end of each time-step to move forward in time.
dx=0.01 # Interval size in x-direction. dy=0.01 # Interval size in y-direction. a=0.5 # Diffusion constant. timesteps=500 # Number of time-steps to evolve system. t=0.
nx = int(1/dx) ny = int(1/dy)
dx2=dx**2 # To save CPU cycles, we'll compute Delta x^2 dy2=dy**2 # and Delta y^2 only once and store them.
# For stability, this is the largest interval possible # for the size of the time-step: dt = dx2*dy2/( 2*a*(dx2+dy2) )
# Start u and ui off as zero matrices: ui = sp.zeros([nx,ny]) u = sp.zeros([nx,ny])
# Now, set the initial conditions (ui). for i in range(nx): for j in range(ny): if ( ( (i*dx-0.5)**2+(j*dy-0.5)**2 <= 0.1) & ((i*dx-0.5)**2+(j*dy-0.5)**2>=.05) ): ui[i,j] = 1 ''' def evolve_ts(u, ui): global nx, ny """ This function uses two plain Python loops to evaluate the derivatives in the Laplacian, and calculates u[i,j] based on ui[i,j]. """ for i in range(1,nx-1): for j in range(1,ny-1): uxx = ( ui[i+1,j] - 2*ui[i,j] + ui[i-1, j] )/dx2 uyy = ( ui[i,j+1] - 2*ui[i,j] + ui[i, j-1] )/dy2 u[i,j] = ui[i,j]+dt*a*(uxx+uyy) ''' def evolve_ts(u, ui): """ This function uses a numpy expression to evaluate the derivatives in the Laplacian, and calculates u[i,j] based on ui[i,j]. """ u[1:-1, 1:-1] = ui[1:-1, 1:-1] + a*dt*( (ui[2:, 1:-1] - 2*ui[1:-1, 1:-1] + ui[:-2, 1:-1])/dx2 + (ui[1:-1, 2:] - 2*ui[1:-1, 1:-1] + ui[1:-1, :-2])/dy2 )
# Now, start the time evolution calculation... #tstart = time.time()
f1 = gcurve(color=color.blue)
while True: rate(60) #for m in range(1, timesteps+1): if t<timesteps: t+=dt evolve_ts(u, ui) ui[:] = u[:] # I add this line to update ui value (not present in original code) #print "Computing u for m =", m f1.gcurve.pos = [] for i in arange(nx): f1.plot(pos=(i,u[nx/2,i]))
#tfinish = time.time()
#print "Done." #print "Total time: ", tfinish-tstart, "s" #print "Average time per time-step using numpy: ", ( tfinish - tstart )/timesteps, "s."
If we want to set b as finite difference result of a, we may tempted to do this
for i in range (9): b[i] = a[i+1]-a[i]
There's another (faster) way. The performance's close to the pure C, :)
b[:-1] = a[1:]-a[:-1]
What's that?
Numpy has slice form for array. If we have an array with length 10, the a[:] refers to all value in a.
a[1:] refers to a[1] to a[9] (without a[0]) a[3:] refers to a[3] to a[9] a[:-1] refers to a[0] to a[8] a[:-3] refers to a[0] to a[6] a[1:-1] refers to a[1] to a[8] ... and so on