39

I am doing some data analysis in python, putting the results in form of a matrix stored into a numpy array. I would like to put this results into a report and the best way would be to put a table with a tabular inside containing the data.

Is anyone aware of a quick way to output latex code from python, or any other way I could use to quickly format an array into a tabular code latex can handle ?

Thanks.

2
  • 1
    Can't you use something like print " & ".join([str(entry) for entry in row]) for each row? Here, row is a vector of numbers.
    – yori
    May 8, 2012 at 22:06
  • @Yori thanks! I used something like this before seeing Paulo's and Mike's answers and it worked.
    – user11170
    May 9, 2012 at 0:54

4 Answers 4

37

It's been a while since I played with NumPy, but I used to call the numpy.savetxt function to export my data as a .csv format:

import numpy
A = numpy.random.randn(4,4)
numpy.savetxt("mydata.csv", A)

The sample file mydata.csv was generated accordingly:

1.058690791897618361e-01 4.236767150069661314e-01 -9.871862191240249329e-02 1.896410657805123634e+00
-3.688082441801866507e-01 -6.185162583308108086e-01 7.779589745526608313e-01 -1.718082361568575633e+00
-2.750126418674324058e-01 1.636150392013778487e-01 -5.227282169549336555e-01 6.038835633452429574e-01
-1.113971762033877821e+00 -1.572603551712207670e+00 -6.206581544211196011e-01 -1.960843071998005893e+00

With the magic of pgfplotstable, we could do the following:

\documentclass{article}

\usepackage{pgfplotstable}
\usepackage{array}

\begin{document}

\begin{table}
\centering
\pgfplotstabletypeset[dec sep align,
   fixed zerofill,
   precision=4,
   col sep=space]{mydata.csv}
\end{table}

\end{document}

Output 1

I don't know if it's possible to remove the headers, but in any case I'll ask Christian Feuersänger about it. :)

My good friend percusse provided a nice way of removing those headers:

\pgfplotstabletypeset[%
   fixed zerofill,
   precision=4,
   col sep=space,
   dec sep align,
   columns/0/.style ={column name=},
   columns/1/.style ={column name=},
   columns/2/.style ={column name=},
   columns/3/.style ={column name=},
]{mydata.csv}

Another, well, "way" of doing it is by tricking numpy.savetxt to act as:

numpy.savetxt("mydata.csv", a, delimiter=' & ', fmt='%2.2e', newline=' \\\\\n')

which will give us the following output file:

1.21e+00 & 3.52e-01 & -5.53e-01 & 7.28e-01 \\
-1.61e+00 & 6.72e-01 & 5.75e-01 & -1.00e+00 \\
3.60e-01 & 1.68e-01 & -1.65e+00 & 2.10e-01 \\
5.73e-01 & -7.29e-03 & 1.65e+00 & -1.37e+00 You could remove the last `\\` and paste it inside a `tabular` enviroment. And thanks to `siunitx`, we get a nice formatting:

\documentclass{article}

\usepackage{siunitx}

\begin{document}
\begin{table}
  \begin{center}
    \begin{tabular}{SSSS}
    1.21e+00 & 3.52e-01 & -5.53e-01 & 7.28e-01 \\
    -1.61e+00 & 6.72e-01 & 5.75e-01 & -1.00e+00 \\
    3.60e-01 & 1.68e-01 & -1.65e+00 & 2.10e-01 \\
    5.73e-01 & -7.29e-03 & 1.65e+00 & -1.37e+00
    \end{tabular}
  \end{center}
\end{table}
\end{document}

Output 2

At last but not least, datatool can also be used. Unfortunately, it seems that datatool does not recognize scientific notation as real values, so it treats like a string value. To fix it, I exported my .csv file with a comma as delimiter and with fixed-point values:

numpy.savetxt("mydata.csv", a, delimiter=',', fmt='%2.2f')

which will generate the following output:

1.24,-0.96,-1.51,-0.21
0.93,-1.37,0.30,-0.89
0.33,-0.16,-1.27,-0.02
1.08,1.22,0.29,0.15

Now, our LaTeX code, based on Alan's answer:

\documentclass{article}

\usepackage{datatool}
\usepackage{siunitx}

\begin{document}

\DTLloaddb[noheader, keys={0,1,2,3}]{db}{mydata.csv}

\begin{table}

  \sisetup{%
    parse-numbers=false,
    table-number-alignment=left,
    table-figures-integer=4,
    table-figures-decimal=4,
    input-decimal-markers={.}
  }
  \renewcommand*\dtlrealalign{S}

  \centering
  \DTLdisplaydb{db}
\end{table}

\end{document}

Output 3

By using \DTLdisplaydb, headers are mandatory. If you don't want them, you can iterate through the values of the .csv value instead.

You can obtain more information on those packages by searching their tags: , ,

Thanks to Jake, percusse and Alan. :)

1
  • 2
    you can add every head row/.style={typeset cell/.style={}}, as option to \pgfplotstabletypeset. This will omit the header row. In fact, it will omit an empty header row. I will think about a suitable style which makes this accessable in a simpler way. May 9, 2012 at 19:48
37

Working from [SciPy-User] 2d array to Latex:

$ python
Python 2.6.6 (r266:84292, Dec 26 2010, 22:31:48)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
>>> a=numpy.zeros((2,2))
>>> print " \\\\\n".join([" & ".join(map(str,line)) for line in a])
0.0 & 0.0 \\
0.0 & 0.0
>>>

Add on tabular, table, and other environments or commands as needed.

1
  • Beautiful solution, Mike! :) May 9, 2012 at 0:17
27

There is a python package called tabulate that can directly output latex tabulars. You can find it on pypi here.

An example would be the following

import scipy
from tabulate import tabulate

a = scipy.random.rand(3,3)

print(tabulate(a, tablefmt="latex", floatfmt=".2f"))

which outputs something like

\begin{tabular}{rrr}
\hline
 0.97 & 0.48 & 0.76 \\
 0.56 & 0.52 & 0.86 \\
 1.00 & 0.11 & 0.25 \\
\hline
\end{tabular}
5

Since numpy.set_printoptions(precision=3) didn't work for me in the combination with Mikes answer, here is a little upgrade which allows defining the precision.

a = np.zeros((2,3))
print " \\\\\n".join([" & ".join(map('{0:.3f}'.format, line)) for line in a])

The precision is changed by changing the 0:.3f to the required number of digits.

Or for

Python 3

print(" \\\\\n".join([" & ".join(map('{0:.3f}'.format, line)) for line in a]))

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .