【问题标题】:How to make a 1-cell table headers with Matplotlib?如何使用 Matplotlib 制作 1 单元格表头?
【发布时间】:2019-06-12 00:47:44
【问题描述】:

我正在使用 Matplotlib 的 PdfPages 从查询的数据中绘制各种图形和表格并生成 Pdf。我想通过基本上创建节标题来按各个部分(例如“第 1 阶段”、“第 2 阶段”和“第 3 阶段”)对图进行分组。例如,在 Jupyter 笔记本中,我可以制作单元格的降价并创建粗体标题。但是,我不确定如何使用 PdfPages 做类似的事情。我的一个想法是生成一个包含部分标题的 1 个单元格表。它不是创建一个 1 个单元格的表格,而是在标题中每个字符都有一个单元格。

import matplotlib.pyplot as plt
fig = plt.figure(figsize=(12, 2))
ax = plt.subplot(111)
ax.axis('off')
tab = ax.table(cellText=['Stage 1'], bbox=[0, 0, 1, 1])
tab.auto_set_font_size(False)
tab.set_fontsize(24)

这会产生以下输出:

如果有人对如何创建节标题或至少解决我创建的表格中的单元格问题有任何建议,我将不胜感激。谢谢!

【问题讨论】:

    标签: python matplotlib pdf-generation sections pdfpages


    【解决方案1】:

    您需要使用colLabels 来命名列,并使用带有相应形状的cellText

    import matplotlib.pyplot as plt
    fig = plt.figure(figsize=(12, 2))
    ax = plt.subplot(111)
    ax.axis('off')
    
    length = 7
    colLabels = ['Stage %s' %i for i in range(1,length+1)] # <--- 1 row, 7 columns
    cellText = np.random.randint(0, 10, (1,length))
    
    tab = ax.table(cellText=cellText, colLabels=colLabels, bbox=[0, 0, 1, 1], cellLoc = 'center')
    tab.auto_set_font_size(False)
    tab.set_fontsize(14)
    

    多行表格

    cellText = np.random.randint(0, 10, (3,length)) # <--- 3 rows, 7 columns
    
    tab = ax.table(cellText=cellText, colLabels=colLabels, bbox=[0, 0, 1, 1], cellLoc = 'center')
    

    从 2 行 7 列开始获取单行多列

    tab = ax.table(cellText=[['']*length], colLabels=colLabels, bbox=[0, 0, 1, 1], cellLoc = 'center')
    cells=tab.get_celld()
    
    for i in range(length):
        cells[(1,i)].set_height(0)
    

    获取单列在上述代码中使用

    length = 1
    

    生产

    【讨论】:

    • 感谢您付出努力并使用易于理解的方法来解释有关 matplotlib 表的更多信息。
    【解决方案2】:

    一个表需要二维的cellText。 IE。 nth 行的 mth 列的内容为 cellText[n][m]。如果cellText=['Stage 1']cellText[0][0] 将评估为"S",因为只有一行,并且里面的字符串被索引为列。相反,您可能想使用

    ax.table(cellText=[['Stage 1']])
    

    即整个文本作为第一行的第一列。


    现在潜在的问题似乎是如何添加章节标题,也许使用表格不是最好的方法?使用通常的文本至少可以实现类似的结果,

    import matplotlib.pyplot as plt
    fig, ax = plt.subplots(figsize=(12, 2))
    ax.tick_params(labelleft=False, left=False, labelbottom=False, bottom=False)
    ax.annotate('Stage 1', (.5,.5), ha="center", va="center", fontsize=24)
    plt.show()
    

    【讨论】:

      【解决方案3】:

      我可能误解了您的问题,但如果您的最终目标是在 PDF 中将多个图组合在一起,则一种解决方案是使您的每个图成为同一 figuresubplot。例如:

      import matplotlib.pyplot as plt
      from matplotlib.backends.backend_pdf import PdfPages
      import random
      
      # Declare the PDF file and the single figure
      pp = PdfPages('test.pdf')
      thefig = plt.figure()
      thefig.suptitle("Group 1")
      
      # Generate 4 subplots for the same figure, arranged in a 2x2 grid
      subplots = [ ["Plot One", 221], ["Plot Two", 222],
                   ["Plot Three", 223], ["Plot Four", 224] ]
      for [subplot_title, grid_position] in subplots:
          plt.subplot(grid_position)
          plt.title(subplot_title)
          # Make a random bar graph:
          plt.bar(range(1,11), [ random.random() for i in range(10) ])
      
      # Add some spacing, so that the writing doesn't overlap
      plt.subplots_adjust(hspace=0.35, wspace=0.35)
      
      # Finish
      pp.savefig()
      pp.close()
      

      当我这样做时,我会得到如下信息:

      【讨论】:

        猜你喜欢
        • 2022-12-31
        • 2012-02-15
        • 2018-06-26
        • 2021-11-29
        • 2022-12-21
        • 1970-01-01
        • 2021-08-24
        • 2015-08-15
        • 1970-01-01
        相关资源
        最近更新 更多