# How to smoothen an Asymptote plot

I tried to plot this function and export it as .eps file but the plot does not look smooth at all

I'm absolute beginner at this tool, Is it a number of samples problem or what?

-
Hi! What program do you use to generate the plot? If it's LaTeX, could you please show the Minimal Example that produces it, please? – yo' Mar 8 '13 at 12:46
My code is: size(8cm,6cm,IgnoreAspect); import graph; real f(real x){return Sin(x)^4;} real g(real x){return (Sin(x)^2)*(Cos((x/2)^2));} xlimits(0, 180); labely(1,2W); yaxis(Label("$y$",position=EndPoint, align=2E),Ticks("%",extend=true), Arrow); xaxis(Label("$x$",position=EndPoint, align=2E), Arrow); draw(graph(f, 0, 180)); draw(graph(g, 0, 180)); labelx(180,2S); – microarm15 Mar 8 '13 at 12:48
If you simply want the graph to look smoother, add the parameter operator .. as the last parameter to the graph command. To set the sample size of a particular graph to--say--50, add the parameter n=50 to a particular graph command. – Charles Staats Mar 8 '13 at 13:00
@microarm15 Please, add the code to your question, rather than in a comment. – egreg Mar 8 '13 at 13:13
@microarm15 Like this: draw(graph(g, 0, 180, n=500, join=operator ..) – mafp Mar 8 '13 at 14:20

You have to increase the sampling rate, which is in the variable ngraph and defaults to 100. Here's the result with 1000:

size(8cm,6cm,IgnoreAspect);
import graph;
ngraph=1000;
real f(real x){return Sin(x)^4;}
real g(real x){return (Sin(x)^2)*(Cos((x/2)^2));}
xlimits(0, 180);
labely(1,2W);
yaxis(Label("$y$",position=EndPoint, align=2E),Ticks("%",extend=true), Arrow);
xaxis(Label("$x$",position=EndPoint, align=2E), Arrow);
draw(graph(f, 0, 180));
draw(graph(g, 0, 180));
labelx(180,2S);


-
And in the interest of teaching a man to fish and all that: here's the online documentation. By specifying the function T you can even make the sampling not evenly spaced, if you know a priori where you need higher "resolution". – Willie Wong Mar 8 '13 at 13:45
@WillieWong Let's say that the documentation is not precisely user friendly. – egreg Mar 8 '13 at 13:50
It still looks ragged to me here. The pdf output is fine, even for the OP's code, but the eps output is ragged. – mafp Mar 8 '13 at 13:52
@mafp: What viewer are you using for the .eps file? Is anti-aliasing enabled? – Jake Mar 8 '13 at 13:55
@Jake ghostview with a-a enabled. Disabling a-a improved the plot considerably. So it is mostly a viewer problem. – mafp Mar 8 '13 at 14:07

This plot needs quite a large number of samples to be represented correctly. I know you're asking for a way to do this in Asymptote, but I'd like to show a way to generate the plot directly within LaTeX using the PGFPlots package:

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
domain=0:180,
samples=300,
axis lines=middle,
xtick={0,180},
enlarge x limits=upper,
enlarge y limits
]
thick,
smooth,
line join=round % To avoid overshoots
] {(sin(x)^2)*(cos((x/2)^2))};
\end{axis}
\end{tikzpicture}
\end{document}

-
sin(x)^2 should be sin(x)^4... – Paul Gaborit Mar 8 '13 at 14:28
@PaulGaborit: I know, but I thought that this version is prettier =) – Jake Mar 8 '13 at 14:34

Apart from the Asymptote specific things (sampling resolution), the problem is heavily influenced by the eps viewer. As already discussed at My .eps images appear to lack resolution, but turn out crisp after compiling my document, what's going on?, line smoothing in the viewer is the culprit. I get the same rags in the eps plot as you with anti-aliasing on in ghostview, turning anti-aliasing off improved the output quite a bit. That being said, the pdf built from your and egreg's code looks much better, as it is rendered by poppler. Also, in print even the eps plot might look smooth.

-