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 a file (table) with a lot of columns. In the first column I have time and in the nexus three columns that represent vector components. Now I want to plot vector magnitude in function of time. It is possible to do it with a simple command?

Another problem. I have a file containing an angular speed and a work that are related. Now, instead of work I want to plot efficiency. It is possible to divide the work column by a number? And if I have to use a different number for each work point and his differents numbers are stored in a file, it is still possible?

For the first case I have the following code:

\documentclass{standalone}

\useoackage{pfgplots}

\begin{document}

\begin{figure}
\centering
        \begin{tikzpicture}
    \begin{axis}[title={}, xlabel={$x$}, ylabel={$z$},%
    legend cell align=left]

        \addplot[orange,thick]
            table[x index=0,y index=1] {mydata.dat};

        \legend{}
    \end{axis}
    \end{tikzpicture}
\end{figure}

\end{document}

And the following file:

0 0 0 0
1 0.1 0 0
2 0.2 0.1 0
3 0.3 0.2 0.1

I want to plot the square root of the sum of the square of the last three columns (the columns represent vector components, so I want the magnitude) in function of time (the first column). Is it possible?

Ps: I posted this two problems in the same question because I think they are strictly related.

share|improve this question
3  
It really would help if you composed a fully compilable MWE that at least sets up the plot and has a few sample data points. While solving problems is fun, setting them up is not. You can use filecontents so that the data points can be packaged in one file that can be cut and pasted to get those trying to help started. –  Peter Grill Oct 7 '12 at 20:07
    
@PeterGrill Sorry. I don't have the data on this computer. I will try to improve my question! –  R. M. Oct 7 '12 at 20:22
    
@PeterGrill Edited. I hope it is better. –  R. M. Oct 7 '12 at 20:27
    
This is not a major problem, but the title is slightly misleading because it's not really calculus you're doing, just arithmetic. I first looked at this question thinking you wanted a way to calculate numerical derivatives in TeX ;-) –  David Z Oct 7 '12 at 20:44

1 Answer 1

up vote 11 down vote accepted

This is indeed possible using the x expr and similar options to \addplot table, described in section 4.2.5 of the PGFplots documentation. These options allow you to manually calculate the values to plot based on the input data, using PGF's math parser. The usage is probably best shown by example:

\documentclass{standalone}
\usepackage{pgfplots}

\begin{document}
 \begin{tikzpicture}
  \begin{axis}
   \addplot table[x index=0,
     y expr={sqrt(\thisrowno{1}^2 + \thisrowno{2}^2 + \thisrowno{3}^2)}]
     {mydata.dat};
  \end{axis}
 \end{tikzpicture}
\end{document}

Your other example can be done the same way if all the numbers are in the same file; you'd just need to replace the mathematical expression with the appropriate one for the new situation. If you have your numbers in different files, then you could write a shell script to pull the numbers from the various files and write them to standard output, and then use \addplot shell to make PGFplots parse the output of the command. But unless the numbers are changing frequently, it's probably less work to just run the shell command manually to create mydata.dat once.

share|improve this answer
1  
Very nice! Note that instead of merging different files using a shell script, you could also use a create on use column style to pull the data together right within TeX. See for example tex.stackexchange.com/questions/53227/… –  Jake Oct 7 '12 at 20:46
    
Ah, I didn't know about that (I've never used pgfplotstable, only pgfplots). If you'd like to post an answer describing how to do that, go right ahead, and feel free to reuse any of mine if you want. –  David Z Oct 7 '12 at 20:55
    
Thank you! I will accept the question tomorrow, when I will try your code (also if I know is what I wanted). ;) –  R. M. Oct 7 '12 at 21:09

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.