Official Asymptote example – datagraph

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

Figure 0045
(Compiled with Asymptote version 2.14svn-r5318)
/* This code comes from The Official Asymptote Gallery */
    
import graph;

size(200,150,IgnoreAspect);

real[] x={0,1,2,3};
real[] y=x^2;

draw(graph(x,y),red);

xaxis("$x$",BottomTop,LeftTicks);
yaxis("$y$",LeftRight,
      RightTicks(Label(fontsize(8pt)),new real[]{0,4,9}));

Étiquettes : ,


Official Asymptote example – diatom

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

Figure 0047
(Compiled with Asymptote version 2.14svn-r5318)
/* This code comes from The Official Asymptote Gallery */
    
import graph;

size(15cm,12cm,IgnoreAspect);

real minpercent=20;
real ignorebelow=0;
string data="diatom.csv";
string[] group;
int[] begin,end;

defaultpen(fontsize(8pt)+overwrite(MoveQuiet));

file in=input(data).line().csv();

string depthlabel=in;
string yearlabel=in;
string[] taxa=in;
group=in;
begin=in;
real[] depth;
int[] year;
real[][] percentage;

while(true) {
  real d=in;
  if(eof(in)) break;
  depth.push(d);
  year.push(in);
  percentage.push(in);
}

percentage=transpose(percentage);
real depthmin=-min(depth);
real depthmax=-max(depth);

int n=percentage.length;

int final;
for(int taxon=0; taxon < n; ++taxon) {
  real[] P=percentage[taxon];
  if(max(P) < ignorebelow) continue;
  final=taxon;
}  

real angle=45;
real L=3cm;
pair Ldir=L*dir(angle);
real ymax=-infinity;
real margin=labelmargin();

real location=0;

for(int i=0; i < begin.length-1; ++i) end[i]=begin[i+1]-1;
end[begin.length-1]=n-1;

typedef void drawfcn(frame f);
drawfcn[] draw=new drawfcn[begin.length];

pair z0;

for(int taxon=0; taxon < n; ++taxon) {
  real[] P=percentage[taxon];
  real maxP=max(P);
  if(maxP < ignorebelow) continue;
  picture pic;
  real x=1;
  if(maxP < minpercent) x=minpercent/maxP;
  if(maxP > 100) x=50/maxP;
  scale(pic,Linear(true,x),Linear(-1));
  filldraw(pic,(0,depthmin)--graph(pic,P,depth)--(0,depthmax)--cycle,
           gray(0.9));
  xaxis(pic,Bottom,LeftTicks("$%.3g$",beginlabel=false,0,2),above=true);
  xaxis(pic,Top,above=true);

  frame label;
  label(label,rotate(angle)*TeXify(taxa[taxon]),(0,0),N);

  pair z=point(pic,N);
  pair v=max(label);
  int taxon=taxon;
  pic.add(new void(frame f, transform t) {
      pair z1=t*z+v;
      ymax=max(ymax,z1.y+margin);
    });

  for(int i=0; i < begin.length; ++i) {
    pair z=point(pic,N);
    pair v=max(label);
    if(taxon == begin[i]) {
      pic.add(new void(frame f, transform t) {
          pair Z=t*z+v;
          z0=Z;
          pair w0=Z+Ldir;
        });
    } else if(taxon == end[i]) {
      int i=i;
      pair align=2N;
      pic.add(new void(frame, transform t) {
          pair z0=z0;
          pair z1=t*z+v;
          pair w1=z1+Ldir;
          draw[i]=new void(frame f) {
            path g=z0--(z0.x+(ymax-z0.y)/Tan(angle),ymax)--
            (z1.x+(ymax-z1.y)/Tan(angle),ymax)--z1;
            draw(f,g);
            label(f,group[i],point(g,1.5),align);
          };
        });
    }
  }

  add(pic,label,point(pic,N));

  if(taxon == 0) yaxis(pic,depthlabel,Left,RightTicks(0,10),above=true);
  if(taxon == final) yaxis(pic,Right,LeftTicks("%",0,10),above=true);
 
  add(shift(location,0)*pic);
  location += pic.userMax().x;
}

