使用多索引绘制matplotlib表 [英] Plotting matplotlib tables with a multi-index
问题描述
是否可以使用matplotlib绘制具有多索引的表格,就像熊猫显示表格一样?我在网上找到的最多的是这个
但是,当我尝试使用以下示例从中创建表格时:
fig=plt.figure(figsize=(9.5, 11))plt.gca().axis('off')matplotlib_tab = pd.tools.plotting.table(plt.gca(),df,loc ='上部中心',colWidths=[0.25]*len(df.columns))table_props = matplotlib_tab.properties()table_cells = table_props ['child_artists']对于 table_cells 中的单元格:cell.set_height(0.024)cell.set_fontsize(12)fig.text(4.25/8.5, 10.5/11., 'plot', ha='center', fontsize=12)plt.plot()
我将以下内容绘制为表格(注意左边的索引不一样):
答案不完整由 pandas.tools.plotting.table
调用的 Matplotlib.table.table
遍历数据帧的MultiIndex,该MultiIndex返回表中所示的元组.我检查了两个函数的源代码,似乎当前它们不支持任何形式的多行或多列表.
如果表是独立的,为什么不使用例如支持多索引的df.to_html()
?
<头><tr style="text-align: right;">< th</th><th></th>第 0 次第 1 个< th> 2< th< th> 3< th</tr></thead>< tr><th rowspan="2" valign="top">bar</th><th>one</th><td>0.578703</td>< td> 1.499785</td><td>-1.144682</td>< td> 0.957464</td></tr>< tr><th>二</th>< td> 1.116768</td><td>0.291652</td>< td> -0.095254</td><td>0.131653</td></tr><tr>< th rowspan ="2" valign ="top"&b; baz</th><th>one</th><td>-1.119140</td>< td> 0.245226</td><td>0.453203</td>< td> -1.827160</td></tr><tr><th>二</th>< td> 0.442228</td>< td> 0.160754</td>< td> 1.199452</td>< td> 0.767720</td></tr>< tr><th rowspan="2" valign="top">foo</th><th>one</th>< td> 0.621110</td>< td> 0.334196</td><td>1.854065</td>< td> 0.505222</td></tr><tr><th>二</th><td>-0.269477</td>< td> 1.294712</td>< td> 0.421114</td>< td> 0.018712</td></tr>< tr>< th rowspan ="2" valign ="top"> qux</th>< one</th><td>-1.736962</td>< td> -2.627593</td>< td> -0.843875</td><td>-2.108532</td></tr><tr><二</th><td>-1.200400</td>< td> -0.319079</td>< td> 0.529251</td><td>-1.928900</td></tr></tbody></table>Is it possible to plot a table with a multi-index using matplotlib the same way pandas displays out the table? The most I found on web was this open git issue from 2012.
Given the following example data frame:
arrays = [['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'],
['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']]
tuples = list(zip(*arrays))
index = pd.MultiIndex.from_tuples(tuples, names=['first', 'second'])
df = pd.DataFrame(np.random.randn(8, 4), index=arrays)
Which prints as follows:
However when I try to create a table out of this using the following example:
fig=plt.figure(figsize=(9.5, 11))
plt.gca().axis('off')
matplotlib_tab = pd.tools.plotting.table(plt.gca(),
df,
loc='upper center',
colWidths=[0.25]*len(df.columns))
table_props=matplotlib_tab.properties()
table_cells=table_props['child_artists']
for cell in table_cells:
cell.set_height(0.024)
cell.set_fontsize(12)
fig.text(4.25/8.5, 10.5/11., 'plot', ha='center', fontsize=12)
plt.plot()
I get the following ploted as a table(Note the left index isn't the same):
解决方案 A bit of an incomplete answer
Matplotlib.table.table
that is called by pandas.tools.plotting.table
iterates through the MultiIndex of the dataframe, that returns the tuples as shown in the table. I checked the source code of both functions and it seems that it is currently they don't suppport any form of multirow or multicolumn table.
If the table is a standalone one, why not use, for example, df.to_html()
that supports multiindexes?
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th></th>
<th>0</th>
<th>1</th>
<th>2</th>
<th>3</th>
</tr>
</thead>
<tbody>
<tr>
<th rowspan="2" valign="top">bar</th>
<th>one</th>
<td>0.578703</td>
<td>1.499785</td>
<td>-1.144682</td>
<td>0.957464</td>
</tr>
<tr>
<th>two</th>
<td>1.116768</td>
<td>0.291652</td>
<td>-0.095254</td>
<td>0.131653</td>
</tr>
<tr>
<th rowspan="2" valign="top">baz</th>
<th>one</th>
<td>-1.119140</td>
<td>0.245226</td>
<td>0.453203</td>
<td>-1.827160</td>
</tr>
<tr>
<th>two</th>
<td>0.442228</td>
<td>0.160754</td>
<td>1.199452</td>
<td>0.767720</td>
</tr>
<tr>
<th rowspan="2" valign="top">foo</th>
<th>one</th>
<td>0.621110</td>
<td>0.334196</td>
<td>1.854065</td>
<td>0.505222</td>
</tr>
<tr>
<th>two</th>
<td>-0.269477</td>
<td>1.294712</td>
<td>0.421114</td>
<td>0.018712</td>
</tr>
<tr>
<th rowspan="2" valign="top">qux</th>
<th>one</th>
<td>-1.736962</td>
<td>-2.627593</td>
<td>-0.843875</td>
<td>-2.108532</td>
</tr>
<tr>
<th>two</th>
<td>-1.200400</td>
<td>-0.319079</td>
<td>0.529251</td>
<td>-1.928900</td>
</tr>
</tbody>
</table>
这篇关于使用多索引绘制matplotlib表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文
登录
关闭
扫码关注1秒登录
发送“验证码”获取
|
15天全站免登陆