【问题标题】:Matplotlib table formattingMatplotlib 表格格式化
【发布时间】:2012-03-29 19:24:26
【问题描述】:

似乎无法在文档中找到如何增加单元格的行高,因为文本本身非常狭窄。

感谢任何有关代码的帮助!表格格式似乎没有很好的记录...

    # Plot line width
    matplotlib.rc('lines', linewidth=3)

    ind = np.arange(len(overall))

    fig = pyplot.figure()
    ax = fig.add_subplot(211)
    ax.set_title('Overall Rating of Experience')
    ax.set_ylabel('Score (0-100)')

    # Plot data on chart
    plot1 = ax.plot(ind, overall)
    plot2 = ax.plot(ind, svc_avg)
    plot3 = ax.plot(ind, benchmark)

    ax.yaxis.grid(True, which='major', ls='-', color='#9F9F9F')
    ax.set_ylim([min(overall + svc_avg + benchmark) - 3, 100])
    ax.set_xlim([-.5,1.5])
    ax.get_xaxis().set_ticks([])
    ax.set_position([.25, .3, 0.7, 0.5])

    colLabels = ['July', 'August']
    rowLabels = ['Average', 'Service Average', 'Benchmark']
    cellText = [overall, svc_avg, benchmark]
    the_table = ax.table(cellText=cellText, rowLoc='right',
                         rowColours=colors, rowLabels=rowLabels,
                         colWidths=[.5,.5], colLabels=colLabels,
                         colLoc='center', loc='bottom')

编辑:感谢 Oz 的回答——循环遍历表格的属性可以轻松修改 height 属性:

    table_props = the_table.properties()
    table_cells = table_props['child_artists']
    for cell in table_cells: cell.set_height(0.1)

【问题讨论】:

  • 在我的 matplotlib 版本中,您编辑中的“儿童艺术家”需要替换为“儿童”

标签: python matplotlib


【解决方案1】:

matplotlib 文档说

将表格添加到当前坐标区。返回一个 matplotlib.table.Table 实例。要对表格进行更精细的控制,请使用 Table 类并使用 add_table() 将其添加到轴中。

你可以做以下,看看你的表的属性(它和属于该类表的对象):

print  the_table.properties() # hint it's a dictionary do: type(the_table.properties() <type 'dict'> 

以您认为正确的方式编辑该字典,并更新您的表格,使用:

the_table.update(giveHereYourDictionary)

提示:如果您使用 IPython 或交互式 shell,那么帮助(objectName)就足够了,例如help(the_table) 查看所有对象的方法。 希望这应该会奏效。

好的,我在这里添加了如何处理这类内容的演练。我承认,这不是微不足道的,但我使用 matplotlib 已经 3.5 年了,所以......

在 IPython 中编写代码(我之前说过,但我必须再次强调),它确实有助于检查对象具有的所有属性(键入对象名称,然后键入键):

