sno*_*ake 23 python pyqt matplotlib
是否可以仅使用matplotlib绘制表格?如果我取消注释该行
plt.bar(index, data[row], bar_width, bottom=y_offset, color=colors[row])
Run Code Online (Sandbox Code Playgroud)
在此示例代码中,该图仍然可见.我希望在我的(PyQt)窗口顶部和一个绘图下面有一个表(中间有一些空格).
Ed *_*ith 25
如果您只是想更改示例并将表放在顶部,那么loc='top'在表中声明就是您所需要的,
the_table = ax.table(cellText=cell_text,
rowLabels=rows,
rowColours=colors,
colLabels=columns,
loc='top')
Run Code Online (Sandbox Code Playgroud)
然后调整图,用
plt.subplots_adjust(left=0.2, top=0.8)
Run Code Online (Sandbox Code Playgroud)
更灵活的选择是使用子图将表放在自己的轴上,
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()
Run Code Online (Sandbox Code Playgroud)
看起来像这样,
然后,您可以根据需要自由调整轴的位置.
Cor*_*yer 25
这是将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()
Run Code Online (Sandbox Code Playgroud)
不知道这是否已经回答,但是如果您只想在图形窗口中使用表格,则可以隐藏轴:
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')
Run Code Online (Sandbox Code Playgroud)