Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.4k views
in Technique[技术] by (71.8m points)

python - Formatting a table that is plotted from a dataframe

I am trying to publish a figure of a table that has been created from a dataframe for use in a report.

#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 here

The output in Jupyter notebook gives a table which looks like.

enter image description here

But the actual png produced by this code looks like

enter image description here

Does anyone know the formatting for a table so I can produce a png file that looks exactly like the output where the row labels are included.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Answer was to recreate the dataframe with a default numerical index

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...