[Tex/LaTex] Reversing a path in TikZ

gnuplottikz-pgf

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

Best Answer

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.