[Tex/LaTex] How to scale existing coordinates data in pgfplots

pgfmathpgfplots

I have a plot with its data, for example

\documentclass[]{article}
\usepackage[]{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[]
\addplot+[%multiply x data by a factor here, multiply y data by another factor
] plot[] coordinates{
 ( 1, 2 )
 ( 2, 3 )
 ( 3, 4 )
};
\end{axis}
\end{tikzpicture}
\end{document}

but I want pgfplots to apply a transformation (in a per-plot basis) to the data.

How do I scale (or more generally transform) the data before plotting?

If possible, this should work for coordinates, and be an option for addplot[...] or plot[...]

I tried x coord trafo/.code={\pgfmathparse{#1*scalefactor}\pgfmathresult}, but that only seems to work for the full axis and not for each curve in the plot.

The effect, should be in the example above, the same as having:

...
coordinates{
 ( 100., 0.2 )
 ( 200., 0.3 )
 ( 300., 0.4 )
};
...

Best Answer

You can use an x filter/.code to transform the x coordinates on a per plot basis. This also works if you use \addplot table instead of \addplot coordinates:

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot+[
    x filter/.code={\pgfmathparse{#1*100}\pgfmathresult},
    y filter/.code={\pgfmathparse{#1*10}\pgfmathresult}
    ] coordinates{
 ( 1, 2 )
 ( 2, 3 )
 ( 3, 4 )
};
\end{axis}
\end{tikzpicture}
\end{document}


If you can provide your data in tabular form instead of as a coordinate list, things get more comfortable, since you can then simply use x expr=\thisrow{X} * 100, y expr=\thisrow{Y} * 10:

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot table [x expr=\thisrow{X}*100, y expr=\thisrow{Y}*10] {
 X Y
 1 2
 2 3
 3 4
};
\end{axis}
\end{tikzpicture}
\end{document}