2
是否可以使用matplotlib以pandas顯示錶格的方式繪製具有多索引的表格?我在網上找到的最多的是這個open git issue from 2012。用多索引繪製matplotlib表格
考慮下面的示例數據幀:
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)
但是當我嘗試創建一個表出來的這種使用下面的例子:
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()
我得到了如下圖表(注意左邊的索引不一樣):