我正在尝试发布从数据帧创建的表的图形,以便在报告中使用。
#Hard Coding example
TopTenSites = 238
RestOfSites = 387
#Creating a dataframe to pass into
Proportion = pd.DataFrame({'Incidence': [TopTenSites, RestOfSites]},
index=['Top 10 Sites', 'Rest of Sites'])
#Creating the table
fig, plot = plt.subplots(figsize = [3,2])
#plot.axis('off')
#Remove axis
plot.xaxis.set_visible(False)
plot.yaxis.set_visible(False)
#Creating the table
plot.table(cellText = Proportion.values, rowLabels = Proportion.index, loc = 'center', colLabels =
Proportion.columns,cellLoc = 'center')
plt.savefig('Proportion.png')enter code hereJupyter notebook中的输出给出了一个如下所示的表格。

但此代码实际生成的png如下所示

有没有人知道表格的格式,这样我就可以生成一个png文件,它看起来与包含行标签的输出完全一样。
发布于 2021-01-13 05:29:44
答案是使用默认的数字索引重新创建数据帧
#Creating a dataframe to pass into
Proportion = pd.DataFrame({'Sites': ['Top 10 Sites', 'Rest of Sites'],'Incidence':
[TopTenSites, RestOfSites]})
#Creating the table plot
fig, plot = plt.subplots(figsize = [3,2])
plot.axis('off')
#Remove axis
plot.xaxis.set_visible(False)
plot.yaxis.set_visible(False)
#Creating the table
plot.table(cellText = Proportion.values,loc = 'center', colLabels =
Proportion.columns, cellLoc = 'center')
plt.savefig('Proportion.png')https://stackoverflow.com/questions/65691654
复制相似问题