Matplotlib表格式 [英] Matplotlib table formatting
问题描述
由于文本本身非常局促,因此似乎无法在文档中找到如何增加单元格的行高.
Can't seem to locate in the documentation how to increase the line-height of the cells, as the text itself is very cramped.
感谢您提供任何有关代码的帮助!表格式似乎没有很好的记录...
Any help with code is appreciated! Table formatting doesn't seem to be well documented...
# 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属性:
Thanks to Oz for the answer-- Looping through the properties of the table allows easy modification of the height property:
table_props = the_table.properties()
table_cells = table_props['child_artists']
for cell in table_cells: cell.set_height(0.1)
推荐答案
matplotlib文档说
The matplotlib documentation says
向当前轴添加表格.返回matplotlib.table.Table实例.为了对表进行更精细的控制,请使用Table类,然后使用add_table()将其添加到轴中.
Add a table to the current axes. Returns a matplotlib.table.Table instance. For finer grained control over tables, use the Table class and add it to the axes with add_table().
您可以执行以下操作,查看表的属性(该属性和属于该类Table的对象):
You could do is the following, look at the properties of your table (it's and object belonging to that class Table):
print the_table.properties() # hint it's a dictionary do: type(the_table.properties() <type 'dict'>
按照您认为正确的方式编辑该词典,并使用以下方法更新表格:
edit that dictionary the way you see right, and the update your table, with:
the_table.update(giveHereYourDictionary)
提示:如果您使用IPython或交互式shell,则足以完成help(objectName),例如help(the_table)查看所有对象的方法. 希望应该可以.
Hint: if you work with IPython or interactive shell it's enough to do help(objectName), e.g. help(the_table) to see all the object's methods. This should, hopefully, work.
好的,我在这里向您介绍如何处理此类内容.我承认,这并不简单,但是我使用matplotlib已有3.5年了,所以...
OK, I'm adding here a walk through of how to to that kind of stuff. I admit, it's not trivial, but I am using matplotlib for 3.5 years now, so ...
在IPython中执行您的代码(我之前说过,但是我必须再次强调),它确实有助于检查对象具有的所有属性(键入对象名称,然后键入键):
Do your code in IPython (I said it before, but I must emphasize again), it really helps to examine all the properties that objects have (type object name and then the key):
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循环来更改所有单元格的高度. 不应该那么难. 赢取赏金会很高兴;-)
Now, I challege you to change the height of all the cells, using a for loop. Should not be so hard. Would be nice to win that bounty ;-)
这篇关于Matplotlib表格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!