7

I have got a file to plot with pgfplots. For convenience I'd like to define a measure in the plot. Unfortunately the relative positioning seems to give weird results. If I define the node coordinates manually everything works fine. According to the pgfplots-manual the sin- and cos-operators use degrees. Could you please tell me where my error is?

\documentclass{standalone}
\usepackage{pgfplots}
\usetikzlibrary{calc}
\usetikzlibrary{positioning}
\usepackage{siunitx}

\pgfplotsset{compat=1.10}

\begin{document}

\pgfplotsset{cellmodel/.style={%
width=0.4\linewidth,
height=0.3\textheight,
axis lines=center,
axis equal image,
domain=0:800,
xmin= 0, xmax= 7,
ymin=-1.5, ymax=6.5,
restrict x to domain=0:7,
ticks=none,
}}

\begin{tikzpicture}
\def\angle{-30}
\begin{axis}[cellmodel]
\addplot[mark=x, mark options={solid}, draw=gray!80!black, dashed, thick] coordinates { 
        (0.0,0.5)
        (2.25,0.5)
        (4.5,4.397114317029974)
        (6.75,4.397114317029974)
};

% necessary nodes
\node (two) at (axis cs:2.25,0.5) {};
\node (thr) at (axis cs:4.5,4.397114317029974) {};

% relative nodes
% \node (mes2l)  at ($(two)+({cos(\angle)},{sin(\angle)})$) {test};     % <- doesn't give the correct coordinates
% \node (mes3l)  at ({$(thr)+({cos(\angle)},{sin(\angle)})$}) {test};
% \node (mes2l)  at ($(two)+(axis cs:{cos(\angle)},{sin(\angle)})$) {test};     % <- doesn't give the correct coordinates
% \node (mes3l)  at ({$(thr)+(axis cs:{cos(\angle)},{sin(\angle)})$}) {test};
\node (mes2l)  at (axis cs:3.116025404,0) {};                   % <- correct coordinates
\node (mes3l)  at (axis cs:5.366025404,3.897114317) {};
\draw[<->]  (mes2l.center) -- (mes3l.center) node [below right, midway]{$l$};
\end{axis}
\end{tikzpicture}

\end{document}
4
  • Why do you expect that adding a positive quantity sin 30 to 0.5 would result in a y=0? You have \node (two) at (axis cs:2.25,0.5) and \node (mes2l) at ($(two)+(axis cs:{cos(30)},{sin(30)})$). – Peter Grill Apr 3 '14 at 20:07
  • 1
    And where exactly should the new nodes be located? Where exactly are you trying to place those additional nodes? – Gonzalo Medina Apr 4 '14 at 0:45
  • @GonzaloMedina If you compile the code as it is, I think you get the desired result. – Torbjørn T. Apr 4 '14 at 8:41
  • Thanks for your comments. Of course it must be -30°. This must have been switched around during all the trial and error runs. – raedma Apr 4 '14 at 8:55
8

Might be easiest to just move the definition of the mes21 and mes31 nodes outside the axis, then they use the coordinate system of the tikzpicture rather than the axis. (I changed the angle to -30.)

enter image description here

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{calc}
\usetikzlibrary{positioning}
\usepackage{siunitx}

\pgfplotsset{compat=1.10}

\begin{document}

\pgfplotsset{cellmodel/.style={%
width=0.4\linewidth,
height=0.3\textheight,
axis lines=center,
axis equal image,
domain=0:800,
xmin= 0, xmax= 7,
ymin=-1.5, ymax=6.5,
restrict x to domain=0:7,
ticks=none,
}}

\begin{tikzpicture}
\def\angle{-30}
\begin{axis}[cellmodel]
\addplot[mark=x, mark options={solid}, draw=gray!80!black, dashed, thick] coordinates { 
        (0.0,0.5)
        (2.25,0.5)
        (4.5,4.397114317029974)
        (6.75,4.397114317029974)
};

% necessary nodes
\coordinate (two) at (axis cs:2.25,0.5) {};
\coordinate (thr) at (axis cs:4.5,4.397114317029974) {};
\end{axis}

\def\scaling{0.4}
\coordinate (mes2l)  at ($(two)+\scaling*({cos(\angle)},{sin(\angle)})$);
\coordinate (mes3l)  at ($(thr)+\scaling*({cos(\angle)},{sin(\angle)})$);
\draw[<->]  (mes2l) -- (mes3l) node [below right, midway]{$l$};
\end{tikzpicture}

\end{document}
5
  • Thanks a lot. This does what I want for my example. But is there also a way to calculate node positions relative to another node inside the axis environment? One may need this for future use, if the axis scaling is of importance – raedma Apr 4 '14 at 8:58
  • @krtek I don't know of any good ways, but I don't know the details of how this work. You could use something like \coordinate (mes2l) at ($(two)+({400*cos(\angle)},{50*sin(\angle)})$) {}; \coordinate (mes3l) at ($(thr)+({400*cos(\angle)},{50*sin(\angle)})$) {};, but it is not very convenient. – Torbjørn T. Apr 4 '14 at 9:05
  • ok. By the way, thanks for the reminder, that \coordinate is sufficient here instead of \node – raedma Apr 4 '14 at 9:10
  • @krtek Hopefully you'll get a better answer later. – Torbjørn T. Apr 4 '14 at 9:12
  • Well, I accepted your answer anyway, since it solves my problem at the moment :) – raedma Apr 4 '14 at 9:15
2

just small variation on @Torbjørn T. solution:

    \documentclass[border=5mm]{standalone}
        \usepackage{pgfplots}
    \pgfplotsset{compat=newest}
%   \pgfplotsset{compat=1.10}
    \usetikzlibrary{calc,positioning}
        \usepackage{siunitx}

    \pgfplotsset{cellmodel/.style={%
        width=0.4\linewidth,
        height=0.3\textheight,
        axis lines=center,
        axis equal image,
        domain=0:800,
        xmin= 0, xmax= 7,
        ymin=-1.5, ymax=6.5,
        restrict x to domain=0:7,
        ticks=none,
    }}
    \begin{document}
        \begin{tikzpicture}
    \begin{axis}[cellmodel]
    \addplot[mark=x, mark options={solid}, draw=gray!80!black, dashed, thick] 
        coordinates {
            (0.0,0.5)
            (2.25,0.5)
            (4.5,4.397114317029974)
            (6.75,4.397114317029974)
                    };
    % necessary nodes
    \coordinate (two) at (axis cs:2.25,0.5) {};
    \coordinate (thr) at (axis cs:4.5,4.397114317029974) {};
    \end{axis}
    \draw[transform canvas={shift={(-30:0.3)}},<->]  
        (two) to node[right] {$l$}    (thr);
    \end{tikzpicture}
    \end{document}

mybe it will help so

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.