我在Ipython笔记本中有一个matplotlib饼图,旁边贴有plt.text
系列表。问题是表格被编组为系列输出,而不是一个很好的表格。我做错了什么?
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 :(得分: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')