5

I'm trying to draw vectors normal to curved surface using pgfplots. Here's my MWE and result:

\documentclass[tikz,margin=5pt]{standalone}

\usetikzlibrary{decorations.markings}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\tikzset{test/.style={
    postaction={decorate},
    decoration={
        markings,
        mark=between positions 0.0 and 0.1 step 0.01
        with { \draw[-latex] (0,0) -- (0,-5); }
        }
    }
}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
    xmin=0, ymin=0, xmax=50, ymax=50,
    unit vector ratio*=1 1 1]
\addplot[smooth cycle, test] coordinates {
    (0.0, 0.0)
    (20.0, 15.0)
    (40.0, 30.0)
    (0.0, 40.0)};
\end{axis};

\end{tikzpicture}

\end{document}

enter image description here

If I however change axis limits, for example setting

\begin{axis}[
    xmin=-20, ymin=0, xmax=50, ymax=50,
    unit vector ratio*=1 1 1]

I get the following output:

enter image description here

I also get similar behavior by setting some data coordinates to negative values, for example

\addplot[smooth cycle, test] coordinates {
    (0.0, 0.0)
    (20.0, 15.0)
    (40.0, 30.0)
    (-20.0, 40.0)};

The question is how to properly use \draw to decorate inside pgfplots axis environment?

0

1 Answer 1

3

The coordinates in the decoration settings need a relative coordinate system, axis direction cs (or rel axis cs). The default coordinate system are absolute coordinates with axis cs.

\draw[-latex] (axis direction cs:0,0) -- (axis direction cs:0,-5);

Full example:

\documentclass[tikz,margin=5pt]{standalone}

\usetikzlibrary{decorations.markings}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\tikzset{test/.style={
    postaction={decorate},
    decoration={
        markings,
        mark=between positions 0.0 and 0.1 step 0.01
        with {
          \draw[-latex] (axis direction cs:0,0) -- (axis direction cs:0,-5);
        }
    }
}}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
    xmin=-20, ymin=-5, xmax=50, ymax=50,
    % xmin=0, ymin=0, xmax=50, ymax=50,
    unit vector ratio*=1 1 1]
\addplot[smooth cycle, test] coordinates {
    (0.0, 0.0)
    (20.0, 15.0)
    (40.0, 30.0)
    (0.0, 40.0)};
\end{axis};

\end{tikzpicture}
\end{document}

Result

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .