Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I wish to add a legend to my two plots - red and black, but the legend seems to have some scaling issues. I think it may be due to the changes made in shifting and domains, but I am not sure!

\newcommand{\vasymptote}[2][]{
\draw [color=gray,densely dashed,#1] ({rel axis cs:0,0} -| {axis cs:#2,0}) -- ({rel axis cs:0,1} -| {axis cs:#2,0});
}

\begin{tikzpicture}
    \begin{axis}[
        xlabel={Distance (m)},
        ylabel={Moment (kNm)}, xmin=0,xmax=60,width=29.7cm,height=21cm, title={Moment Envelopes}, grid=both, y dir=reverse, ytick={-5500,-5000,...,1500},]

  % Axis
    \addplot[gray,thick,domain=0:60,shift={(0,0)},forget plot] {0}; %x axis
   \vasymptote {7.5};
   \vasymptote {22.5};
   \vasymptote {37.5};
   \vasymptote {52.5};

    % 1.2G + 1.5 Q
  \addplot[black,smooth,domain=0:7.5,shift={(0,0)}, line legend] {-46.61*x^2 - 349.6*x}; % A
    \addplot[black,smooth,domain=0:15,shift={(75,0)}, forget plot] {-54.22*x^2 + 829.29*x - 2206}; % B
  \addplot[black,smooth,domain=0:15,shift={(225,0)}, forget plot] {-54.22*x^2 + 813.22*x - 2020}; % C
  \addplot[black,smooth,domain=0:15,shift={(375,0)}, forget plot] {-54.22*x^2 + 797.16*x - 1965}; % D
  \addplot[black,smooth,domain=0:7.5,shift={(525,0)}, forget plot] {-46.61*(7.5-x)^2 - 349.6*(7.5-x)}; % E

    \addlegendentry{Black Line}

    % G + 0.3 Q
    \addplot[red,smooth,domain=0:7.5,shift={(0,0)}, line legend] {-29.36*x^2 - 220.25*x}; % A
    \addplot[red,smooth,domain=0:15,shift={(75,0)}, forget plot] {-36.6*x^2 + 559.77*x - 1489.05}; % B
    \addplot[red,smooth,domain=0:15,shift={(225,0)}, forget plot] {-36.6*x^2 + 548.92*x - 1363.5}; % C
    \addplot[red,smooth,domain=0:15,shift={(375,0)}, forget plot] {-36.6*x^2 + 538.68*x - 1326.38}; % D
    \addplot[red,smooth,domain=0:7.5,shift={(525,0)}, forget plot] {-29.36*(7.5-x)^2 - 220.25*(7.5-x)}; % E

     \addlegendentry{Red Line}

\end{axis}
\end{tikzpicture}

Uninverting the y-axis leads to a reduction in the size of the legend, but does not fix the problem.

Any help would be great, thanks!

share|improve this question
1  
Welcome to TeX.SX! You may have a look on our starter guide. –  texenthusiast May 23 '13 at 13:48

1 Answer 1

up vote 4 down vote accepted

Yes, this is caused by the shift commands. You shouldn't use the normal TikZ transformation commands (like shift or scale) to alter a plot, because they operate in the "paper domain", while you should be working in the "data domain". So if you want to shift a plot by 7.5 "data units" to the right, you can either alter your function (\addplot [...] { -36.6*(x-7.5)^2 + 559.77*(x-7.5) - 1489.05 };), or you can use a coordinate filter:

x filter/.code=\pgfmathparse{\pgfmathresult+7.5}

will add 7.5 to all x coordinates in the plot.

\documentclass{standalone}
\usepackage{pgfplots}

\begin{document}
\newcommand{\vasymptote}[2][]{
\draw [color=gray,densely dashed,#1] ({rel axis cs:0,0} -| {axis cs:#2,0}) -- ({rel axis cs:0,1} -| {axis cs:#2,0});
}

\begin{tikzpicture}
    \begin{axis}[
        xlabel={Distance (m)},
        ylabel={Moment (kNm)}, xmin=0,xmax=60,width=29.7cm,height=21cm, title={Moment Envelopes}, grid=both, y dir=reverse, ytick={-5500,-5000,...,1500},]

  % Axis
    \addplot[gray,thick,domain=0:60,shift={(0,0)},forget plot] {0}; %x axis
   \vasymptote {7.5};
   \vasymptote {22.5};
   \vasymptote {37.5};
   \vasymptote {52.5};

    % 1.2G + 1.5 Q
  \addplot[black,smooth,domain=0:7.5, line legend] {-46.61*x^2 - 349.6*x}; % A
    \addplot[black,smooth,domain=0:15, x filter/.code=\pgfmathparse{\pgfmathresult+7.5}, forget plot] {-54.22*x^2 + 829.29*x - 2206}; % B
  \addplot[black,smooth,domain=0:15, x filter/.code=\pgfmathparse{\pgfmathresult+22.5}, forget plot] {-54.22*x^2 + 813.22*x - 2020}; % C
  \addplot[black,smooth,domain=0:15, x filter/.code=\pgfmathparse{\pgfmathresult+37.5},, forget plot] {-54.22*x^2 + 797.16*x - 1965}; % D
  \addplot[black,smooth,domain=0:7.5,x filter/.code=\pgfmathparse{\pgfmathresult+52.5}, forget plot] {-46.61*(7.5-x)^2 - 349.6*(7.5-x)}; % E

    \addlegendentry{Black Line}

    % G + 0.3 Q
    \addplot[red,smooth,domain=0:7.5, line legend] {-29.36*x^2 - 220.25*x}; % A
    \addplot[red,smooth,domain=0:15, x filter/.code=\pgfmathparse{\pgfmathresult+7.5}, forget plot] {-36.6*x^2 + 559.77*x - 1489.05}; % B
    \addplot[red,smooth,domain=0:15, x filter/.code=\pgfmathparse{\pgfmathresult+22.5}, forget plot] {-36.6*x^2 + 548.92*x - 1363.5}; % C
    \addplot[red,smooth,domain=0:15, x filter/.code=\pgfmathparse{\pgfmathresult+37.5}, forget plot] {-36.6*x^2 + 538.68*x - 1326.38}; % D
    \addplot[red,smooth,domain=0:7.5, x filter/.code=\pgfmathparse{\pgfmathresult+52.5}, forget plot] {-29.36*(7.5-x)^2 - 220.25*(7.5-x)}; % E

     \addlegendentry{Red Line}

\end{axis}
\end{tikzpicture}
\end{document}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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