#!/usr/bin/env python # -*- coding: utf-8 -*- from tkinter import * from random import choice # http://docs.python.org/library/random.html import numpy as np import matplotlib.pyplot as plt # http://matplotlib.sourceforge.net/api/pyplot_api.html#module-matplotlib.pyplot import matplotlib.mlab as mlab # http://matplotlib.sourceforge.net/api/mlab_api.html#module-matplotlib.mlab window = Tk() sizex = 200 sizey = 600 canvas = Canvas(window, width = sizex, height = sizey) canvas.pack() x = 100 # initial left-most edge of first ball y = 1 # initial top-most edge of first ball r = 4 # ball diameter depx = 10 # displacement at each move in x direction depy = 0 # create balls: no_particles = 6400 dy = (sizey-2.*y)/(no_particles+1) # y initial separation between balls print(dy) ball_list = [] for i in range(no_particles): ball = canvas.create_oval(x,y,x+r,y+r,fill="red") y = y+dy ball_list.append(ball) #moves no_moves = 6 # number of moves for j in range(no_moves): for ball in ball_list: canvas.move(ball, choice([-1,1])*depx, depy) canvas.after(1) canvas.update() #analysis - histogram # see http://matplotlib.sourceforge.net/examples/api/histogram_demo.html xpos=[] for ball in ball_list: posi = canvas.coords(ball) xpos.append(((no_moves+1.)/no_moves)*(posi[0]-x)/depx) # le facteur (no_moves+1.)/no_moves) permet de gérer la largeur des barres de l'histogramme xh = np.array(xpos) # see http://www.scipy.org/Cookbook/BuildingArrays #print(xh) fig = plt.figure() ax = fig.add_subplot(111) n, bins, patches = ax.hist(xh, (no_moves)+1, facecolor='green', alpha=0.75) print(n,bins, patches) plt.show() #window.mainloop()