Asymptote FAQ - Section 6
Questions about 2D graphs


Question 6.1. How can I draw x axis ticks on the right side, with the tick labels on the left side (relative to the axis path)?

import graph;

size(250,200,IgnoreAspect);

draw(graph(exp,-1,1),red);

xaxis("$x$",RightTicks(Label(align=left)));
yaxis("$y$",RightTicks);

Question 6.2. How can I reposition the x axis label to three-quarters along the axis length?

import graph;

size(250,200,IgnoreAspect);

draw(graph(exp,-1,1),red);

xaxis(Label("$x$",0.75),LeftTicks);
yaxis("$y$",RightTicks);

Question 6.3. How can I move the x axis label down 10bp?

import graph;
size(250,200,IgnoreAspect);

draw(graph(exp,-1,1),red);

xaxis(shift(0,-10)*"$x$",LeftTicks);
yaxis("$y$",RightTicks);

Question 6.4. Can I use different pens for the axis, the axis label, and the tick labels?

Yes:
import graph;
size(300,200,IgnoreAspect);

xlimits(-50,50); 
ylimits(0,100);

xaxis(Label("$x$",MidPoint,red),Bottom,blue,LeftTicks(green)); 
yaxis("$y$",Left,RightTicks);

Question 6.5. How can I change the font type of the axes label?

import graph;
size(300,200,IgnoreAspect);

xlimits(-50,50); 
ylimits(0,100);

xaxis("x",Bottom,Courier("m","n"),LeftTicks); 
yaxis("$y$",Left,RightTicks);

Question 6.6. How can I change the font type of the tick labels on an axis?

Tick labels are by default typeset in (TeX) math mode, so to use other fonts you need to override the default tick format:
import graph;
size(300,200,IgnoreAspect);

xlimits(-50,50); 
ylimits(0,100);

xaxis("$x$",Bottom,LeftTicks("%.4g",Courier("m","n")+fontsize(12))); 
yaxis("$y$",Left,RightTicks);

Question 6.7. How can I prevent axes tick labels from rendering on top of each other?

Either:

(i) give LeftTicks/RightTicks/Ticks the arguments beginlabel=false and/or endlabel=false;

(ii) use the xasy GUI to move overlapping labels;

(iii) change the Label argument of LeftTicks to:

Label(currentpen+overwrite(Move))
Solution (iii) will move labels that might otherwise overwrite a previous label. Other possible overwrite arguments are Allow (allows overlapping labels; the default), Suppress (an overlapping label will not be written at all), SuppressQuiet, and MoveQuiet. The last two achieve the same result as the non-quiet types, but will not notify you which labels are overlapping. See: http://asymptote.sourceforge.net/doc/Pens.html.

In the case of a user-specified tick array, you can change which labels get suppressed/moved by changing the order of array entries.

Question 6.8. Is it possible to define customized palettes?

Yes, you may generate your own pen[] array. For example:
int NColors=32768;
pen[] MyPalette=new pen[NColors]; 
real step=1/(NColors-1.0); 
// Start at black: rgb(0,0,0) 
// End at yellow: rgb(1,1,0) 
for(int i=0; i < NColors; ++i) { 
  real rgval=i*step; 
  MyPalette[i]=rgb(rgval,rgval,0.0); 
} 

Question 6.9. Is there an easy way to graph factorial functions nicely?

The example below shows a continuous function and two methods for placing markers at integer values of x:
import graph; 
 
size(200,200,IgnoreAspect); 
 
real factorial(real t) {return gamma(t+1);} 
 
scale(Linear,Log); 
 
// Graph the factorial function.
draw(graph(factorial,0,10)); 
 
// Method 1: Draw nodes, but hide line
pair F(int t) {return (t,factorial(t));} 
// Graph of factorial function from 0 to 10
pair[] z=sequence(F,11);
draw(graph(z),invisible,marker(scale(0.8mm)*unitcircle,blue,Fill,Below)); 
 
// Method 2: Nongraphing routines require explicit scaling:
pair dotloc(int t) {return Scale(F(t));} 
pair[] dotlocs=sequence(dotloc,11);
dot(dotlocs); 
 
xaxis("$x$",BottomTop,LeftTicks); 
yaxis("$y$",LeftRight,RightTicks); 

Question 6.10. How do I indicate that a certain length should be exactly the size I prescribe with no rescaling, within a picture which has its own size?

Here's an easy way to do this.
size(12cm,0); 
 
void distance(picture pic=currentpicture, pair A, pair B, Label L="", real n=0,
	      pen p=currentpen) 
{
  real d=3mm;
  guide g=A--B;
  transform T=shift(-n*d*unit(B-A)*I);
  pic.add(new void(frame f, transform t) {
    picture opic;
    guide G=T*t*g;
    draw(opic,G,p,Arrows(NoFill),Bars,PenMargins); 
    label(opic,L,midpoint(G),UnFill(1)); 
    add(f,opic.fit());
  });
  pic.addBox(min(g),max(g),T*min(p),T*max(p));
} 
 
