2016-11-24 134 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