4

I am trying to multiply the x coordinates of a plot by a certain factor.

Section 4.3.4 from the pgfplots manual and this thread explain how to use mathematical expressions with data tables using /pgfplots/table/x expr={expression}.

My data are provided to pgfplots as coordinates lists, is there a mean to use it this way or should I switch my data to tables?

EDIT: here's my code

\begin{tikzpicture}
 \tikzstyle{every node}=[font=\scriptsize]
\begin{axis}[
    ybar=5pt,
    enlarge x limits=0.15,
    legend pos=north east,
    legend style={legend columns=1},
    ylabel={Number of fruits daily eaten},
    symbolic x coords={1,2,3,4,5},
    bar width=5pt,
    ymin=1,
    ymax=20,
    xticklabels={orange,apple,banana,strawberry,tomato},
    error bars/.cd,
    yminorgrids=true,
    ymajorgrids=true,
    nodes near coords,
    every node near coord/.append style={rotate=90, anchor=west},
    scaled ticks=base 10:0,     %insane 
]
\addplot+[fill, text=black, error bars/.cd, y dir=both, y explicit]
 coordinates {
    (1,1450)% +- (121,230)
    (2,1478)%  +- (4,7)
    (3,1494)% +- (18,27)
    (4,1476)% +- (18,27)
    (5,1494)% +- (18,27)
};
\addplot+[fill, text=black, error bars/.cd, y dir=both, y explicit]
coordinates {
    (1,446)% +- (20,32)
    (2,1747)%  +- (4,7)
    (3,2327)%  +- (2,4)
    (4,2497)% +- (18,27)
    (5,2988)% +- (18,27)
};
\addplot+[fill, text=black, error bars/.cd, y dir=both, y explicit]
coordinates {
    (1,1293)% +- (14,16)
    (2,4740)%  +- (4,7)
    (3,12252)%  +- (4,7)
    (4,16091)% +- (18,27)
    (5,20115)% +- (18,27)
};


\legend{Robert, Gertrude, Roger}

\end{axis}
\end{tikzpicture}
6

From the manual about coordinate lists

You should only use this input format if you have short diagrams and you want to provide mathematical expressions for each of the involved coordinates. Any data plots are typically easier to handle using a table format and \addplot table.

But if you really have to, you have to provide the coordinate transformations manually for all plots in the axis environment hence it wouldn't be ideal because the axes needs to be setup: Here I fake a linear plot with a squared input

\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
  \begin{tikzpicture}
    \begin{axis}[ 
      axis x line=bottom,
      axis y line=left,
      ultra thick,
      y coord trafo/.code={\pgfmathparse{sqrt(#1)}},
      y coord inv trafo/.code={\pgfmathparse{pow(#1,2)}}
    ] 
    \addplot coordinates {(0,0)(1,1)(2,4)(3,9)(4,16)(5,25)};
  \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

See section 4.22 for more information about these transformations

3
  • Thanks for the reply. When I try to play with /pgfplots/x coord trafo/.code={...} I have weird results and the display is empty. Based on your advice and with some further research I found /pgfplots/x filter/.code={...} which appeared to work well. Any idea why?
    – denaitre
    Jun 15 '15 at 12:38
  • @denaitre Can you add your code to your question so we can see what is exactly going on? But I would really suggest that you switch to tables. Much easier and more robust.
    – percusse
    Jun 15 '15 at 12:55
  • 1
    sorry I should have put it first. It is done. My problem was solved by adding the following : y filter/.code={\pgfmathparse{(1.8 / #1) * 4096}\pgfmathresult}%
    – denaitre
    Jun 15 '15 at 13:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.