Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.9k 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 Reply

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...