Official Asymptote example – histogram

Category: Asymptote,Official Gallery One-PagerPh. Ivaldi @ 1 h 57 min

 (Compiled with Asymptote version 2.14svn-r5318)
/* This code comes from The Official Asymptote Gallery */

import graph;
import stats;

size(400,200,IgnoreAspect);

int n=10000;
real[] a=new real[n];
for(int i=0; i < n; ++i) a[i]=Gaussrand();

draw(graph(Gaussian,min(a),max(a)),blue);

// Optionally calculate "optimal" number of bins a la Shimazaki and Shinomoto.
int N=bins(a);

histogram(a,min(a),max(a),N,normalize=true,low=0,lightred,black,bars=false);

xaxis("$x$",BottomTop,LeftTicks);
yaxis("$dP/dx$",LeftRight,RightTicks(trailingzero));



Étiquettes : ,

Official Asymptote example – imagehistogram

Category: Asymptote,Official Gallery One-PagerPh. Ivaldi @ 7 h 57 min

 (Compiled with Asymptote version 2.14svn-r5318)
/* This code comes from The Official Asymptote Gallery */

import stats;
import graph;
import palette;
import contour;

size(20cm);

scale(false);

pair[] data=new pair[50000];
for(int i=0; i < data.length; ++i)
data[i]=Gaussrandpair();

// Histogram limits and number of bins
pair datamin=(-0.15,-0.15);
pair datamax=(0.15,0.15);
int Nx=30;
int Ny=30;

int[][] bins=frequency(data,datamin,datamax,Nx,Ny);

real[] values=new real[Nx*Ny];
pair[] points=new pair[Nx*Ny];
int k=0;
real dx=(datamax.x-datamin.x)/Nx;
real dy=(datamax.y-datamin.y)/Ny;
for(int i=0; i < Nx; ++i) {
for(int j=0; j < Ny; ++j) {
values[k]=bins[i][j];
points[k]=(datamin.x+(i+0.5)*dx,datamin.y+(j+0.5)*dy);
++k;
}
}

// Create a color palette
pen[] InvGrayscale(int NColors=256) {
real ninv=1.0/(NColors-1.0);
return sequence(new pen(int i) {return gray(1-17*i*ninv);},NColors);
}

// Draw the histogram, with axes
bounds range=image(points,values,Range(0,40),InvGrayscale());
draw(contour(points,values,new real[] {1,2,3,4,8,12,16,20,24,28,32,36,40},
operator--),blue);
xaxis("$x$",BottomTop,LeftTicks,above=true);
yaxis("$y$",LeftRight,RightTicks,above=true);



Étiquettes : , , , ,

Official Asymptote example – leastsquares

Category: Asymptote,Official Gallery One-PagerPh. Ivaldi @ 22 h 57 min

 (Compiled with Asymptote version 2.14svn-r5318)
/* This code comes from The Official Asymptote Gallery */

size(400,200,IgnoreAspect);

import graph;
import stats;

file fin=input("leastsquares.dat").line();

real[][] a=fin.dimension(0,0);
a=transpose(a);

real[] t=a[0], rho=a[1];

// Read in parameters from the keyboard:
//real first=getreal("first");
//real step=getreal("step");
//real last=getreal("last");

real first=100;
real step=50;
real last=700;

// Remove negative or zero values of rho:
t=rho > 0 ? t : null;
rho=rho > 0 ? rho : null;

scale(Log(true),Linear(true));

int n=step > 0 ? ceil((last-first)/step) : 0;

real[] T,xi,dxi;

for(int i=0; i <= n; ++i) {
real first=first+i*step;
real[] logrho=(t >= first & t <= last) ? log(rho) : null;
real[] logt=(t >= first & t <= last) ? -log(t) : null;

if(logt.length < 2) break;

// Fit to the line logt=L.m*logrho+L.b:
linefit L=leastsquares(logt,logrho);

T.push(first);
xi.push(L.m);
dxi.push(L.dm);
}

draw(graph(T,xi),blue);
errorbars(T,xi,dxi,red);

crop();

ylimits(0);

xaxis("$T$",BottomTop,LeftTicks);
yaxis("$\xi$",LeftRight,RightTicks);



Étiquettes : , , ,