Python 绘制熊猫数据框的饼图和表格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38337918/
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
Plot pie chart and table of pandas dataframe
提问by user3447653
I have to plot pie-chart and a table side by side using matplotlib.
我必须使用 matplotlib 并排绘制饼图和表格。
For drawing the pie-chart, I use the below code:
为了绘制饼图,我使用以下代码:
import matplotlib.pyplot as plt
df1.EventLogs.value_counts(sort=False).plot.pie()
plt.show()
For drawing a table, I use the below code:
为了绘制表格,我使用以下代码:
%%chart table --fields MachineName --data df_result2
df_result2 is a table with the list of MachineName's in it.
df_result2 是一个表,其中包含 MachineName 的列表。
Not sure whether we can place both pie chart and table side by side. Any help would be appreciated.
不确定我们是否可以并排放置饼图和表格。任何帮助,将不胜感激。
回答by Serenity
Look at the code:
看代码:
import pandas as pd
import matplotlib.pyplot as plt
from pandas.tools.plotting import table
# sample data
raw_data = {'officer_name': ['Jason', 'Molly', 'Tina', 'Jake', 'Amy'],
'jan_arrests': [4, 24, 31, 2, 3],
'feb_arrests': [25, 94, 57, 62, 70],
'march_arrests': [5, 43, 23, 23, 51]}
df = pd.DataFrame(raw_data, columns = ['officer_name', 'jan_arrests', 'feb_arrests', 'march_arrests'])
df['total_arrests'] = df['jan_arrests'] + df['feb_arrests'] + df['march_arrests']
plt.figure(figsize=(16,8))
# plot chart
ax1 = plt.subplot(121, aspect='equal')
df.plot(kind='pie', y = 'total_arrests', ax=ax1, autopct='%1.1f%%',
startangle=90, shadow=False, labels=df['officer_name'], legend = False, fontsize=14)
# plot table
ax2 = plt.subplot(122)
plt.axis('off')
tbl = table(ax2, df, loc='center')
tbl.auto_set_font_size(False)
tbl.set_fontsize(14)
plt.show()
回答by devm2024
A very simple solution
df.whatever.value_counts().plot(kind='pie')
一个非常简单的解决方案
df.whatever.value_counts().plot(kind='pie')