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 have created a simple 2D plot from a table (xy-data) and now I would like to both translate and scale the x-axis. (I would prefer not to scale the actual data only the axis).

As an example, let's say the x-values runs between (a,b) and I now want them to be between (0,1) instead. How would I go about doing this? I have found an example on how to subtract a constant but I cannot get it to work with multiplication. Any help would be much appreciated.

\documentclass{article}
\usepackage{pgfplots}
\begin{document}

\begin{tikzpicture}
\begin{axis}[scaled x ticks=manual:{}{% 
\pgfmathfloatcreate{1}{6.0}{0}%
\pgfmathfloatsubtract{#1}{\pgfmathresult}%
}] 
\addplot coordinates {
(6, 2)
(8, 3)
};
\end{axis} 
\end{tikzpicture}

\end{document}
share|improve this question
2  
Please add an MWE to your question by editing. Note that a full MWE is a compilable example (yours don't include a documentclass, used packages or a \begin{document} and a \begin{end}. –  N.N. Aug 23 '11 at 15:08
    
Hopefully it's a MWE now :-) –  Jimbo Aug 24 '11 at 6:43
add comment

2 Answers

up vote 2 down vote accepted

You can do the subtraction and division in one command, using scaled x ticks=manual:{}{\pgfmathparse{ (#1 - 6) / 2 } }.

If you want to make this a bit more comfortable, you can define a new style using

\pgfplotsset{
    normalise/.style 2 args={
        scaled x ticks=manual:{}{% 
            \pgfmathparse{ ( ##1 - (#1) ) / (#2 - #1) }%
        }
    }
}

You can then call normalise={<lower>}{<upper>} in the axis options to normalise the ticks.

Note that this only scales each tick label, which means you might end up with unelegant tick positions (try normalising between 6 and 9).

I would recommend to normalise the coordinates themselves instead. You can do this using x filter/.code instead of scaled x ticks. This will alter the coordinates themselves, instead of just the tick labels. This means that the algorithm that determines which tick positions to display can work with the scaled coordinates, which will usually yield better results:

\documentclass{article}

\usepackage{pgfplots}
\begin{document}

\begin{tikzpicture}

\pgfplotsset{
    normalise/.style 2 args={
        x filter/.code={% 
            \pgfmathparse{ ( ##1 - (#1) ) / (#2 - #1) }%
        }
    }
}

\begin{axis}[
    normalise={6}{9}
] 
\addplot coordinates {
    (6, 2)
    (8, 3)
    (9,1)
};
\end{axis} 
\end{tikzpicture}

\end{document}

For comparison, here's the output you'd get if you used scaled x ticks instead. Note that there are seven tick labels instead of 6:

share|improve this answer
    
This is exactly the kind of solution I was looking for! Thank you! –  Jimbo Aug 24 '11 at 6:56
add comment

I think normalization is quite easier, but you can set and name the ticks manually for example for a range from a to e, [a:e]:

\begin{axis}[xtick={a,b,c,d,e},
             xticklabels={0, 0.25, 0.5, 0.75, 1},
            ]
share|improve this answer
    
This would also work, however, I would like to avoid setting the ticks manually (too many plots). –  Jimbo Aug 24 '11 at 6:58
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.