# Using external (delimited) data files for Asymptote contour plots

I need to plot two-dimensional data, which I would like to do in a contour plot. From my personal experience in trying to get nice contour plots using pgf/tikz and what I read in Fill a contour-plot, I wanted to try out Asymptote. As a first step, I am trying to convert the example given in the link above from using a function f(x,y) to using a data set (i.e. generated in matlab,100x100 delimited dat file. From what I read here, I do not need to specify the x and y coordinates, just the minimum and maximum pair, the specification of the height-"function" should then follow from the image.asy example (v in this case):

size(12cm,12cm);

import graph;
import palette;

int n=256;
real ninv=2pi/n;
real[][] v=new real[n][n];

for(int i=0; i < n; ++i)
for(int j=0; j < n; ++j)
v[i][j]=sin(i*ninv)*cos(j*ninv);

pen[] Palette=BWRainbow();

picture bar;

bounds range=image(v,(0,0),(1,1),Palette);
palette(bar,"$A$",range,(0,0),(0.5cm,8cm),Right,Palette,
PaletteTicks("$%+#.1f$"));


I am trying to do the same, but it does not accept my "function" as valid to draw the contours:

no matching function 'contour(real[][], pair, pair, real[], int, guide(... guide[]))

I am more familiar with PGF, but the quality of the Asymptote contours drew me towards it. Could anyone point out my mistake/provide me with a suitable PGF/Tikz alternative?

Code below:

\documentclass{article}
\usepackage[pdftex]{graphicx}
\usepackage[inline]{asymptote}
\begin{document}

\begin{figure}[!h]
\begin{asy}
import graph;
import contour;
import palette;
defaultpen(fontsize(10pt));
size(14cm,8cm,IgnoreAspect);
file Z=input("dataZ.dat").line();
real[][] z= Z;
z = transpose(z);
pair xyMin=(1,74);
pair xyMax=(3,86);
// original function
real f(real x, real y) {return -2.051^3*1000/(2*3.1415*(2.99*10^2)^2)/(x^2*Cos(y)^2);}
int N=200;
int Levels=16;
defaultpen(1bp);
bounds range=bounds(-3,-0.10);
real[] Cvals=uniform(range.min,range.max,Levels);
// Original
// guide[][] g=contour(f,xyMin,xyMax,Cvals,100,operator --);
// Modified
guide[][] g=contour(z,xyMin,xyMax,Cvals,100,operator --);
for(int i=0;i<g.length-1;++i){
filldraw(g[i][0]--xyMin--(xyMax.x,xyMin.y)--xyMax--cycle,Palette[i],darkblue+0.2bp);
}
xaxis("$x$",BottomTop,1,3,darkblue+0.5bp,RightTicks(Step=0.2,step=0.05),above=true);
yaxis("$y$",LeftRight,74,86,darkblue+0.5bp,LeftTicks(Step=2,step=0.5),above=true);
palette("$f(x,y)$",range,point(SE)+(0.2,0),point(NE)+(0.3,0),Right,Palette,
PaletteTicks("$%+#0.1f$",N=Levels,olive+0.1bp));
\end{asy}
\caption{A contour plot.}
\end{figure}

\end{document}

• What happens with contour(z,xyMin,xyMax,Cvals) only. Since the grid size is related to the size of z, no option 100 is allowed. – O.G. Jul 22 at 21:44
• Hey! In that case I get:  /usr/local/texlive/2018/texmf-dist/asymptote/contour.asy: 463.10: array f must have length >= 2, which I suppose has to do with the fact that I specify a 2D array Z? – WhyCFD Jul 24 at 16:41
• Could you send your data file please? It should be easier to debug... – O.G. Jul 25 at 7:11