1

I want draw over a plotted graph. In this particular case I want to show a projectile path hitting a wall(gray box). I want the blue path stop at the gray wall.

May be the coordinates of the pgfplots and the coordinates tikz uses are not at same scale.

\documentclass[border=3mm]{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{esvect}

\def\FunctionF(#1){tan(30)*(#1)-(9.8*(#1)^2)/(2*(15*cos(30))^2)}%

\begin{document}
    \begin{tikzpicture}

        \begin{axis}[
                xmin=0,
                xmax=20,
                ymin=0,
                ymax=4,
                height=4cm,
                width=20cm,
                hide axis
            ]
            \addplot[domain=0:10, samples=500, blue] {\FunctionF(x)};
            \addplot[domain=10:19.883, samples=500, blue, dotted] {\FunctionF(x)};
        \end{axis}
    \draw (-1,0)--(20,0);
    \draw[->] (0,0)--node[above]{$\vv{v}$}(30:3);
        \draw[fill=black, fill opacity=0.2, black] (10,0) rectangle (10.2,3);
    \end{tikzpicture}
\end{document}

enter image description here

4
  • 1
    No, the coordinates of the tikzpicture are in general not the same as those of the axis. Try to add \pgfplotsset{compat=1.11} to the preamble, and move the \draw-lines before \end{axis}. Jun 26 '20 at 21:24
  • @TorbjørnT. In my case after adding \pgfplotsset{compat=1.11} the document is not finishing building. After about 30 minutes it still is building.
    – Soyeb Jim
    Jun 27 '20 at 9:31
  • 1
    Huh, odd. Don't know why that would happen, and I couldn't even guess without an example demonstrating the behaviour. But nevermind, remove the compat setting then, and instead prefix all the coordinates with axis cs:. That is, instead of \draw (-1,0) ... you have \draw (axis cs:-1,0) ... Jun 27 '20 at 9:34
  • 1
    Sorry, hadn't noticed the polar coordinate, that might muddle things a bit. Jun 27 '20 at 9:44
2

This is probably a duplicate of one or more questions, but anyways. The coordinates of an axis environment is in general not the same as the underlying tikzpicture. To draw stuff in axis units, move the stuff inside the axis environment and either

  • activate compat=1.11 or some higher version or
  • prefix coordinates with axis cs. (i.e. (axis cs:0,0) instead of (0,0))

The latter doesn't work with polar coordinates though, but a workaround for that is to decompose the vector (e.g. drawing lines using polar axis coordinate system in pgfplots).

You said \pgfplotsset{compat=1.11} stopped your document from compiling, so either try activating it for this single axis, by adding it to the options of the environment, or use axis cs. Both are demonstrated below.

\documentclass[tikz,border=3mm]{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{esvect}

\def\FunctionF(#1){tan(30)*(#1)-(9.8*(#1)^2)/(2*(15*cos(30))^2)}%

\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
                compat=1.11, % <-- added
                xmin=0,
                xmax=20,
                ymin=0,
                ymax=4,
                height=4cm,
                width=20cm,
                hide axis
            ]
            \addplot[domain=0:10, samples=50, blue] {\FunctionF(x)};
            \addplot[domain=10:19.883, samples=50, blue, dotted] {\FunctionF(x)};
            
         \draw (-1,0)--(20,0);
         \draw[->] (0,0)--node[above]{$\vv{v}$}(30:3);
         \draw[fill=black, fill opacity=0.2, black] (10,0) rectangle (10.2,3);
        \end{axis}

    \end{tikzpicture}

    \begin{tikzpicture}
        \begin{axis}[
                xmin=0,
                xmax=20,
                ymin=0,
                ymax=4,
                height=4cm,
                width=20cm,
                hide axis
            ]
            \addplot[domain=0:10, samples=50, blue] {\FunctionF(x)};
            \addplot[domain=10:19.883, samples=50, blue, dotted] {\FunctionF(x)};
            
         \draw (axis cs:-1,0) -- (axis cs:20,0);
         % can't use polar coordinates directly with axis cs I think, so calculate x- and y-components instead
         \draw[->] (axis cs:0,0) -- node[above]{$\vv{v}$} (axis cs:{3*cos(30)},{3*sin(30});
         \draw[fill=black, fill opacity=0.2, black] (axis cs:10,0) rectangle (axis cs:10.2,3);
        \end{axis}

    \end{tikzpicture}

\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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