0

Related to my recent question on printing \pm in latex with pandas How to print a literal backslash to get \pm in a pandas data frame to generate a proper error bars in a latex table in python? I also wanted to display the table with the equations being shown correctly but instead I get the literal \pm in my table (+ it has a weird shape).

enter image description here

Is there a way to convert my data frame with equations to a png latex directly from python and save it as an image?

script

import pandas as pd

# data = {'first_column':  ['first_value', 'second_value', ...],
#         'second_column': ['first_value', 'second_value', ...],
#          ....
#         }

import pandas as pd

from uutils import put_pm_to_pandas_data

 data = {
     'Initialization': ['Random',
                        'Random2',
                        ],

     'Test Accuracy': ['0.200+-0.029',
                       '0.200+-0.0',
                       ],
 }

# - to pandas table
df = pd.DataFrame(data)
print(df)

# https://stackoverflow.com/questions/70009242/how-does-one-generate-latex-table-images-with-proper-equations-from-python-panda

# - to latex
data = put_pm_to_pandas_data(data)
df = pd.DataFrame(data)

print(df.to_latex(index=False, escape=False))

import matplotlib.pyplot as plt
import pandas as pd
from pandas.plotting import table

# ax = plt.subplot(111, frame_on=False) # no visible frame
ax = plt.gca()
ax.xaxis.set_visible(False)  # hide the x axis
ax.yaxis.set_visible(False)  # hide the y axis

# table(ax, df)  # where df is your data frame
table(ax, df)  # where df is your data frame

plt.show()

# plt.savefig('mytable.png')

related:

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
Charlie Parker
  • 5,884
  • 57
  • 198
  • 323

1 Answers1

1

You didn't provide us with the definition of put_pm_to_pandas_data. But here is a solution using DataFrame.replace

import matplotlib.pyplot as plt
import pandas as pd
from pandas.plotting import table

def put_pm_df(df):
    return df.replace("\+-", r"$\pm$", regex=True)
    
data = {
     'Initialization': ['Random',
                        'Random2',
                        ],
     'Test Accuracy': ['0.200+-0.029',
                       '0.200+-0.0',
                       ],
}

df = pd.DataFrame(data)
print(df)

df = put_pm_df(df)
print(df)

print(df.to_latex(index=False, escape=False))

ax = plt.gca()
ax.xaxis.set_visible(False)  # hide the x axis
ax.yaxis.set_visible(False)  # hide the y axis

table(ax, df) 

plt.show()

Output

# initial df

  Initialization Test Accuracy
0         Random  0.200+-0.029
1        Random2    0.200+-0.0

# result of put_pm_df(df)

  Initialization    Test Accuracy
0         Random  0.200$\pm$0.029
1        Random2    0.200$\pm$0.0

# df.to_latex(index=False, escape=False)

\begin{tabular}{ll}
\toprule
Initialization &    Test Accuracy \\
\midrule
        Random &  0.200$\pm$0.029 \\
       Random2 &    0.200$\pm$0.0 \\
\bottomrule
\end{tabular}

enter image description here

Rodalm
  • 5,169
  • 5
  • 21