creating publication quality tables in python

  • Last Update :
  • Techknowledgy :

You could also just write an HTML table file and style it with css.

with open("example.html", "w") as of:
of.write("<html>
<table>")
   for index, row in enumerate(data):
   if index == 0:
   of.write("<th>")
      else:
      of.write("<tr>")

         for cell in row:
         of.write("<td>" + cell + "</td>")
         if index == 0:
         of.write("</th>")
   else:
   of.write("</tr>")

   of.write("</table>

</html>")

Suggestion : 2

 Apr 10, 2021  4 min read,12 Apr 2021 - Add R packages to related work,12 Apr 2021 - Add reference to related work,10 Apr 2021 - Post: Publication ready tables

1._
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import seaborn as sns
import os

def bold_extreme_values(data, data_max = -1):

   if data == data_max:
   return "\\bfseries %s" % data

return data

if __name__ == "__main__":

   # Load data and
# calculate mean of each column
df = (sns.load_dataset('iris')
   .groupby("species")
   .mean()
   .reset_index()
)

# Specify in which columns to make the maximum bold
col_show_max = ["sepal_length", "sepal_width",
   "petal_length", "petal_width"
]

# Iterate through columns
for k in col_show_max:
   df[k] = df[k].apply(
      lambda data: bold_extreme_values(data, data_max = df[k].max()))

# Set column header to bold title
case
df.columns = (df.columns.to_series()
   .apply(lambda r: "\\textbf}".format(
      r.replace("_", " ").title())))

# Write to file
with open(
      os.path.splitext(
         os.path.basename(__file__))[0] + ".tbl", "w") as f:

   format = "l" + \
   "@{\hskip 12pt}" + \
   4 * "S[table-format = 2.2]"

f.write(df.head()
   .to_latex(index = False,
      escape = False,
      column_format = format)
)
2._
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
3._
import seaborn as sns
import os

def bold_extreme_values(data, data_max = -1):

   if data == data_max:
   return "\\bfseries %s" % data

return data

if __name__ == "__main__":

   # Load data and
# calculate mean of each column
df = (sns.load_dataset('iris')
   .groupby("species")
   .mean()
   .reset_index()
)

# Specify in which columns to make the maximum bold
col_show_max = ["sepal_length", "sepal_width",
   "petal_length", "petal_width"
]

# Iterate through columns
for k in col_show_max:
   df[k] = df[k].apply(
      lambda data: bold_extreme_values(data, data_max = df[k].max()))

# Set column header to bold title
case
df.columns = (df.columns.to_series()
   .apply(lambda r: "\\textbf}".format(
      r.replace("_", " ").title())))

# Write to file
with open(
      os.path.splitext(
         os.path.basename(__file__))[0] + ".tbl", "w") as f:

   format = "l" + \
   "@{\hskip 12pt}" + \
   4 * "S[table-format = 2.2]"

f.write(df.head()
   .to_latex(index = False,
      escape = False,
      column_format = format)
)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import seaborn as sns
import os

def bold_extreme_values(data, data_max = -1):

   if data == data_max:
   return "\\bfseries %s" % data

return data

if __name__ == "__main__":

   # Load data and
# calculate mean of each column
df = (sns.load_dataset('iris')
   .groupby("species")
   .mean()
   .reset_index()
)

# Specify in which columns to make the maximum bold
col_show_max = ["sepal_length", "sepal_width",
   "petal_length", "petal_width"
]

# Iterate through columns
for k in col_show_max:
   df[k] = df[k].apply(
      lambda data: bold_extreme_values(data, data_max = df[k].max()))

# Set column header to bold title
case
df.columns = (df.columns.to_series()
   .apply(lambda r: "\\textbf}".format(
      r.replace("_", " ").title())))

# Write to file
with open(
      os.path.splitext(
         os.path.basename(__file__))[0] + ".tbl", "w") as f:

   format = "l" + \
   "@{\hskip 12pt}" + \
   4 * "S[table-format = 2.2]"

f.write(df.head()
   .to_latex(index = False,
      escape = False,
      column_format = format)
)
1._
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16\ documentclass[tikz, crop, convert = {
   density = 400,
   outext = .png
}] {
   standalone
}

\ usepackage {
   booktabs
}\
usepackage {
   etoolbox
}\
usepackage[round - mode = places, detect - weight = true, detect - inline - weight = math] {
   siunitx
}\
renewcommand\ arraystretch {
   1.2
}

\
listfiles

\ begin {
   document
}\
begin {
   table
}\
robustify\ bfseries\ caption {
   A generated table
}\
input {
   table.tbl
}\
end {
   table
}\
end {
   document
}
2._
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
3._
\
documentclass[tikz, crop, convert = {
   density = 400,
   outext = .png
}] {
   standalone
}

\ usepackage {
   booktabs
}\
usepackage {
   etoolbox
}\
usepackage[round - mode = places, detect - weight = true, detect - inline - weight = math] {
   siunitx
}\
renewcommand\ arraystretch {
   1.2
}

\
listfiles

\ begin {
   document
}\
begin {
   table
}\
robustify\ bfseries\ caption {
   A generated table
}\
input {
   table.tbl
}\
end {
   table
}\
end {
   document
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
\
documentclass[tikz, crop, convert = {
   density = 400,
   outext = .png
}] {
   standalone
}

\ usepackage {
   booktabs
}\
usepackage {
   etoolbox
}\
usepackage[round - mode = places, detect - weight = true, detect - inline - weight = math] {
   siunitx
}\
renewcommand\ arraystretch {
   1.2
}

\
listfiles

\ begin {
   document
}\
begin {
   table
}\
robustify\ bfseries\ caption {
   A generated table
}\
input {
   table.tbl
}\
end {
   table
}\
end {
   document
}

Suggestion : 3

I'd like to create publication quality anycodings_tabular tables for output as svg or jpg or png anycodings_tabular images using python.,I'm familiar with the texttable module which anycodings_tabular produces nice text tables but if I have for anycodings_tabular example ,Is there a module I can turn to, or can you anycodings_tabular point me to a process for going about it?,You can also use Tabular to output latex anycodings_python source as per anycodings_python http://en.wikibooks.org/wiki/LaTeX/Tables

You could also just write an HTML table anycodings_python file and style it with css.

with open("example.html", "w") as of:
of.write("<html>
<table>")
   for index, row in enumerate(data):
   if index == 0:
   of.write("<th>")
      else:
      of.write("<tr>")

         for cell in row:
         of.write("<td>" + cell + "</td>")
         if index == 0:
         of.write("</th>")
   else:
   of.write("</tr>")

   of.write("</table>

</html>")

Suggestion : 4

Another feature of ZenTables is that it makes it easy to create descriptive tables commonly found in publications, such as frequency tables and mean tables with standard deviations. These tables typically require special treatments such as providing percentages or standard deviations in parentheses or adding subtotals and totals. ZenTables handles all these things for you. ,Another common descriptive table is mean tables with frequency and standard deviation. The df.zen.mean_sd_table() is designed specifically for this:,You might wonder: what if I have other types of tables that I would like to create? No problem! ZenTables also has a generic pivot_table() function that is built on the pandas version of pivot_table(). For example, if you want to create a table with counts, mean, and median, you can do this:,That’s it! ZenTables works on any DataFrame object. Just use the accessor .zen with any DataFrame and all functionalities are readily available. Here’s the table that ZenTables outputs.

Installing ZenTables is very easy. First, ensure that you have Python 3.7.1+ and pandas 1.3.0+ installed. Then, you can install ZenTables with pip:

pip install zentables

To use ZenTables, import both pandas and ZenTables in your Jupyter Notebook:

import pandas as pd
import zentables as zen

Then you will be able to access all ZenTables functionalities as a pandas add-on. To illustrate this, let’s first import some data:

url = (
   "https://raw.githubusercontent.com/thepolicylab"
   "/ZenTables/main/tests/fixtures/superstore.csv?raw=true"
)
super_store = pd.read_csv(url)

Now let’s see what ZenTables can do to make this table look better:

df.zen.pretty()

Note the improvement in readability compared to the original output, and you only need to write one line of code. You can even chain the method with the original code like this:

super_store.pivot_table(
   index = ["Segment", "Region"],
   columns = ["Category"],
   values = "Order ID",
   aggfunc = "count",
   margins = True,
).zen.pretty()

Suggestion : 5

I'd like to create publication quality tables for output as svg or jpg or png images using python.,Is there a module I can turn to, or can you point me to a process for going about it?,You can use ReportLab, as per Python reportlab inserting image into table,You can do something similar with Latex tables as an output.

You could also just write an HTML table file and style it with css.

with open("example.html", "w") as of:
of.write("<html>
<table>")
   for index, row in enumerate(data):
   if index == 0:
   of.write("<th>")
      else:
      of.write("<tr>")

         for cell in row:
         of.write("<td>" + cell + "</td>")
         if index == 0:
         of.write("</th>")
   else:
   of.write("</tr>")

   of.write("</table>

</html>")

Suggestion : 6

A Package To Make Publication Quality Latex Tables From Python Regression Output ,A Package To Make Publication Quality Latex Tables From Python Regression Output, A Package To Make Publication Quality Latex Tables From Python Regression Output ,This package was built to emulate the features of estout or esttab in Stata although it lacks a lot of their functionality. This package has been tested with statsmodels OLS and linearmodels OLS, IV2SLS and PanelOLS. Current limitations are that it does not have full support for all relevant model statistics, though this will be added in future releases.

# Testing pystout
import statsmodels.api as sm
import linearmodels as ln
from pystout
import pystout

dta = sm.datasets.webuse('auto')
dta.loc[: , 'const'] = 1

y = dta.price

# === === === === === === === === === === === === === === === === === === === === === === === === === ==
   # First three models are from statsmodels
# === === === === === === === === === === === === === === === === === === === === === === === === === ==
   X = dta[['const', 'mpg', 'displacement']]
model1 = sm.OLS(y, X).fit()

X = dta[['const', 'mpg', 'displacement', 'turn']]
model2 = sm.OLS(y, X).fit()

X = dta[['displacement', 'const', 'turn']]
model3 = sm.OLS(y, X).fit()

# === === === === === === === === === === === === === === === === === === === === === === === === === ==
   # Next 2 are an ols and a tsls from linearmodels
# === === === === === === === === === === === === === === === === === === === === === === === === === ==
   X = dta[['displacement', 'const', 'turn', 'gear_ratio']]
model4 = ln.OLS(y, X).fit()

model5 = ln.IV2SLS(dependent = dta.price, endog = dta.mpg,
   exog = dta.filter(['const', 'turn', 'displacement']),
   instruments = dta.length).fit()

# === === === === === === === === === === === === === === === === === === === === === === === === === ==
   # Print result
# === === === === === === === === === === === === === === === === === === === === === === === === === ==
   pystout(models = [model1, model2, model3, model4, model5],
      file = 'test_table.tex',
      addnotes = ['Here is a little note', 'And another one'],
      digits = 2,
      endog_names = ['Custom', 'Header', 'Please', 'Thanks', 'Again'],
      varlabels = {
         'const': 'Constant',
         'displacement': 'Disp',
         'mpg': 'MPG'
      },
      addrows = {
         'Test': ['A', 'Test', 'Row', 'Here', 'Too']
      },
      mgroups = {
         'Statsmodels': [1, 3],
         'L OLS': 4,
         'L TSLS': 5
      },
      modstat = {
         'nobs': 'Obs',
         'rsquared_adj': 'Adj. R\sym{2}',
         'fvalue': 'F-stat'
      }
   )

Suggestion : 7

Ben Barad 20 August 2014

import matplotlib
import matplotlib.pyplot as plt

data = [220, 14.2, 150, 400, 420]
error = [10, 1, 20, 60, 10]
x = [i + .5
   for i in range(5)
]

fig, ax = plt.subplots()
bar = ax.bar(x, data, 0.8, align = "center", yerr = error)
plot = ax.plot(x, data)
ax.set_xticks(x)
ax.set_xticklabels(('wt', 'N23PP', 'N23PP/PEKN', 'PEKN', 'N23PP/PEKN/L28F'))
ax.set_title(r "Everything in the document can use m$\alpha$th language", y = 1.05)
ax.set_ylabel(r "Rate (s$^{-1}$)", labelpad = 10)
ax.set_xlabel("Mutant", labelpad = 10)
ax.yaxis.set_ticks_position('left')
ax.xaxis.set_ticks_position('bottom')
plt.savefig('test.png')
plt.show()
import numpy as np
import matplotlib
import matplotlib.pyplot as plt

x = np.arange(0, 5, .01)
fig, ax = plt.subplots()
for i in range(10):
   y = [np.sin(datum ** 2 + i / 10.0) for datum in x]
ax.plot(x, y)
ax.yaxis.set_ticks_position('left')
ax.xaxis.set_ticks_position('bottom')
ax.set_title("Color Cycle, demonstrated", y = 1.05)
ax.set_ylabel(r "$sin(x^{2}+\lambda{})$", labelpad = 5)
ax.set_xlabel(r "x", labelpad = 5)
plt.savefig('test2.png')