是否可以使用 matplotlib 绘制具有多索引的表格,就像 pandas 显示表格一样?我在网上找到最多的是这个打开 2012 年的 git 问题.
给出以下示例数据框:
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:
![enter image description here](https://i.stack.imgur.com/3DKY0.png)
但是,当我尝试使用以下示例创建一个表时:
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()
我将以下内容绘制为表格(请注意,左侧索引不相同):
![enter image description here](https://i.stack.imgur.com/yXPGe.png)