2016-11-24 136 views
0

我在Ipython笔记本中有一张matplotlib饼图,在其旁边张贴plt.text系列表。问题是这个表格是作为系列输出格式化的,而不是一张漂亮的表格。我究竟做错了什么?Ipython - 从系列表旁边绘制系列表的饼图

sumByGroup = df['dollar charge'].groupby(df['location']).sum().astype('int') 

sumByGroup.plot(kind='pie', title='DOLLARS', autopct='%1.1f%%') 
plt.axis('off') 
plt.text(2, -0.5, sumByGroup, size=12) 

回答

0

我认为问题是,你在df['dollar change'],而不是df整体呼叫GROUPBY。试试这个,

sumByGroup = df.groupby(df['location']).sum().astype('int') 

sumByGroup.plot(y='dollar charge', kind='pie', title='DOLLARS', autopct='%1.1f%%') 
plt.axis('off') 
plt.text(2, -0.5, sumByGroup, size=12) 

完全正与由数据的例子。

import numpy as np 
import pandas as pd 
import matplotlib.pyplot as plt 


n = 20 
locations = ['MD', 'DC', 'VA', 'NC', 'NY'] 

df = pd.DataFrame({'dollar charge': np.random.randint(28, 53, n), 
        'location': np.random.choice(locations, n), 
        'Col A': np.random.randint(-5, 5, n), 
        'Col B': np.random.randint(-5, 5, n)}) 

sumByGroup = df.groupby(df['location']).sum() 

fig, ax = plt.subplots() 

sumByGroup.plot(y='dollar charge', kind='pie', title='DOLLARS', 
       autopct='%1.1f%%', legend=False, ax=ax) 
ax.axis('off') 
ax.text(2, -0.5, sumByGroup, size=12) 
ax.set_aspect('equal') 

enter image description here