12

I have a file (table) with a lot of columns. In the first column I have a time variable and the next three columns represent the vector components of position. Now I want to plot the vector magnitude in function of time. It is possible to do it with a simple command?

Another, related, problem. I have a file containing an angular velocity and a work, which are related. Now, instead of work I want to plot the 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 these different 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}

where mydata.dat contains the following:

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?

4
  • 5
    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. Oct 7, 2012 at 20:07
  • @PeterGrill Sorry. I don't have the data on this computer. I will try to improve my question!
    – user17895
    Oct 7, 2012 at 20:22
  • @PeterGrill Edited. I hope it is better.
    – user17895
    Oct 7, 2012 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, 2012 at 20:44

1 Answer 1

17

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.

3
  • 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, 2012 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, 2012 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). ;)
    – user17895
    Oct 7, 2012 at 21:09

You must log in to answer this question.