Hello,
I don't know what I'm doing wrong, but it don't work for me.
I have to open different sets of data, plot it, select an initial point,
make some calculations and save all the data.
Each plot have a diferente initial point, so I thought of do this with a fo=
r
sentences, opening each plot, doing all and so on.
But I open the first one, and then it plots the second one, but don't let m=
e
select the original point, it catches the previos one. And the same for the
rest.
And I don't know why it doesn't work.
Here you have all the program:
--------------------------
#!/usr/bin/python
# -*- encoding: latin1 -*-
#Importamos los m=F3dulos necesarios:
import Numeric as num
import pylab
import random
import array
import sys
from matplotlib.widgets import SpanSelector
from matplotlib.widgets import Cursor
#Leemos el fichero que contiene el valor de la energia y la suma de los
espines:
def readfiles(filename):
"""
Lee el fichero donde estan los datos. La primera fila del fichero nos d=
a
la temperatura y el numero de puntos.
Las dos columnas tienen la energia y la suma de los spines.
"""
fin =3D open (filename, 'r')
fields =3D fin.readline()
fin.close()
labels =3D fields[1:].split()
t =3D eval(labels[1])
n =3D eval(labels[6])
datos =3D pylab.load(filename,comments=3D'#')
energia =3Ddatos[:,0]
sumspines =3Ddatos[:,1]
return t, n, energia, sumspines
def filelist(filename):
fin =3D open(filename,'r')
files =3D []
for line in fin:
files.append(line[:-1]),
return files
def writefile(filename,start,cv,mag,t):
"""
Escribimos en un fichero todos los datos de punto de corte, calores
especificos,
magnetizacion y temperatura
"""
fout =3D open(filename,'w')
fout.write ("#punto de corte cv magnetizacion temperatura"=
)
for i in range (len(start)):
fout.write("%f \t %f \t %f \t %f \n" % (start[i],cv[i],
mag[i], t[i]))
fout.close()
def cv(energia,T,n,g,start):
"""
Calculamos el calor espec=EDfico segun la formula:
Cv=3D k*B**2/(nx*ny)*(<H=B2> - <H>=B2
donde:
<H>=B2 =3D (sumatorio H/n) **2
<H=B2> =3D (sumatorio H) **2 /n
"""
hp =3D sum(energia[start:])/(n*g*g*2.)
energia2 =3D num.asarray(energia)
hp2 =3D sum(energia2[start:]**2)/n*(g*g*2)**2
# print 'Algunos valores de la energia: ',energia[start:start+10]
return (hp2-hp**2)/(g*g*T**2)
def mag(sumspines,n,g,start):
"""
Calculamos la magnetizacion, usando la formula:
<M> =3D (sumatorio S) /n
"""
# print 'Algunos valores de sumspines: ', sumspines[start:start+10]
# print 'la n es: ', n
return sum(sumspines[start:]) /(n*g*g)
def grid(zn):
im1 =3D pylab.imshow(zn, cmap=3Dpylab.cm.gray, interpolation=3D'nearest=
')
pylab.show()
def plotear(energia):
fig=3Dpylab.figure(figsize=3D(8,6))
ax =3D fig.add_subplot(111)
x=3Dpylab.arange(0,len(energia),1)
ax.plot(x,energia,linewidth=3D2)
pylab.grid(True)
cursor =3D Cursor(ax, useblit=3DFalse, color=3D'red', linewidth=3D2)
cursor.horizOn =3D False
def click(event):
global x
#x,y =3D event.x,event.y
if event.button =3D=3D 1:
if event.inaxes:
# print 'coordenada x', event.xdata
x =3D event.xdata
pylab.disconnect(cid)
pylab.close()
return x
def main0 ():
global cid
filename =3D raw_input("Introduce el fichero de datos: ")
# t=3D temperatura
# n=3D numero de pasos
# energia =3D valores de la energia, sin normalizar (hay que multiplicar po=
r
# 1/( 2*nx*ny)
# sumspines=3D valor de la suma de los espines para cada energia
# g =3D grid (nx =3Dny)
t, n, energia, sumspines =3D readfiles(filename)
g =3D float(filename[(filename.find('-')+1):])
plotear(energia)
cid =3D pylab.connect('button_press_event', click)
pylab.show()
x1 =3D int(x)
print 'Coordenada x: ',x1
print 'El calor especifico es: ',cv(energia,t, n,g,x1)
print 'La magnetizacion es: ', mag(sumspines,n,g,x1)
def main1():
global x, cid
filename =3D raw_input("Introduce el fichero con la lista: ")
files =3D filelist(filename)
start=3D[]
cvall=3D[]
magall=3D[]
ts=3D[]
for i in range(len(files)):
t,n,energia,sumspines=3D readfiles(files[i])
g =3D float(files[1][(files[1].find('-')+1):])
plotear(energia)
cid =3D pylab.connect('button_press_event', click)
pylab.show()
x1 =3D int(x)
start.append(x1)
cv1=3Dcv(energia,t, n,g,start[i])
mag1=3D mag(sumspines,n,g,start[i])
cvall.append(cv1)
magall.append(mag1)
ts.append(t)
print 'Coordenada x: ',x1
print 'El calor especifico es: ',cv1
print 'La magnetizacion es: ', mag1
filename2=3D'resultados_globales.%s' % (g)
writefile(filename2,start,cvall,magall,ts)
def main2():
print "todavia no esta hecho"
if __name__ =3D=3D "__main__":
print "Selecciona una de las siguientes opciones: \n "
print "Un solo fichero de datos (0)"
print "Lista de ficheros de datos (1)"
print "Lista de ficheros de datos y valores de corte (2)"
option =3D input()
if option =3D=3D0:
main0()
if option =3D=3D 1:
main1()
if option =3D=3D2:
main2()
-------------------------------------
2007/6/27, Matthias Michler < MatthiasMichler@...>:
>
> Hello darkside,
>
> I'm a little confused that you are sending the email directly to me ...
> and I'm not sure I really understand your problem, but I set up an exampl=
e
> that hopefully satify your needs and shows that it works in principle :
> >------------------------------------------------------------------------=
-----------------------------------
>
> import pylab
>
> def click(event):
> global x # allow to change global variable
> if event.button =3D=3D 1:
> if event.inaxes:
> x =3D event.xdata # change global variable
> print " x - intern =3D ", x
> pylab.disconnect(cid)
> pylab.close()
>
> xlist =3D [] # list for x values
> global x
> for i in xrange(8):
> pylab.figure ()
> pylab.subplot(111)
> pylab.draw()
> cid =3D pylab.connect('button_press_event', click)
> pylab.show()
> print " x - extern =3D ", x
> xlist.append(x)
>
> pylab.figure()
> pylab.subplot(111)
> pylab.plot(xlist, 'b+', ms=3D8, mew=3D4) # plot ensemble of x values
> pylab.show()
>
> >------------------------------------------------------------------------=
-------------------
>
> If this example is not sufficient for you, please set up a little example
> of
> your problem, which can be executed from here, too.
> This you may send to mpl mailing-list.
>
I have already tried this example and it doesn't work for me. It catches
the same x for all the plots.
Thank you for you kind help.
P.D. I send the previous message to you because I clicked the reply button
and I didn't see that it was only you, sorry.
|