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.

Using the following example code:

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{document}
\begin{tikzpicture}
        \begin{axis}[
                xlabel=$x$,
                ylabel={$f(x) = x^2 - x +4$}
        ]
        % use TeX as calculator:
        \addplot {x^2 - x +4};
        \addlegendentry{First plot}
        \addplot [xshift=1cm]{x^2 - x +4};
        \addlegendentry{Second plot}
        \end{axis}
\end{tikzpicture}
\end{document}

Creates this chart with the strange legend:

Broken legend

I guess this is due to the xshift property for the plot, but how can I revert this?

share|improve this question
add comment

1 Answer

up vote 4 down vote accepted

You can revert this shift by shifting the "legend image" (i.e. the small plot extract that is drawn in the legend) back by the same distance. To do this, set the option legend image post style={xshift=-1cm} as an option to \addplot.

It should be pointed out that using xshift is not really the "proper" way to shift a plot, because it works on the wrong level (you just shift the path, not the "logical" plot). You should instead be adjusting the function: In this case, you should use (x-1)^2 - (x-1) +4 instead of x^2 - x +4.

Another option is to use x filter/.code=\pgfmathparse{\pgfmathresult+1} to manipulate the x-coordinate. This can come in handy if you want to shift the plot without changing the plot expression.

The code below shows all three approaches, also demonstrating the problem with the xshift approach:

shifting things with PGFplots

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{document}
\begin{tikzpicture}
        \begin{axis}[
                xlabel=$x$,
                ylabel={$f(x) = x^2 - x +4$}
        ]
        % use TeX as calculator:
        \addplot {x^2 - x +4};  \addlegendentry{First plot}
        \addplot +[xshift=1cm,legend image post style={xshift=-1cm}]{x^2 - x +4};  \addlegendentry{Second plot}
        \addplot {(x+1)^2 - (x+1) +4};  \addlegendentry[]{Third plot}
        \addplot +[x filter/.code=\pgfmathparse{\pgfmathresult-2}] {(x+1)^2 - (x+1) +4};  \addlegendentry{Fourth plot}
        \end{axis}
\end{tikzpicture}
\end{document}
share|improve this answer
    
Hi Jake, thanks for your answer! Your right, for this example you should change the formula, but it was just a small example I adapted to make a clear example. For my use case it's a small shift to plot 3 data sets (marks only) in the same chart. –  Davy Landman Jul 22 '11 at 12:25
    
Ah yes, that makes sense! I've added a different way of shifting the plot that might be useful for your case –  Jake Jul 22 '11 at 12:38
add comment

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.