如何在 Matplotlib 中只绘制一个 table?

How do I plot only a table in Matplotlib?

是否可以用matplotlib只画一个table?如果我取消注释行

plt.bar(index, data[row], bar_width, bottom=y_offset, color=colors[row])

这个例子code,情节仍然可见。我想在我的 (PyQt) window 之上和情节之下(中间有一些 space)有一个 table。

如果您只是想更改 example 并将 table 放在顶部,那么 table 声明中的 loc='top' 就是您所需要的,

the_table = ax.table(cellText=cell_text,
                      rowLabels=rows,
                      rowColours=colors,
                      colLabels=columns,
                      loc='top')

然后调整剧情,

plt.subplots_adjust(left=0.2, top=0.8)

一个更灵活的选择是使用子图将 table 放在它自己的轴上,

import numpy as np
import matplotlib.pyplot as plt


fig, axs =plt.subplots(2,1)
clust_data = np.random.random((10,3))
collabel=("col 1", "col 2", "col 3")
axs[0].axis('tight')
axs[0].axis('off')
the_table = axs[0].table(cellText=clust_data,colLabels=collabel,loc='center')

axs[1].plot(clust_data[:,0],clust_data[:,1])
plt.show()

看起来像这样,

然后您可以自由调整轴的位置 required

不确定是否已经回答了这个问题,但是如果您只想在图 window 中使用 table,那么您可以隐藏坐标轴:

fig, ax = plt.subplots()

# Hide axes
ax.xaxis.set_visible(False) 
ax.yaxis.set_visible(False)

# Table from Ed Smith answer
clust_data = np.random.random((10,3))
collabel=("col 1", "col 2", "col 3")
ax.table(cellText=clust_data,colLabels=collabel,loc='center')

这是将 pandas 数据帧直接写入 matplotlib 的另一种选择 table:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

fig, ax = plt.subplots()

# hide axes
fig.patch.set_visible(False)
ax.axis('off')
ax.axis('tight')

df = pd.DataFrame(np.random.randn(10, 4), columns=list('ABCD'))

ax.table(cellText=df.values, colLabels=df.columns, loc='center')

fig.tight_layout()

plt.show()

你可以这样做:

#axs[1].plot(clust_data[:,0],clust_data[:,1]) # Remove this if you don't need it
axs[1].axis("off")  # This will leave the table alone in the window