4

I need to shift several curves in a plot but in a way that is independent of the scale of the data. For example I need to shift each curve by a fifth or so of the scale of plot.

Suppose one has to plot several curves that are very similar:

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot+[
] coordinates{
 ( 1, 2 )
 ( 2, 3 )
 ( 3, 4 )
};
\addplot+[
] coordinates{
 ( 1, 2.1 )
 ( 2, 2.9 )
 ( 3, 4.1 )
};
\end{axis}
\end{tikzpicture}
\end{document}

A common technique (if the background level is obvious) is to use a Y-offset. Like here: originexample

The obvious way would be to add a filter to shift the data (like below). However all the filters modify the data in a data coordinates not in units relative the axis size on paper.

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot+[
    y filter/.code={\pgfmathparse{#1+0.0}\pgfmathresult}
    ] coordinates{
 ( 1, 2 )
 ( 2, 3 )
 ( 3, 4 )
};
\addplot+[
    y filter/.code={\pgfmathparse{#1+0.5}\pgfmathresult}
    ] coordinates{
 ( 1, 2.1 )
 ( 2, 2.9 )
 ( 3, 4.1 )
};
\end{axis}
\end{tikzpicture}
\end{document}

How can I shift the data (e.g. in the Y-direction) in proportion to the size of the axis?

I found this answer here after posting: How to do a shift/offset of ycomb or ybar **in y-direction** without stacking in pgfplots? , but it has problem as it also shifts the point in the legend!

alfC
  • 14,350

1 Answers1

5

You can offset the plot using \addplot [yshift=1cm, legend image post style={yshift=-1cm}].

Jake
  • 232,450
  • So, it looks like internally the mark in the legend is part of the plot, not part of the legend or the legendentry. – alfC Mar 12 '15 at 17:08
  • Is there a variable that I can access that tells me the current size of the plot so I can shift in something approximately proportional to the plot size? \addplot [yshift=0.5\currentyaxislength, ... – alfC Mar 12 '15 at 17:10
  • @alfC: No, but you could define a macro \def\myheight{5cm} and use that to set the height of the axis and to shift your plot. – Jake Mar 12 '15 at 17:16
  • Also, I was wondering that maybe there is a "substyle" that controls only the curve and not the lengend, for example \addplot[curve draw style={yshift=1cm}] so the shift doesn't have to be repeated. no? – alfC Mar 12 '15 at 17:35
  • 1
    @alfC: I don't think that exists, but you can define your own: \pgfplotsset{shift plot/.style={yshift=#1, legend image post style={yshift=-#1}}}. Then you can shift the plot by setting shift plot=1cm. – Jake Mar 12 '15 at 17:38