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 need to draw a shifted plot from a data table (or a external file) without edit table coordinates. Using the shift={(x,y)} option works but x and y are not related to the axis coordinates s and f. Any advice?

\documentclass{article}

\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
    xmin=0, xmax=1,
    ymin=0, ymax=360,
    xlabel=$s$,
    ylabel=$f(s)$
]
\addplot[shift={(0.0,0.0)}] table[header=true,x=s,y=f] {
    s       f
    0.0     75.9638
    0.380665    75.9638
    0.380665    206.565
    0.58711     206.565
    0.58711     243.435
    0.793555    243.435
    0.793555    333.435
    1.0     333.435
};
\end{axis}
\end{tikzpicture}

\end{document}
share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

The proper way to do achieve this is to use filters: You can use x filter/.code={<code>} to perform mathematical operations on the coordinates. Inside the <code> of an x/y filter, \pgfmathresult holds the current coordinate, and the content of \pgfmathresult at the end of the code will be used as the new coordinate. Alternatively, you can use the key disabledatascaling for the axis environment, then you'll be able to use the shift key in the way you tried it. This won't work with very large values that require the fpu library, however. The development version of PGFplots also supports using shift={(axis cs:<x>,<y>)}, which will work with large values.

\documentclass{article}

\usepackage{pgfplots}

\begin{document}

\pgfplotstableread{
    s       f
    0.0     75.9638
    0.380665    75.9638
    0.380665    206.565
    0.58711     206.565
    0.58711     243.435
    0.793555    243.435
    0.793555    333.435
    1.0     333.435
}\data

\begin{tikzpicture}
\begin{axis}[
    xmin=0, xmax=1,
    ymin=0, ymax=360,
    xlabel=$s$,
    ylabel=$f(s)$,
    no markers
]
\addplot table[x=s,y=f] \data;
\addplot +[shift={(-0.1,10)}] table \data;
\addplot +[x filter/.code={\pgfmathparse{\pgfmathresult+0.1}},
  y filter/.code={\pgfmathparse{\pgfmathresult-10}}] table \data;
\end{axis}
\end{tikzpicture}

\end{document}

shifting plots in PGFplots

share|improve this answer
    
The second \addplot doesn't work for me: ! Undefined control sequence. <argument> \pgfplots@data@scale@trafo@EXPONENT@x l.28 \addplot +[shift={(axis cs:-0.1,10)}] t<<next line>>able \data; –  Fernando Nadal Jun 4 '11 at 13:27
    
@Fernando: Sorry, this only works in the development version. You have to use the disabledatascaling key, or use the x filter/.code. I've edited my answer accordingly. –  Jake Jun 4 '11 at 20:26
    
Thanks a lot, third \addplot works great for me anyway. –  Fernando Nadal Jun 4 '11 at 21:22
    
@Fernando: Great! If you think this answers your question, it would be good if you accept this answer by clicking the tick mark next to it. That will mark the question as answered and give me and you some reputation. –  Jake Jun 4 '11 at 21:57
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.