本文介绍了绘制饼图和 pandas 数据框表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我必须使用matplotlib并排绘制饼图和表格.
I have to plot pie-chart and a table side by side using 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是一个包含MachineName列表的表.
df_result2 is a table with the list of MachineName's in it.
不确定是否可以同时放置饼图和表格.任何帮助将不胜感激.
Not sure whether we can place both pie chart and table side by side. Any help would be appreciated.
推荐答案
查看代码:
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()
这篇关于绘制饼图和 pandas 数据框表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!