<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"> <html> <head><title> Asymptote FAQ - Section 6 </title> <link rev="made" href="mailto:"> <link rel="Contents" href="index.html"> <link rel="Start" href="index.html"> <link rel="Next" href="section7.html"><link rel="Previous" href="section5.html"><link rel="Bookmark" title="Asymptote FAQ" href="index.html"> </head><body text="#000000" bgcolor="#FFFFFF"><h1> Asymptote FAQ - Section 6 <br> Questions about 2D graphs </h1> <ul> <li><a href="#axisticks" rel=subdocument>Q6.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)?</a> <li><a href="#axislabel" rel=subdocument>Q6.2. How can I reposition the x axis label to three-quarters along the axis length?</a> <li><a href="#axislabeldown" rel=subdocument>Q6.3. How can I move the x axis label down 10bp?</a> <li><a href="#threeaxispens" rel=subdocument>Q6.4. Can I use different pens for the axis, the axis label, and the tick labels?</a> <li><a href="#axislabelfont" rel=subdocument>Q6.5. How can I change the font type of the axes label?</a> <li><a href="#axisticklabelfont" rel=subdocument>Q6.6. How can I change the font type of the tick labels on an axis?</a> <li><a href="#overlappingticklabels" rel=subdocument>Q6.7. How can I prevent axes tick labels from rendering on top of each other?</a> <li><a href="#fixedsizegraphs" rel=subdocument>Q6.8. How do I make the plot region of a graph, ignoring labels and legends, have a fixed size?</a> <li><a href="#graphlimits" rel=subdocument>Q6.9. How can I plot a function f(x) within [0,1]x[0,2] without explicitly calculating the x values for which f(x) hits the boundary?</a> <li><a href="#custompalettes" rel=subdocument>Q6.10. Is it possible to define customized palettes?</a> <li><a href="#factorial" rel=subdocument>Q6.11. Is there an easy way to graph factorial functions nicely?</a> <li><a href="#length" rel=subdocument>Q6.12. 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?</a> <li><a href="#log2" rel=subdocument>Q6.13. How can I make the y axis display base-2 logarithmic values?</a> <li><a href="#align" rel=subdocument>Q6.14. How can I align the x axes of two graphs on the same figure?</a> <li><a href="#changeaxis" rel=subdocument>Q6.15. How can I change the direction of the y-axis, such that negatives values are on the upper y-axis?</a> <li><a href="#functioncolor" rel=subdocument>Q6.16. How can I fill a path with a function that defines the color of each location?</a> <li><a href="#nonexplicitfun" rel=subdocument>Q6.17. Is there a way to draw a function that is not explicitly given, such as (y - 2)^2 = x - 1 ?</a> <li><a href="#scalesecondaryaxis" rel=subdocument>Q6.18. Is it possible to reverse or stretch an axis?</a> <li><a href="#emptymarkers" rel=subdocument>Q6.19. Why can't I use the UnFill option to draw graphs with empty markers?</a> <li><a href="#paletterange" rel=subdocument>Q6.20. How can I force several images to use the same palette range (e.g. the entire 0-255 grayscale range)?</a> </ul><hr> <h2><A name="axisticks"> 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)? </A></h2> <pre> import graph; size(250,200,IgnoreAspect); draw(graph(exp,-1,1),red); xaxis("$x$",RightTicks(Label(align=left))); yaxis("$y$",RightTicks); </pre> <h2><A name="axislabel"> Question 6.2. How can I reposition the x axis label to three-quarters along the axis length? </A></h2> <pre> import graph; size(250,200,IgnoreAspect); draw(graph(exp,-1,1),red); xaxis(Label("$x$",0.75),LeftTicks); yaxis("$y$",RightTicks); </pre> <h2><A name="axislabeldown"> Question 6.3. How can I move the x axis label down 10bp? </A></h2> <pre> import graph; size(250,200,IgnoreAspect); draw(graph(exp,-1,1),red); xaxis(shift(0,-10)*"$x$",LeftTicks); yaxis("$y$",RightTicks); </pre> <h2><A name="threeaxispens"> Question 6.4. Can I use different pens for the axis, the axis label, and the tick labels? </A></h2> Yes: <pre> 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); </pre> <h2><A name="axislabelfont"> Question 6.5. How can I change the font type of the axes label? </A></h2> <pre> import graph; size(300,200,IgnoreAspect); xlimits(-50,50); ylimits(0,100); xaxis("x",Bottom,Courier("m","n"),LeftTicks); yaxis("$y$",Left,RightTicks); </pre> <h2><A name="axisticklabelfont"> Question 6.6. How can I change the font type of the tick labels on an axis? </A></h2> Tick labels are by default typeset in (TeX) math mode, so to use other fonts you need to override the default tick format: <pre> 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); </pre> <h2><A name="overlappingticklabels"> Question 6.7. How can I prevent axes tick labels from rendering on top of each other? </A></h2> Either: <p> (i) give LeftTicks/RightTicks/Ticks the arguments beginlabel=false and/or endlabel=false; <p> (ii) explicitly remove specific ticks and their labels (drawing them manually; see <A href="http://www.github.com/vectorgraphics/asymptote/base/graph.asy">http://www.github.com/vectorgraphics/asymptote/base/graph.asy</A> for the definition of NoZero): <pre> 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); </pre> (iii) explicitly remove specific tick labels and draw them manually (see <A href="http://www.github.com/vectorgraphics/asymptote/base/graph.asy">http://www.github.com/vectorgraphics/asymptote/base/graph.asy</A> for the definition of NoZeroFormat): <pre> 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); </pre> (iv) use the xasy GUI to move overlapping labels; <p> (v) change the Label argument of LeftTicks, RightTicks, or Ticks to: <pre> Label(currentpen+overwrite(Move)) </pre> 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: <A href="http://asymptote.sourceforge.net/doc/Pens.html">http://asymptote.sourceforge.net/doc/Pens.html</A>. <p> In the case of a user-specified tick array, you can change which labels get suppressed/moved by changing the order of array entries. <h2><A name="fixedsizegraphs"> Question 6.8. How do I make the plot region of a graph, ignoring labels and legends, have a fixed size? </A></h2> Either: <p> i) Specify an explicit unitsize, which overrides any call to <code>size</code>: <pre> unitsize(x=1cm,y=2cm); </pre> ii) Explicitly tell Asymptote to map the plot region to a specific size: <pre> 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); </pre> iii) Specify the points in user coordinates that should correspond to a given picture size: <pre> 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)); </pre> In this example, the user coordinate <code>(-1.5,-0.5)</code> will end up being the lower left corner of the figure and <code>(1.5,3.5)</code> 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 <code>fixedscaling()</code> leaves room for any labels). <p> See also <A href="http://asymptote.sourceforge.net/doc/Frames-and-pictures.html">http://asymptote.sourceforge.net/doc/Frames-and-pictures.html</A>. <h2><A name="graphlimits"> Question 6.9. How can I plot a function f(x) within [0,1]x[0,2] without explicitly calculating the x values for which f(x) hits the boundary? </A></h2> Call <code>limits</code> with the <code>Crop</code> option before drawing the graph: <pre> 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); </pre> See also <A href="http://asymptote.sourceforge.net/doc/graph.html">http://asymptote.sourceforge.net/doc/graph.html</A>. <h2><A name="custompalettes"> Question 6.10. Is it possible to define customized palettes? </A></h2> Yes, you may generate your own pen[] array. For example: <pre> 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); } </pre> <h2><A name="factorial"> Question 6.11. Is there an easy way to graph factorial functions nicely? </A></h2> The example below shows a continuous function and two methods for placing markers at integer values of x: <pre> 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)); // 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); </pre> <h2><A name="length"> Question 6.12. 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? </A></h2> Here's an easy way to do this. <pre> 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); </pre> <h2><A name="log2"> Question 6.13. How can I make the y axis display base-2 logarithmic values? </A></h2> See the example <A href="http://asymptote.sourceforge.net/gallery/2D graphs/log2graph.asy">http://asymptote.sourceforge.net/gallery/2D graphs/log2graph.asy</A>. <h2><A name="align"> Question 6.14. How can I align the x axes of two graphs on the same figure? </A></h2> An easy way to do this, if the axes to be aligned have the same scaling and size, is illustrated in the example <A href="http://asymptote.sourceforge.net/gallery/2D graphs/alignedaxis.asy">http://asymptote.sourceforge.net/gallery/2D graphs/alignedaxis.asy</A>. <p> 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: <pre> 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=truepoint(N,user=false).y-truepoint(S,user=false).y; add(shift(0,-height)*(shift(shift)*pic2).fit(T)); </pre> <h2><A name="changeaxis"> Question 6.15. How can I change the direction of the y-axis, such that negatives values are on the upper y-axis? </A></h2> Here is a simple example (see also the example <A href="http://asymptote.sourceforge.net/gallery/2D graphs/diatom.asy">http://asymptote.sourceforge.net/gallery/2D graphs/diatom.asy</A> or the discussion of Linear(-1) in the documentation): <pre> import graph; size(250,200,IgnoreAspect); scale(Linear,Linear(-1)); draw(graph(log,0.1,10),red); xaxis("$x$",LeftTicks); yaxis("$y$",RightTicks); </pre> <h2><A name="functioncolor"> Question 6.16. How can I fill a path with a function that defines the color of each location? </A></h2> Use <code>functionshade</code> with a PDF tex engine, as illustrated by the example {functionshading.asy}. <p> If you want to produce PostScript output, an approximate solution for now would be to superimpose a fine grid and specify colors to <code>latticeshade</code> that depend on position as a single pen[][] lattice. Alternatively, it may be more efficient to use <code>tensorshade}. <h2><A name="nonexplicitfun"> Question 6.17. Is there a way to draw a function that is not explicitly given, such as (y - 2)^2 = x - 1 ? </A></h2> Yes, use the parametric form <pre> y=t x=(t-2)^2+1 </pre> See the example <A href="http://asymptote.sourceforge.net/gallery/2D graphs/parametricgraph.asy">http://asymptote.sourceforge.net/gallery/2D graphs/parametricgraph.asy</A>. <h2><A name="scalesecondaryaxis"> Question 6.18. Is it possible to reverse or stretch an axis? </A></h2> 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. <p> 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). <h2><A name="emptymarkers"> Question 6.19. Why can't I use the UnFill option to draw graphs with empty markers? </A></h2> 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 (assuming a white background): <pre> 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); </pre> <h2><A name="paletterange"> Question 6.20. How can I force several images to use the same palette range (e.g. the entire 0-255 grayscale range)? </A></h2> The palette color space corresponds to a range of values specified by the argument range, which can be <code>Full</code>, <code>Automatic</code> or an explicit range <code>Range(pair min, pair max)</code>. Here <code>Full} specifies a range varying from the minimum to maximum values of the function over the sampling interval, while <code>Automatic</code> selects "nice" limits. <hr> Next: <a href="section7.html" rel=precedes>Questions about programming</a>.<br> Back: <a href="section5.html" rev=precedes>Questions about arrows</a>.<br> <a href="index.html" rev=subdocument>Return to contents</a>.<p> <address> <A href="http://asymptote.sourceforge.net">Asymptote</A> - 21 April 2019 </address><br> Extracted from Asymptote Frequently Asked Questions, Copyright © 2019 . </body></html>