23

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 )
};
...
alfC
  • 14,350
  • It's much more comfortable if you use \addplot table instead of \addplot coordinates. Do you have a particular reason for preferring the latter? – Jake Sep 23 '13 at 19:55
  • @Jake, ideally I would like a solution that works both with table and coordinates, to reprocess source files that are already there and I want to change units of those plots. – alfC Sep 23 '13 at 19:59

1 Answers1

24

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}
Jake
  • 232,450
  • at the same time the first solution is more general since works both for coordinates and table – alfC Sep 23 '13 at 20:05
  • @alfC: That's true. For new plots, however, I don't think there's any reason to prefer coordinates over table. Plus, \addplot table allows you to provide the data either inline or in a data file. – Jake Sep 23 '13 at 20:08
  • 6
    If you find strange error messages after trying this solution try with \pgfmathparse{\pgfmathresult*10}\pgfmathresult, see http://tex.stackexchange.com/questions/134754/very-strange-interaction-between-addlegendentry-and-y-filter-in-pgfplot – alfC Sep 27 '13 at 21:51
  • Note for LaTeX beginners (like me): If you would like to scale down the data, \thisrow{Y}/10 won't work. You have to use \thisrow{Y}*(1/10). – ComFreek Sep 09 '14 at 12:13
  • @ComFreek: \thisrow{Y}/10 works fine for me. Are you using an old version of PGFPlots, by any chance? – Jake Sep 09 '14 at 17:08
  • @Jake I'm using PGF 2.10 (compatiblity mode set to 1.9). I think I discovered the problem: the use of empty fields with \thisrow{y}/10 throws an error. See http://pastebin.com/KYTBmjRk – ComFreek Sep 10 '14 at 12:55