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.

When I use \path plot function {x^2}; in tikz, it plots it left-to-right (the default domain is -5:5). This is annoying, because, for example, one cannot pile on -- plot function {x^2 + 1} -- cycle; and then fill the whole thing; it doesn't fill the obvious cycle, but rather the self-intersecting one going between opposite corners as it "returns carriage", so to speak.

Is there some way to get plot function to generate points in the reverse order? Alternatively, is there some way of getting tikz to draw a path in the reverse order the points were specified? (This last one is of more general utility, since the orientation of a path is used a lot.)

share|improve this question
    
You can specify the domain as 5:-5? –  Peter Grill Oct 7 '11 at 0:53
    
@Peter: sadly, it doesn't help. –  Ryan Reich Oct 7 '11 at 0:55
add comment

3 Answers

up vote 6 down vote accepted

If you use PGFplots for plotting your functions, the paths will be reversed and connected appropriately by using stack plots=y in the axis options and issuing \closedcycle after the second plot:

\documentclass{article}

\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
    stack plots=y,
    hide axis
]
\addplot [draw=orange,thick]{x^2};
\addplot [draw=orange, thick, fill=yellow] {1} \closedcycle;
\end{axis}
\end{tikzpicture}

\end{document}

If all you want is the filled path, you can disable the axes by using hide axis.

share|improve this answer
    
I really should be using pgfplots, eh? Does it allow me to outsource to gnuplot and, if so, does \closedcycle work as expected? –  Ryan Reich Oct 10 '11 at 6:51
    
It sure does! Just use \addplot gnuplot {x**2}; instead of \addplot {x^2};, and the calculations will be done by gnuplot. The \closedcycle works exactly as before. –  Jake Oct 10 '11 at 7:04
    
Well, then it looks like you've answered my question! –  Ryan Reich Oct 10 '11 at 7:41
add comment

With the current implementation of plot function, I don't think it is possible since pgf reads sequencially the file output by gnuplot. Since gnuplot computes values of the function from the lowest value of x to the highest value, the reverse order is not possible (see file pgfmoduleplot.code.tex) (I may have missed a gnuplot option to do so).

However, you can achieve what you want with the pgf math engine like in the following, reversing the bounds of the domain.

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[scale=.25]
  \fill plot (\x,\x^2) -- plot[domain=5:-5] (\x,\x^2 + 1) -- cycle;
\end{tikzpicture}
\end{document}

Note: this is my first contrib to tex.se. Hope it is formatted ok.

share|improve this answer
    
It gives an x^3 plot when implemented :) Your format is A-OK by the way. –  percusse Oct 7 '11 at 8:33
3  
@percusse: It's actually a x*abs(x) plot. That's a bug in TikZ that's been around for a long time. Hendrik Vogt posted a fix at tex.stackexchange.com/questions/5400/… –  Jake Oct 7 '11 at 8:42
    
@Jake : Yay, it's good to know. Thanks. –  percusse Oct 7 '11 at 8:50
    
It's good to know that reversing the domain works when tikz is in charge, but there are good reasons to use gnuplot, e.g. for computationally expensive functions. –  Ryan Reich Oct 10 '11 at 6:50
add comment

Actually you can force Gnuplot compute the path in the order you want. The idea is to use Gnuplot's parametric curve representation.

In the following example (in the %% Good section, the %% Bad section is just here to show what happens if the trick is not used), I draw the first curve normally, and the second one using the trick. Each value of the parameter t which lives between 0 and 1 is sent to the desired interval (here [2,-1] in that order) using the barycentric formula : x = (1-t) *2 + t *(-1)

More generally, if you want to send [0,1] on to [a,b] starting with b you just use (1-t)*b + t*a.

\documentclass{article}
\usepackage{tikz}
\begin{document}

%% Requires Gnuplot !

%% Functions definition
\newcommand{\funcf}[1]{(#1)**2)}   % it is wise to always put the #1 argument between ()
\newcommand{\funcg}[1]{1+(#1)**2)}

%% Bad 
\begin{tikzpicture}
    \fill
        plot[domain=-1:2, id=funcf] function {\funcf{x}}
        --  
        plot[domain=2:-1, id=funcgbad] function {\funcg{x}} % inverting boundaries does *NOT* work with Gnuplot
        -- cycle;
\end{tikzpicture}


%% Good 
\begin{tikzpicture}
    \fill
        plot[domain=-1:2, id=funcf] function {\funcf{x}}
        --  
        plot[domain=0:1, parametric, id=funcggood] function {(1-t)*2+t*(-1),\funcg{(1-t)*2+t*(-1)}} 
        -- cycle;
\end{tikzpicture}

\end{document}
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.