Visualizzazione post con etichetta weed&plot. Mostra tutti i post
Visualizzazione post con etichetta weed&plot. Mostra tutti i post

lunedì 29 agosto 2016

MethPlot Lib

Another fast line of code, so that I know where to look when I need it.
This time is MatplotLib, with LateX in the labels ;-)


import numpy as np
import pylab as pl
data = np.loadtxt('dijmsd5nm')
pl.rc('text', usetex=True)
pl.rc('font', family='serif')
pl.plot(data[:,0], data[:,1], 'ro')
pl.xlabel (r"$\displaystyle\sum_{n=1}^\infty 1$", fontsize= 12, color= 'black')
pl.ylabel(r'\textbf{time}(s)')

pl.show()




A little bit of explanation: in the folder where I use the Python script there are several data files, named something like "dijmsd5nm, dijmsd7_5nm, dijmsd10nm, dijmsd12_5nm, etc", where the underscore stands for the decimal point, since if I want to take these files in windows I am not allowed to have a point otherwise it can be seen as the beginning of the file extension.
If I don't do all the "re.sub" (the substituiting string characters in the file name) matplotlib plots the graphs as they come, i.e not in the good order 5nm->7.5nm->10nm->etc.
So I have to sort them first and then in the for loop that plots they are in order.

And here the refined super confusing version:


import numpy as np
import pylab as pl
import os
import itertools
import re

plotnum= 0
data=[]
dataname=[]
path= '.'
for i in os.listdir(path):
    if os.path.isfile and 'dij' in i:
        dataname.append(i)
        plotnum+=1
#sortthem!
for i in range(0,plotnum):
    for j in range(i,plotnum):
        nameI=re.sub("dijmsd","", dataname[i])
        nameI=re.sub("_", ".", nameI)
        nameI=float(re.sub("nm","",nameI))
        nameJ=re.sub("dijmsd","", dataname[j])
        nameJ=re.sub("_", ".", nameJ)
        nameJ=float(re.sub("nm","",nameJ))
        supp=dataname[i]
        if(nameJ<nameI):
            dataname[i]=dataname[j]
            dataname[j]=supp

for i in range(0,plotnum):
    data.append(np.loadtxt(dataname[i]))
marker = itertools.cycle(('o', '*', 'v', '^', '>', '<', 's', 'D'))

pl.figure(figsize=(8,6), dpi=180)

pl.rc('text', usetex=True)
pl.rc('font', family='serif')
for i in range(0,plotnum):
    name=re.sub("dijmsd","", dataname[i])
    name=re.sub("_", ".", name)
    pl.scatter(data[i][:,0], data[i][:,1],s=30, c=np.random.rand(3,), marker=next(marker), label=name)
pl.title(r"$\delta_{ij}^2$ per shell")
pl.xlabel (r"Shell")
pl.ylabel(r"$\delta_{ij}^2$ $\left[\r{A} ^2\right]$")
pl.xlim(0.,20.)
pl.ylim(0.02, 0.041)
pl.legend(loc="lower right")

pl.savefig("pictures/exercice_2.png", dpi=180)

pl.show()


lunedì 15 febbraio 2016

addiction to GNU pot

Since I do not have Origin (the scientific software, not the EA Steam-copy-paste) I tried to use alternatives like SciDavis or QtPlot. After struggling for hours to get what I needed, I went back to the roots using GNUPlot.

I hereby post a little script that I used to print the same function with different parameters. The function is a LogNormal distribution and I think that, for a fast visualization, my script is more then enough for everyone.
It's not that easy to find all the commands together in a single example and I don't want  to waste any more hours looking around.

Comments start with '#' character.



f(x,m,s,d)=1/((x-d)*s*sqrt(2*pi))*exp(-(log(x-d)-m)**2/(2*s*s))
#the function I want to plot with parameters m, s, d and variable x

set autoscale

set border 3 front 
set tics nomirror out scale 0.75
#these two lines remove the top and right borders, leaving only the 2 xy axes

set yrange [0:0.22]
set xrange [0:35]
#set the min-max range of the two axes x and yù

set ytics 0.025
set xtics 2
#set the spacing on axes, the tics

set ylabel "p(D)"
set xlabel "E.C.D. [nm]"
#sets the label to the axes

plot f(x, 2.38, 0.38, 0) notitle lt -1 lw 2
#plots the 1st function without graph legend, with lines of color -1 (black) and line width of 2 (thicker line than default)

replot f(x, 1.60, 0.46, 1.8129) notitle lt 1 lw 2
#plots the 2nd graph without erasing the first and changing only the color to 2 (red)

pause -1
#this is needed if you call gnuplot via terminal in order to have the image not disappears after few moments