In [95]: prop=the_table.properties()
In [96]: prop #This is a dictionary, it's not so trivial, but never the less one can understand how dictionaries work...
Out[96]: 
{'agg_filter': None,
 'alpha': None,
 'animated': False,
 'axes': <matplotlib.axes.AxesSubplot at 0x9eba34c>,
 'celld': {(0, -1): <matplotlib.table.Cell at 0xa0cf5ec>,
  (0, 0): <matplotlib.table.Cell at 0xa0c2d0c>,
  (0, 1): <matplotlib.table.Cell at 0xa0c2dec>,
  (0, 2): <matplotlib.table.Cell at 0xa0c2ecc>,
  (1, -1): <matplotlib.table.Cell at 0xa0cf72c>,
  (1, 0): <matplotlib.table.Cell at 0xa0c2fac>,
  (1, 1): <matplotlib.table.Cell at 0xa0cf08c>,
  (1, 2): <matplotlib.table.Cell at 0xa0cf18c>,
  (2, -1): <matplotlib.table.Cell at 0xa0cf84c>,
  (2, 0): <matplotlib.table.Cell at 0xa0cf28c>,
  (2, 1): <matplotlib.table.Cell at 0xa0cf3ac>,
  (2, 2): <matplotlib.table.Cell at 0xa0cf4cc>},
 'child_artists': [<matplotlib.table.Cell at 0xa0c2dec>,
  <matplotlib.table.Cell at 0xa0cf18c>,
  <matplotlib.table.Cell at 0xa0c2d0c>,
  <matplotlib.table.Cell at 0xa0cf84c>,
  <matplotlib.table.Cell at 0xa0cf3ac>,
  <matplotlib.table.Cell at 0xa0cf08c>,
  <matplotlib.table.Cell at 0xa0cf28c>,
  <matplotlib.table.Cell at 0xa0cf4cc>,
  <matplotlib.table.Cell at 0xa0cf5ec>,
  <matplotlib.table.Cell at 0xa0c2fac>,
  <matplotlib.table.Cell at 0xa0cf72c>,
  <matplotlib.table.Cell at 0xa0c2ecc>],
 'children': [<matplotlib.table.Cell at 0xa0c2dec>,
  <matplotlib.table.Cell at 0xa0cf18c>,
  ...snip snap ...
  <matplotlib.table.Cell at 0xa0cf72c>,
  <matplotlib.table.Cell at 0xa0c2ecc>],
 'clip_box': TransformedBbox(Bbox(array([[ 0.,  0.],
       [ 1.,  1.]])), CompositeAffine2D(BboxTransformTo(Bbox(array([[ 0.,  0.],
       [ 1.,  1.]]))), BboxTransformTo(TransformedBbox(Bbox(array([[ 0.25,  0.3 ],
       [ 0.95,  0.8 ]])), BboxTransformTo(TransformedBbox(Bbox(array([[ 0.,  0.],
       [ 8.,  6.]])), Affine2D(array([[ 80.,   0.,   0.],
       [  0.,  80.,   0.],
       [  0.,   0.,   1.]])))))))),
 'clip_on': True,
 'clip_path': None,
 'contains': None,
 'figure': <matplotlib.figure.Figure at 0x9eaf56c>,
 'gid': None,
 'label': '',
 'picker': None,
 'rasterized': None,
 'snap': None,
 'transform': BboxTransformTo(TransformedBbox(Bbox(array([[ 0.25,  0.3 ],
       [ 0.95,  0.8 ]])), BboxTransformTo(TransformedBbox(Bbox(array([[ 0.,  0.],
       [ 8.,  6.]])), Affine2D(array([[ 80.,   0.,   0.],
       [  0.,  80.,   0.],
       [  0.,   0.,   1.]])))))),
 'transformed_clip_path_and_affine': (None, None),
 'url': None,
 'visible': True,
 'zorder': 0}

# we now get all the cells ...
 [97]: cells = prop['child_artists']

In [98]: cells
Out[98]: 
[<matplotlib.table.Cell at 0xa0c2dec>,
 <matplotlib.table.Cell at 0xa0cf18c>,
... snip snap...
 <matplotlib.table.Cell at 0xa0cf72c>,
 <matplotlib.table.Cell at 0xa0c2ecc>]

In [99]:cell=cells[0]
In [100]: cell # press tab here to see cell's attributes

Display all 122 possibilities? (y or n)
cell.PAD
cell.add_callback
...snip snap ...
cell.draw
cell.eventson
cell.figure
...snip snap ...
In [100]: cell.set_h
cell.set_hatch   cell.set_height 

# this looks promising no? Hell, I love python ;-)
wait, let's examine something first ...
In [100]: cell.get_height()
Out[100]: 0.055555555555555552
In [101]: cell.set_height(0.1) # we just 'doubled' the height...
In [103]: pyplot.show()

和 TA DA:

现在,我要求您使用 for 循环更改所有单元格的高度。 应该不会那么难。 赢得那笔赏金会很高兴;-)

【讨论】:

  • 我查看了所有返回的属性,但我仍然不确定应该调整什么来更改单元格的高度。
  • @Rob,我现在更新了答案,使其更加全面。希望你喜欢!
  • 非常感谢你,奥兹。这个只是有点超出我的放大镜。
  • @Rob,没问题 :-) Matplotlib 很有趣。由于时间不够,我一开始没有给出完整的答案。
【解决方案2】:

您也可以直接在函数调用时使用 bbox(边界框)属性,因此只需在 .table 方法中添加 bbox 参数,如下所示:

the_table = ax.table(cellText=cellText, rowLoc='right',
                     rowColours=colors, rowLabels=rowLabels,
                     colWidths=[.5,.5], colLabels=colLabels,
                     colLoc='center', loc='bottom',
                     bbox=[0, -0.3, 1, 0.275]) # <-- here is the magic

bbox 是:[left, bottom, width, height]

因此您可以将表格放低一点(-0.3),并将高度设置为 0.275(

无论如何,这个想法是你可以使用bbox 参数来相当容易地移动和调整你的桌子(至少比公认的答案更容易)

如果你的表格在保存图形时碰巧消失了,你可以重新调整整个图形:

plt.tight_layout(rect=[0.11, 0.3, 0.95, .95])

遵守与 bbox (l, b, w, h) 相同的规则。 这个 (0.3) 将为斧头下方的表格留出一些空间,而 0.11 将允许 rowLabel 适合图中。您将不得不摆弄这些值,使其看起来最适合您。

这给出了(我使用fig.add_suplot(111) btw):

作为对比,同样的图不改变bbox属性:

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-06
    • 2021-07-15
    • 2014-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多