import graph; size(250,200,IgnoreAspect); draw(graph(exp,-1,1),red); xaxis("$x$",RightTicks(Label(align=left))); yaxis("$y$",RightTicks);
import graph; size(250,200,IgnoreAspect); draw(graph(exp,-1,1),red); xaxis(Label("$x$",0.75),LeftTicks); yaxis("$y$",RightTicks);
import graph; size(250,200,IgnoreAspect); draw(graph(exp,-1,1),red); xaxis(shift(0,-10)*"$x$",LeftTicks); yaxis("$y$",RightTicks);
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);
import graph; size(300,200,IgnoreAspect); xlimits(-50,50); ylimits(0,100); xaxis("x",Bottom,Courier("m","n"),LeftTicks); yaxis("$y$",Left,RightTicks);
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);
(i) give LeftTicks/RightTicks/Ticks the arguments beginlabel=false and/or endlabel=false;
(ii) explicitly remove specific ticks and their labels (drawing them manually; see http://asymptote.svn.sourceforge.net/viewvc/asymptote/trunk/asymptote/base/graph.asy for the definition of NoZero):
import graph; size(10cm); real f(real x) {return x^2;} draw(graph(f,-2,2)); xaxis(Ticks(NoZero)); yaxis(Ticks(NoZero)); label("$0$",(0,0),SW);(iii) explicitly remove specific tick labels and draw them manually (see http://asymptote.svn.sourceforge.net/viewvc/asymptote/trunk/asymptote/base/graph.asy for the definition of NoZeroFormat):
import graph; size(10cm); real f(real x) {return x^2;} draw(graph(f,-2,2)); xaxis(Ticks(NoZeroFormat)); yaxis(Ticks(NoZeroFormat)); label("$0$",(0,0),SW);(iv) use the xasy GUI to move overlapping labels;
(v) change the Label argument of LeftTicks, RightTicks, or Ticks to:
Label(currentpen+overwrite(Move))Solution (v) 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.
Further examples of graphs are available at http://home.tele2.fr/phivaldi/asymptote/graph/index.html.
i) Specify an explicit unitsize, which overrides any call to
size
:
unitsize(x=1cm,y=2cm);ii) Explicitly tell Asymptote to map the plot region to a specific size:
import graph; real[] x={0,1,2,3}; real[] y=x^2; draw(graph(x,y),red); xaxis("$x$",BottomTop,LeftTicks); yaxis("$y$",LeftRight,RightTicks); size(5cm,5cm,point(SW),point(NE)); label("$f_\mathrm{T}$",point(N),2N);iii) Specify the points in user coordinates that should correspond to a given picture size:
import graph; size(250,200,IgnoreAspect); draw(graph(exp,-1,1),red); xaxis("$x$",BottomTop,LeftTicks); yaxis("$y$",LeftRight,RightTicks); fixedscaling((-1.5,-0.5),(1.5,3.5));In this example, the user coordinate
(-1.5,-0.5)
will end up being the lower left corner of the figure and
(1.5,3.5)
will be the upper right corner. You can use this option to ensure multiple
figures have the same scaling and same resulting figure size (just ensure the
two coordinates given to fixedscaling()
leaves room for any labels).
See also http://asymptote.sourceforge.net/doc/Frames-and-pictures.html.
limits
with the Crop
option before drawing the graph: import graph; size(250,200,IgnoreAspect); draw(graph(exp,-1,1),red); limits((0,0),(1,2),Crop); xaxis("$x$",BottomTop,LeftTicks); yaxis("$y$",LeftRight,RightTicks);See also http://asymptote.sourceforge.net/doc/graph.html.
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); }
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);
size(12cm,0); void distance(picture pic=currentpicture, pair A, pair B, Label L="", real n=0, pen p=currentpen) { real d=3mm; path g=A--B; transform T=shift(-n*d*unit(B-A)*I); pic.add(new void(frame f, transform t) { picture opic; path G=T*t*g; draw(opic,Label(L,Center,UnFill(1)),G,p,Arrows(NoFill),Bars,PenMargins); 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);
Here is a more general solution to the problem of aligning two arbitrary axes. One 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); scale(pic1,false); scale(pic2,false); real xmin1=6; real xmax1=9; real xmin2=8; real xmax2=16; real a1=1; real a2=0.001; real f1(real x) {return a1*sin(x/2*pi);} real f2(real x) {return a2*sin(x/4*pi);} draw(pic1,graph(pic1,f1,xmin1,xmax1)); draw(pic2,graph(pic2,f2,xmin2,xmax2)); xaxis(pic1,Bottom,LeftTicks()); yaxis(pic1,"$f_1(x)$",Left,RightTicks); xaxis(pic2,"$x$",Bottom,LeftTicks(Step=4)); yaxis(pic2,"$f_2(x)$",Left,RightTicks); yequals(pic1,0,Dotted); yequals(pic2,0,Dotted); pair min1=point(pic1,SW); pair max1=point(pic1,NE); pair min2=point(pic2,SW); pair max2=point(pic2,NE); real scale=(max1.x-min1.x)/(max2.x-min2.x); real shift=min1.x/scale-min2.x; transform t1=pic1.calculateTransform(); transform t2=pic2.calculateTransform(); transform T=xscale(scale*t1.xx)*yscale(t2.yy); add(pic1.fit()); real height=framepoint(N).y-framepoint(S).y; add(shift(0,-height)*(shift(shift)*pic2).fit(T));
import graph; size(250,200,IgnoreAspect); scale(Linear,Linear(-1)); draw(graph(log,0.1,10),red); xaxis("$x$",LeftTicks); yaxis("$y$",RightTicks);
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.
y=t x=(t-2)^2+1See the example http://asymptote.sourceforge.net/gallery/2D graphs/parametricgraph.asy.
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).
import graph; size(10cm,0); pair[] z={(0,0),(0.5,0.5),(1,1)}; path g=graph(z); draw(shift(0,.5)*g,marker(scale(5)*unitcircle,FillDraw(white))); xaxis(BottomTop,LeftTicks); yaxis(LeftRight,RightTicks);
Full
, Automatic
or an explicit range Range(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
- 26 October 2008
Extracted from Asymptote Frequently Asked Questions,
Copyright © 2008 .