add(new void(frame f, transform) {
    for(int i=0; i < draw.length; ++i)
      draw[i](f);
  });

for(int i=0; i < year.length; ++i)
  if(year[i] != 0) label((string) year[i],(location,-depth[i]),E);

label("\%",(0.5*location,point(S).y),5*S);

Étiquettes : , ,


Official Asymptote example – filegraph

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

Figure 0064
(Compiled with Asymptote version 2.14svn-r5318)
/* This code comes from The Official Asymptote Gallery */
    
import graph;

size(200,150,IgnoreAspect);

file in=input("filegraph.dat").line();
real[][] a=in.dimension(0,0);
a=transpose(a);

real[] x=a[0];
real[] y=a[1];

draw(graph(x,y),red);

xaxis("$x$",BottomTop,LeftTicks);
yaxis("$y$",LeftRight,RightTicks);

Étiquettes : ,


Official Asymptote example – filesurface

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

Figure 0065
(Compiled with Asymptote version 2.14svn-r5318)
/* This code comes from The Official Asymptote Gallery */
    
import graph3;
import palette;

size3(200,IgnoreAspect);

file in=input("filesurface.dat").line();
real[] x=in;
real[] y=in;

real[][] f=in.dimension(0,0);

triple f(pair t) {
  int i=round(t.x);
  int j=round(t.y);
  return (x[i],y[j],f[i][j]);
}

surface s=surface(f,(0,0),(x.length-1,y.length-1),x.length-1,y.length-1);
real[] level=uniform(min(f)*(1-sqrtEpsilon),max(f)*(1+sqrtEpsilon),4);

s.colors(palette(s.map(new real(triple v) {return find(level >= v.z);}),
                 Rainbow())); 

draw(s,meshpen=thick(),render(merge=true));

triple m=currentpicture.userMin();
triple M=currentpicture.userMax();
triple target=0.5*(m+M);

xaxis3("$x$",Bounds,InTicks);
yaxis3("$y$",Bounds,InTicks(Step=1,step=0.1));
zaxis3("$z$",Bounds,InTicks);

/*
picture palette;
size3(palette,1cm);
draw(palette,unitcube,red);
frame F=palette.fit3();
add(F,(M.x,m.y,m.z));
*/

currentprojection=perspective(camera=target+realmult(dir(68,225),M-m),
                              target=target);



Étiquettes : , , , , ,


Official Asymptote example – galleon

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

Figure 0075
(Compiled with Asymptote version 2.14svn-r5318)
/* This code comes from The Official Asymptote Gallery */
    
import obj;

size(15cm);
currentprojection=orthographic(0,2,5,up=Y);

// A compressed version of the required data file may be obtained from:
// http://www.cs.technion.ac.il/~irit/data/Viewpoint/galleon.obj.gz

pen[] surfacepen={darkred,brown,darkred+orange,heavyred,heavyred,darkred+orange,
                  palegreen+blue+lightgrey,darkred,darkred,yellow,darkred,white,
                  white,white,white,white,white};
surfacepen.cyclic=true;

draw(obj("galleon.obj",verbose=false,surfacepen));

Étiquettes : , ,


Official Asymptote example – lmfit1

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

Figure 0126
(Compiled with Asymptote version 2.14svn-r5318)
/* This code comes from The Official Asymptote Gallery */
    
import lmfit;
import graph;

size(10cm, 7cm, IgnoreAspect);

real[] date = { 1790, 1800, 1810, 1820, 1830, 1840, 1850, 1860, 1870, 1880,
1890, 1900, 1910, 1920, 1930, 1940, 1950, 1960, 1970, 1980, 1990 };
real[] population = { 3.929, 5.308, 7.240, 9.638, 12.866, 17.069, 23.192, 31.443,
38.558, 50.156, 62.948, 75.996, 91.972, 105.711, 122.775, 131.669, 150.697,
179.323, 203.185, 226.546, 248.710 };

real t0 = 1776;

real P(real[] params, real t) {
  real P0 = params[0];
  real K = params[1];
  real r = params[2];
  return (K * P0) / (P0 + (K - P0) * exp(-r * (t - t0)));
}

real[] params = { 10, 500, 0.1 };

real res = lmfit.fit(date, population, P, params).norm;

