0

I try to add some draw objects to pgfplot (gnuplot), MWE is as follows:

\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}
\pgfplotsset{compat=newest}

\begin{tikzpicture}
% define lengths and coordinates
\def\xinit{0}
\def\yinit{-2.5}
\def\hgt{0.5}
\def\wa{1.}
\def\wb{0.5}
\def\wc{1}
\begin{axis}[
xlabel={x},
ylabel={y},
restrict x to domain=0:2*pi,
no markers,
clip = false,
]

\addplot gnuplot [raw gnuplot, very thick] {set xrange [0:2*pi];
    set samples 1000;
plot sin(x^2) w l};
\draw (\xinit,\yinit) rectangle ++(\wa, \hgt) coordinate (a1);
\fill[red] (\xinit,\yinit) rectangle (\xinit +\wa,\yinit +\hgt);
\draw (a1) -- +(2,0);
\end{axis}
\end{tikzpicture}

\end{document}

I don't understand why the first rectangle is drawn wrong, it should be the same as the second one (red). Next, I want to sequentially draw several objects with known length. The last command shows my attempt to firstly define coordinate (a1), then use this coordinate as initial for the next object: that should be a horizontal line from (a1) of length 2 (actually from (a1) + (0,-\hgt /2) -- the middle of the right side of red rectangle). So, if I explicitly set coordinates this works, but + or ++ method doesn't.

Thank you.

1

You have to use axis direction cs (in addition to + or ++) to make it work. The PGFPlots manual states in section 4.17.1 on page 349 (v1.14):

While axis cs allows to supply absolute positions, axis direction cs supplies directions. It allows to express relative positions, including lengths and dimensions, by means of axis coordinates.

As noted in the documentation for axis cs, adding two coordinates by means of the TikZ ++ operator may have unexpected eff ects. The correct way for ++ operations is axis direction cs: [...]

% used PGFPlots v1.14
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    \usetikzlibrary{calc}
\begin{document}
    \pgfplotsset{compat=newest}
\begin{tikzpicture}
        % define lengths and coordinates
        \def\xinit{0}
        \def\yinit{-2.5}
        \def\hgt{0.5}
        \def\wa{1.}
        \def\wb{0.5}
        \def\wc{1}
    \begin{axis}[
        xlabel={x},
        ylabel={y},
        restrict x to domain=0:2*pi,
        no markers,
        clip = false,
    ]
        \addplot gnuplot [raw gnuplot, very thick] {
            set xrange [0:2*pi];
            set samples 1000;
            plot sin(x^2) w l;
        };
        % this doesn't work (as expected), ...
        \draw (\xinit,\yinit) rectangle ++(\wa, \hgt) coordinate (a1);
        % ... because you have to use `axis direction cs:', too.
        \draw [ultra thick,thick,blue]
            (\xinit,\yinit) rectangle ++(axis direction cs:\wa, \hgt)
                coordinate (a1);

        \fill [red] (\xinit,\yinit) rectangle (\xinit +\wa,\yinit +\hgt);

        \draw (a1) -- +(2,0);
        % the same here
        \draw [very thick,green] (a1) -- +(axis direction cs:2,0);
    \end{axis}
\end{tikzpicture}
\end{document}

image showing the zoomed result of above code

  • You are welcome. When you find my answer useful you could -- besides accepting the answer -- upvote the answer too by pressing the upward pointing arrow to the left of the answer. – Stefan Pinnow Mar 16 '17 at 10:03

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.