pair A=(0,0), B=(3,3); 
 
dot(A); 
dot(B); 
 
distance(A,B,"$\ell$",1); 

Question 6.11. How can I make the y axis display base-2 logarithmic values?

See the example http://asymptote.sourceforge.net/gallery/2D graphs/log2graph.asy.

Question 6.12. Is there any way to change the default appearance of the decimal separator in axis ticks, using a comma instead of a dot?

Just set your locale appropriately. For example, under the UNIX bash shell:
LC_NUMERIC=it_IT asy -V lineargraph 

Question 6.13. Shouldn't dots always be the same size?

From the documentation:

"The dot command defined in the module plain draws a dot having a diameter equal to an explicit pen linewidth or the default linewidth magnified by dotfactor (6 by default)."

Thus, when you use the default pen, the dot will have size 6*linewidth, but when you give a pen with an explicit width specified, you will have a dot of size linewidth. If you want the first case to behave like the second, you may set dotfactor=1.

Question 6.14. How can I align the x axis of two graphs on the same figure?

Here is solution that fits the second picture to a frame based on the horizontal scaling for the first picture:
import graph;  
 
real width=15cm; 
real aspect=0.3; 
 
picture pic1,pic2; 
 
size(pic1, width, aspect * width, IgnoreAspect); 
size(pic2, width, aspect * width, IgnoreAspect); 
 
real xmin=0; 
real xmax=2*pi; 
 
real a1=1; 
real a2=0.001; 
 
real f1(real x) {return a1*sin(x);} 
real f2(real x) {return a2*sin(x);} 
 
draw(pic1,graph(pic1,f1,xmin,xmax)); 
draw(pic2,graph(pic2,f2,xmin,xmax)); 
 
xaxis(pic1,Bottom,LeftTicks); 
xaxis(pic2,"$x$",Bottom,LeftTicks); 
yaxis(pic1,"$f_1(x)$",Left,RightTicks); 
yaxis(pic2,"$f_2(x)$",Left,RightTicks); 
 
yequals(pic1,0,dotted); 
yequals(pic2,0,dotted); 
 
add(pic1.fit(),(0,0),NW); 
transform t1=pic1.calculateTransform(); 
 
transform t2=pic2.calculateTransform(); 
add(pic2.fit(xscale(t1.xx)*yscale(t2.yy)),(0,0),SW); 

Question 6.15. How can I change the direction of the y-axis, such that negatives values are on the upper y-axis?

Here is a simple example (see also the example http://asymptote.sourceforge.net/gallery/2D graphs/diatom.asy or the discussion of Linear(-1) in the documentation):
import graph;
size(250,200,IgnoreAspect);

scale(Linear,Linear(-1)); 

draw(graph(log,0.1,10),red);

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

Question 6.16. How can I fill a path with a function that defines the color of each location?

General function shading is only supported by PDF, not PostScript, so this will have to wait until we produce PDF code directly.

An approximate solution for now would be to superimpose a fine grid and specify colors to latticeshade depending on position as a single pen[][] lattice. Or use Gouraud shading.

Question 6.17. Is there a way to draw a function that is not explicitly given, such as (y - 2)^2 = x - 1 ?

Yes, use the parametric form
y=t 
x=(t-2)^2+1 
See the example http://asymptote.sourceforge.net/gallery/2D graphs/parametricgraph.asy.

Question 6.18. Is it possible to reverse or stretch an axis?

The real scaling argument to Linear is used to stretch (or reverse) the axis. To see the effect of axis stretching, be sure not to specify IgnoreAspect in the picture size command.

A secondary axis has the same length as the primary axis, so stretching cannot have any effect. But one can still reverse the axis, with Linear(-1).

Question 6.19. Why can't I use the UnFill option to draw graphs with empty markers?

UnFill won't work here because it only affects the local frame the markers are initially drawn on, before being added to currentpicture. Here is a way of achieving the desired effect:
import graph; 
size(10cm,0); 
pair[] z={(0,0),(0.5,0.5),(1,1)}; 
guide g=graph(z); 
 
draw(shift(0,.5)*g,marker(scale(5)*unitcircle,FillDraw(white))); 
 
xaxis(BottomTop,LeftTicks); 
yaxis(LeftRight,RightTicks); 

Question 6.20. How can I force several images to use the same palette range (e.g. the entire 0-255 grayscale range)?

The palette color space corresponds to a range of values specified by the argument range, which can be Full, Automatic or an explicit bounds(pair min, pair max). Here Full specifies a range varying from the minimum to maximum values of the function over the sampling interval, while Automatic selects "nice" limits.
Next: Questions about programming.
Back: Questions about arrows.
Return to contents.

Asymptote - 01 November 2006

Extracted from Asymptote Frequently Asked Questions, Copyright © 2006 .