top of page

Python is good for you like physics is

Student: We learnt C, C++, Fortran, Matlab, Labview, Excell (sic)...

Why should we learn python? What is python?


Reply: because

I do not use Python for a long time but I can say:


  1. During your study, Python is free

  2. After your study, Python is .... free

  3. Python works with any OS

  4. Python is interpreted (I loved and still love QuickBasic)

  5. The community is very dynamic and is made of fine scientists

  6. Python allows data analysis and the graphics are beautiful

  7. You can interface a DAQ, an instrument, a camera (keywords: pyvisa, pydaq, opencv)

  8. Packages are easy to install thanks to anaconda


Seduced? What to do? Here is a procedure that will allow you to live on the free part of the world. Some help in French: https://www.youtube.com/watch?v=22qI92HJTq0


You can also directly install Anaconda https://www.continuum.io/downloads.


Download and install VirtualBox: https://www.virtualbox.org/wiki/Downloads

Download «.iso» Debian: https://www.debian.org/distrib/

Put «.iso» in a folder

Config the Virtual machine:

Start VirtualBox

Create a new Virtual Machine

Boot disk priority: CD/DVD then Hard disk

Change memory, processor and virtual disk as you want

Activate 3D for display

Stockage: Master IDE —> put the path to .iso

5. Start VM

6. Chose “graphic install” and “domain.local” and “Debian+XFCe” and install GRUB on the hard disk when you ask.


Start VM, it should work.


Open a terminal:

su

apt-get install python-idle

apt-get install python-matplotlib

apt-get install python-scipy

(option) apt-get install python-pip


C’est fait!


Example of a Boud’code

In a directory, I have a set of data files that which names have the same structure: «data.txt».


The following code allows to automatically analyse all the file «*.txt» contained in the directory defined by the variable path.



from pylab import *

from scipy.optimize import curve_fit

from scipy import interpolate

import os

import glob



# Curve fit procedures



def cur_pow(x,y):

def f(x,a,b):

return a*x**b





params = curve_fit(f, x, y)

[a,b]=params[0]

print params

xmax=max(x)

xmin=min(x)

x_new=np.linspace(xmin,xmax,1000)



plot(x_new,a*x_new**b,'-r')



return 0











def cur_exp(x,y,a0,b0):

def f(x,a,b):

return a*np.exp(-b*x)



guess=(a0,b0)

params = curve_fit(f, x, y,guess)

[a,b]=params[0]

xmax=max(x)

xmin=min(x)

x_new=np.linspace(xmin,xmax,1000)

print a,b

print params



plot(x_new,a*np.exp(-b*x_new),'g-')



return 0











#Positionning of the data





path='C:\Users\Administrator\Documents\Python Scripts'

os.chdir(path)

path2="*.txt"

dataf=glob.glob(path2) #dataf is a list containing all the names of the files.





# LOOP on ALL .txt files contained in the path





for d in dataf:

#read the data + initialisation of vectors

data=loadtxt(d,delimiter=',')

x=data[:,0]

y=data[:,1]



xaxis='time(s)'

yaxis='angle'



fig=figure(1)

pl=fig.add_subplot(2,1,1)

plot(x,y,'o')

pl=fig.add_subplot(2,1,2)

plot(y,x,'o')





show()





figure(2)



plot(x,x*x,'o')

cur_pow(x,x*x)

cur_exp(x,x*x,1,0.2)



show()




bottom of page