Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
    
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? –  tohecz 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
1  
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
2  
@microarm15 Please, add the code to your question, rather than in a comment. –  egreg Mar 8 '13 at 13:13
2  
@microarm15 Like this: draw(graph(g, 0, 180, n=500, join=operator ..) –  mafp Mar 8 '13 at 14:20
show 1 more comment

3 Answers

up vote 9 down vote accepted

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

enter image description here

share|improve this answer
1  
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
3  
@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
add comment

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
]
\addplot [thick, red, smooth] {(sin(x)^2)};
\addplot [
    thick,
    smooth,
    line join=round % To avoid overshoots
] {(sin(x)^2)*(cos((x/2)^2))};
\end{axis}
\end{tikzpicture}
\end{document}
share|improve this answer
    
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
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.