8
$\begingroup$

General purpose

My intension is to use data created by Mathematica as in

but with a nicer $\LaTeX$ conform font style and similar stuff. This is the reason why I am using the $\LaTeX$ package pgfplots. This package also allows me to add further information and fancy graphical stuff (by using TikZ) to the plot at later time.

Issue

Now I do not get Mathematica to write me a data-file which can be read by the $\LaTeX$ package pgfplots. The current data (and export) has the following structure:

DataTable = {{9/50, 5.00251*10^9, 3.02252*10^9, 2.17472*10^9, 1.97472*10^9},<<1000>>};
Export["Energy.csv", DataTable];

The idea is to plot in the end something like

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{document}
\begin{tikzpicture}
\begin{axis}[%
    xlabel={xlabel},
    ylabel={ylabel}]
\addplot[color=blue,mark=none] 
    table [x expr=\thisrowno{0}, y expr=\thisrowno{1}, col sep=space] {data.dat};
\addplot[color=blue,mark=none] 
    table [x expr=\thisrowno{0}, y expr=\thisrowno{2}, col sep=space] {data.dat};
\end{axis}
\end{tikzpicture}
\end{document}

EDIT:

The $\LaTeX$ routine pgfplots uses the following data structure (where the option col sep=space|tab|comma|colon|semicolon|braces|&|ampersand defines the separation character - same is also vaid for the column separation row sep=newline|\\):

Field,Energy1,    Energy2,    Energy3,    Energy4
9/50, 5.00251e+09,3.02252e+09,2.17472e+09,1.97472e+09
... (all data like this first row of data)

You might want to check out chapter 4.3.2 (Reading Coordinates From Tables) in the pgfplots manual which contains all important informations. By the way the header is not necessary but helps other readers to understand your data.

$\endgroup$
4
  • 2
    $\begingroup$ So, what structure would be required for this to be possible, for those of us without a familiarity/working installation of $\LaTeX$? $\endgroup$ Commented Jan 4, 2014 at 0:12
  • 1
    $\begingroup$ The "CSV" export of Mathematica creates exactly the structure you like, where one line is exported as 9/50,5.00251e9,3.02252e9,2.17472e9,1.97472e9. In you pgfplots code, you used data.dat which should be data.csv with your current approach. What exactly is the issue? $\endgroup$
    – halirutan
    Commented Jan 4, 2014 at 13:37
  • $\begingroup$ @halirutan: It was a stupid error that occured because I did not choose the correct file (I still used the data.dat file. You are completely right. +1 I will rephrase an answer out of my question for other interested people (as I invested too much time in that issue). $\endgroup$
    – strpeter
    Commented Jan 4, 2014 at 16:46
  • 1
    $\begingroup$ @strpeter No problem. This happens to everyone once in a while. The good thing is, that it works now. $\endgroup$
    – halirutan
    Commented Jan 4, 2014 at 16:47

1 Answer 1

6
$\begingroup$

Mathematica export

Just export the data such that in one column you have the x-coordinates and in another you have the y-coordinates.

DataTable = {{9/50, 5.00251*10^9, 3.02252*10^9, 2.17472*10^9, 1.97472*10^9},<<1000>>};
DataTableWithHeader = Flatten[{{{"ColumnX", "ColumnY1", "ColumnY2", "ColumnY3", "ColumnY4"}}, DataTable}, 1];
Export["Energy.csv", DataTableWithHeader];

$\LaTeX$ import and compiling

You now can use a compiler like pdflatex to create your plots from a $\LaTeX$-file like the folowing - like you desire.

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\begin{axis}[%
    xlabel={xlabel},
    ylabel={ylabel}]
\addplot[color=blue,mark=none] 
    table [x expr=\thisrowno{0},y expr=\thisrowno{1},col sep=comma]{Energy.csv};
\addplot[color=blue,mark=none] 
    table [x expr=\thisrowno{0},y expr=\thisrowno{2},col sep=comma]{Energy.csv};
\end{axis}
\end{tikzpicture}
\end{document}

The resulting images might look like the following example.

Illustration created with pgfplots

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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