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):


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)

pen[] Palette=BWRainbow();

picture bar;

bounds range=image(v,(0,0),(1,1),Palette);

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:


import graph;
import contour;
import palette;
// Load my height function:
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;
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 --);
pen[] Palette=Gradient(Levels,rgb(0.3,0.06,0.5),rgb(0.9,0.9,0.85));
for(int i=0;i<g.length-1;++i){
\caption{A contour plot.}

  • 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
  • 1
    Could you send your data file please? It should be easier to debug... – O.G. Jul 25 at 7:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.