write("P_0 = ", params[0]);
write("K = ", params[1]);
write("r = ", params[2]);
write("error = ", res);

real P(real t) {
  return P(params, t);
}

draw(graph(date, population), blue);
draw(graph(P, t0, 2000), red);
xaxis("Year", BottomTop, LeftTicks);
yaxis("Population in millions", LeftRight, RightTicks);

Étiquettes : , , ,


Official Asymptote example – pdb

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

Figure 0152
(Compiled with Asymptote version 1.87svn-r4652)
/* This code comes from The Official Asymptote Gallery */
    
import three;
import cpkcolors;

// A sample Protein Data Bank file for this example is available from
// http://ndbserver.rutgers.edu/ftp/NDB/coordinates/na-biol/100d.pdb1

bool getviews=true;

currentlight=White;
//currentlight=nolight;

size(200);
currentprojection=perspective(30,30,15);

// Uncomment this line for more accurate (but slower) PDF rendering
//dotgranularity=0;

pen chainpen=green;
pen hetpen=purple;

string filename="100d.pdb1";
//string filename=getstring("filename");

string prefix=stripextension(filename);
file data=input(filename);

pen color(string e) 
{
  e=replace(e," ","");
  int n=length(e);
  if(n < 1) return currentpen;
  if(n > 1) e=substr(e,0,1)+downcase(substr(e,1,n-1));
  int index=find(Element == e);
  if(index < 0) return currentpen;
  return rgb(Hexcolor[index]);
}       

// ATOM
string[] name,altLoc,resName,chainID,iCode,element,charge;
int[] serial,resSeq;
real[][] occupancy,tempFactor;

bool newchain=true;

struct bond 
{
  int i,j;
  void operator init(int i, int j) {
    this.i=i;
    this.j=j;
  }
}

bond[] bonds;

struct atom 
{
  string name;
  triple v;
  void operator init(string name, triple v) {
    this.name=name;
    this.v=v;
  }
}

struct chain
{
  int[] serial;
  atom[] a;
}

int[] serials;
chain[] chains;
atom[] atoms;

while(true) {
  string line=data;
  if(eof(data)) break;
  string record=replace(substr(line,0,6)," ","");
  if(record == "TER") {newchain=true; continue;}
  bool ATOM=record == "ATOM";
  bool HETATOM=record == "HETATM";
  int serial;

  atom a;
  if(ATOM || HETATOM) {
    serial=(int) substr(line,6,5);
    a.name=substr(line,76,2);
    a.v=((real) substr(line,30,8),
         (real) substr(line,38,8),
         (real) substr(line,46,8));
  }
  if(ATOM) {
    if(newchain) {
      chains.push(new chain);
      newchain=false;
    }
    chain c=chains[chains.length-1];
    c.serial.push(serial);
    c.a.push(a);
    continue;
  }
  if(HETATOM) {
    serials.push(serial);
    atoms.push(a);
  }
  if(record == "CONECT") {
    int k=0;
    int i=(int) substr(line,6,5);
    while(true) {
      string s=replace(substr(line,11+k,5)," ","");
     if(s == "") break;
      k += 5;
      int j=(int) s;
      if(j <= i) continue;
      bonds.push(bond(i,j));
     }
  }
}

write("Number of atomic chains: ",chains.length);

int natoms;
for(chain c : chains) {
  for(int i=0; i < c.a.length-1; ++i)
    draw(c.a[i].v--c.a[i+1].v,chainpen,currentlight);
  for(atom a : c.a)
    dot(a.v,color(a.name),currentlight);
  natoms += c.a.length;
}

write("Number of chained atoms: ",natoms);
write("Number of hetero atoms: ",atoms.length);

for(atom h : atoms)
  dot(h.v,color(h.name),currentlight);

write("Number of hetero bonds: ",bonds.length);

for(bond b : bonds) {
  triple v(int i) {return atoms[find(serials == i)].v;}
  draw(v(b.i)--v(b.j),hetpen,currentlight);
}

string options;
string viewfilename=prefix+".views";

if(!error(input(viewfilename,check=false)))
  options="3Dviews="+viewfilename;

