【问题标题】:Making a table in python在python中制作表格
【发布时间】:2015-06-03 11:52:33
【问题描述】:

我正在尝试在 python 中创建一个表格,我可以将其放入我正在用 LaTeX 编写的报告中。我尝试使用 matplotlib 来执行此操作,但我得到以下信息...

如果有人知道如何修复我的代码,或者如果有更好的方法,我将不胜感激。这是我用来生成它的代码:

columns=('Temporary ID','ID')
tabledata=[]
for i in range(0,len(ID_mod)):
    tabledata.append([ID_mod[i],sort_ID2[i]])
plt.figure()
plt.table(cellText=tabledata,colLabels=columns)
plt.savefig('table.png')

【问题讨论】:

  • 我刚刚意识到 plt.table 命令正在将表格添加到轴上。我现在完全不知道如何制作我想要的表格
  • 您应该使用 tkinter 或 pyside 等 GUI 库。当您搜索这些库时,已经有很多关于这个主题的问题。根据个人经验,Pyside 和 PyQt(都使用 QT 框架)都有相对容易使用的表格小部件。
  • 你看过this example吗?

标签: python matplotlib


【解决方案1】:

我强烈建议改用 LaTeX 的内置表格支持。还有许多美化表格的选项,例如 booktabs 包。更多信息和选项here

【讨论】:

    【解决方案2】:

    以下是一些生成LaTeX表格的简单示例函数;

    In [1]: %cpaste
    Pasting code; enter '--' alone on the line to stop or use Ctrl-D.
    :def header(align, caption, label=None, pos='!htbp'):
    :    """
    :    Return the start for a standard table LaTeX environment that contains a
    :    tabular environment.
    :
    :    Arguments:
    :    align -- a string containing the LaTeX alignment directives for the columns.
    :    caption -- a string containing the caption for the table.
    :    label -- an optional label. The LaTeX label will be tb:+label.
    :    pos -- positioning string for the table
    :    """
    :    rs =  r'\begin{table}['+pos+']\n'
    :    rs +=  '  \\centering\n'
    :    if label:
    :        rs += r'  \caption{\label{tb:'+str(label)+r'}'+caption+r'}'+'\n'
    :    else:
    :        rs += r'  \caption{'+caption+r'}'+'\n'
    :    rs += r'  \begin{tabular}{'+align+r'}'
    :    return rs
    :
    :def footer():
    :    """
    :    Return the end for a standard table LaTeX environment that contains a
    :    tabular environment.
    :    """
    :    rs =  r'  \end{tabular}'+'\n'
    :    rs += r'\end{table}'
    :    return rs
    :
    :def line(*args):
    :    """
    :    Return the arguments as a line in the table, properly serparated and
    :    closed with a double backslash.
    :    """
    :    rs = '  '
    :    sep = r' & '
    :    for n in args:
    :        rs += str(n)+sep
    :    rs = rs[:-len(sep)]+r'\\'
    :    return rs
    :--
    

    如果你打印函数的输出,你会看到它是 LaTeX 代码;

    In [2]: print(header('rr', 'Test table'))
    \begin{table}[!htbp]
      \centering
      \caption{Test table}
      \begin{tabular}{rr}
    
    In [3]: print(line('First', 'Second'))
      First & Second\\
    
    In [4]: print(line(12, 27))
      12 & 27\\
    
    In [5]: print(line(31, 9))
      31 & 9\\
    
    In [6]: print(footer())
      \end{tabular}
    \end{table}
    

    运行相同的代码,但不打印返回值;

    In [7]: header('rr', 'Test table')
    Out[7]: '\\begin{table}[!htbp]\n  \\centering\n  \\caption{Test table}\n  \\begin{tabular}{rr}'
    
    In [8]: line('First', 'Second')
    Out[8]: '  First & Second\\\\'
    
    In [9]: line(12, 27)
    Out[9]: '  12 & 27\\\\'
    
    In [10]: line(31, 9)
    Out[10]: '  31 & 9\\\\'
    
    In [11]: footer()
    Out[11]: '  \\end{tabular}\n\\end{table}'
    

    将这些函数的输出写入文件,并使用 LaTeX 中的\input 机制将其包含在您的主 LaTeX 文件中。

    【讨论】:

      猜你喜欢
      • 2011-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多