Based on this answer you can also use Latex to create a table.
For ease of usability, you can create a function that turns your data into the corresponding text-string:
import numpy as np
import matplotlib.pyplot as plt
from math import pi
from matplotlib import rc
rc('text', usetex=True)
# function that creates latex-table
def latex_table(celldata,rowlabel,collabel):
table = r'egin{table} egin{tabular}{|1|'
for c in range(0,len(collabel)):
# add additional columns
table += r'1|'
table += r'} hline'
# provide the column headers
for c in range(0,len(collabel)-1):
table += collabel[c]
table += r'&'
table += collabel[-1]
table += r'\ hline'
# populate the table:
# this assumes the format to be celldata[index of rows][index of columns]
for r in range(0,len(rowlabel)):
table += rowlabel[r]
table += r'&'
for c in range(0,len(collabel)-2):
if not isinstance(celldata[r][c], basestring):
table += str(celldata[r][c])
else:
table += celldata[r][c]
table += r'&'
if not isinstance(celldata[r][-1], basestring):
table += str(celldata[r][-1])
else:
table += celldata[r][-1]
table += r'\ hline'
table += r'end{tabular} end{table}'
return table
# set up your data:
celldata = [[32, r'$alpha$', 123],[200, 321, 50]]
rowlabel = [r'1st row', r'2nd row']
collabel = [r' ', r'$alpha$', r'$eta$', r'$gamma$']
table = latex_table(celldata,rowlabel,collabel)
# set up the figure and subplots
fig = plt.figure()
ax1 = fig.add_subplot(121)
ax2 = fig.add_subplot(122)
ax1.plot(np.arange(100))
ax2.text(.1,.5,table, size=50)
ax2.axis('off')
plt.show()
The underlying idea of this function is to create one long string, called table
which can be interpreted as a Latex-command. It is important to import rc
and set rc('text', uestec=True)
to ensure that the string can be understood as Latex.
The string is appended using +=
; the input is as raw string, hence the r
. The example data highlights the data format.
Finally, with this example, your figure looks like this:
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…