if(getviews) {
  picture pic;
  add(pic,embed("label",currentpicture,options=options),(0,0),N);
  label(pic,cameralink("label"),(0,0),S,fontsize(12pt));
  shipout(prefix,pic,options=options);
} else
  shipout(prefix,options=options);

Étiquettes : , , , , ,


Official Asymptote example – secondaryaxis

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

Figure 0193
(Compiled with Asymptote version 2.14svn-r5318)
/* This code comes from The Official Asymptote Gallery */
    
import graph;

size(9cm,6cm,IgnoreAspect);
string data="secondaryaxis.csv";

file in=input(data).line().csv();

string[] titlelabel=in;
string[] columnlabel=in;

real[][] a=in.dimension(0,0);
a=transpose(a);
real[] t=a[0], susceptible=a[1], infectious=a[2], dead=a[3], larvae=a[4];
real[] susceptibleM=a[5], exposed=a[6],infectiousM=a[7];

scale(true);

draw(graph(t,susceptible,t >= 10 & t <= 15));
draw(graph(t,dead,t >= 10 & t <= 15),dashed);

xaxis("Time ($\tau$)",BottomTop,LeftTicks);
yaxis(Left,RightTicks);

picture secondary=secondaryY(new void(picture pic) {
    scale(pic,Linear(true),Log(true));
    draw(pic,graph(pic,t,infectious,t >= 10 & t <= 15),red);
    yaxis(pic,Right,red,LeftTicks(begin=false,end=false));
  });
                             
add(secondary);
label(shift(5mm*N)*"Proportion of crows",point(NW),E);


Étiquettes : , , , ,


Official Asymptote example – westnile

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

Figure 0260
(Compiled with Asymptote version 2.14svn-r5318)
/* This code comes from The Official Asymptote Gallery */
    
import graph;

size(9cm,8cm,IgnoreAspect);
string data="westnile.csv";

file in=input(data).line().csv();

string[] columnlabel=in;

real[][] A=in.dimension(0,0);
A=transpose(A);
real[] number=A[0], survival=A[1];

path g=graph(number,survival);
draw(g);

scale(true);

xaxis("Initial no.\ of mosquitoes per bird ($S_{M_0}/N_{B_0}$)",
      Bottom,LeftTicks);
xaxis(Top);
yaxis("Susceptible bird survival",Left,RightTicks(trailingzero));
yaxis(Right);

real a=number[0];
real b=number[number.length-1];

real S1=0.475;
path h1=(a,S1)--(b,S1);
real M1=interp(a,b,intersect(h1,g)[0]);

real S2=0.9;
path h2=(a,S2)--(b,S2);
real M2=interp(a,b,intersect(h2,g)[0]);

labelx("$M_1$",M1);
labelx("$M_2$",M2);

draw((a,S2)--(M2,S2)--(M2,0),Dotted);
draw((a,S1)--(M1,S1)--(M1,0),dashed);

pen p=fontsize(10pt);

real y3=0.043;
path reduction=(M1,y3)--(M2,y3);
draw(reduction,Arrow,TrueMargin(0,0.5*(linewidth(Dotted)+linewidth())));

arrow(shift(-20,5)*Label(minipage("\flushleft{\begin{itemize}\item[1.]
Estimate proportion of birds surviving at end of season\end{itemize}}",100),
                         align=NNE),
      (M1,S1),NNE,1cm,p,Arrow(NoFill));

arrow(shift(-24,5)*Label(minipage("\flushleft{\begin{itemize}\item[2.]
Read off initial mosquito abundance\end{itemize}}",80),align=NNE),
      (M1,0),NE,2cm,p,Arrow(NoFill));

arrow(shift(20,0)*Label(minipage("\flushleft{\begin{itemize}\item[3.]
Determine desired bird survival for next season\end{itemize}}",90),align=SW),
      (M2,S2),SW,arrowlength,p,Arrow(NoFill));

arrow(shift(8,-15)*Label(minipage("\flushleft{\begin{itemize}\item[4.]
Calculate required proportional reduction in mosquitoes\end{itemize}}",90),
                         align=NW),
      point(reduction,0.5),NW,1.5cm,p,Arrow(NoFill));

Étiquettes : ,