Matplotlib表格式化

时间:2012-03-29 19:24:26

标签: python matplotlib

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

对代码的任何帮助表示赞赏!表格格式似乎没有很好地记录......

    # 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的回答 - 通过表格的属性循环可以轻松修改高度属性:

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

2 个答案:

答案 0 :(得分:33)

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),例如帮助(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:

Table with modified height for one cell

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

答案 1 :(得分:14)

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

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(<0.3)将在绘图和表格之间创建一个水平空间,同时将较高的单元格作为高度的默认值似乎小于0.15。

无论如何,我们的想法是你可以使用bbox参数轻松移动和调整你的表格(至少比接受的答案更容易)

如果您的表格在保存图片时恰好消失,您可以使用以下方法重新调整整个数字:

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

遵守与bbox(l,b,w,h)相同的规则。 这(0.3)将为ax下方的表格留出一些空间,0.11将允许rowLabel适合图中。你必须弄清楚价值观,以便它看起来最适合你。

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

Python Matplotlib table

作为比较,相同的数字没有改变bbox属性:

Python Matplotlib table bad

希望这有帮助。