Python 如何在 Matplotlib 中仅绘制表格?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32137396/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 11:04:43  来源:igfitidea点击:

How do I plot only a table in Matplotlib?

pythonmatplotlibpyqt

提问by snowflake

Is it possible to draw only a table with matplotlib? If I uncomment the line

是否可以仅使用 matplotlib 绘制表格?如果我取消注释该行

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

of this example code, the plot is still visible. I want to have a table on top of my (PyQt) window and underneath a plot (with some space in between).

在此示例代码中,绘图仍然可见。我想在我的(PyQt)窗口顶部和绘图下方(中间有一些空间)有一个表格。

采纳答案by Ed Smith

If you just wanted to change the exampleand put the table at the top, then loc='top'in the table declaration is what you need,

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

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

Then adjusting the plot with,

然后调整情节,

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

A more flexible option is to put the table in its own axis using subplots,

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

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()

which looks like this,

看起来像这样,

enter image description here

在此处输入图片说明

You are then free to adjust the locations of the axis as required.

然后您可以根据需要自由调整轴的位置。

回答by Ryszard Cetnarski

Not sure if this is already answered, but if you want only a table in a figure window, then you can hide the axes:

不确定这是否已经得到回答,但如果您只想要图形窗口中的表格,则可以隐藏轴:

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')

回答by Cord Kaldemeyer

This is another option to write a pandas dataframe directly into a matplotlib table:

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

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()

enter image description here

在此处输入图片说明

回答by Apostolos

You can di this:

你可以这样做:

#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