饼图matplotlib上的传奇

时间:2016-05-03 00:51:24

标签: matplotlib charts

我正在尝试使我的传奇盒子更小,并放在饼图的左上角。到目前为止,我有: pumsData = pd.DataFrame.from_csv('ss13hil.csv')

pieLabels = ['English Only','Spanish','Other Indo-European','Asian and Pacific Island Languages','Other']  
plt.pie(pumsData.HHL.value_counts())  
plt.axis('equal')  
plt.title('Household Languages')  
plt.legend(pieLabels,loc=2,borderpad=0.05)  

我的图表的输出是:
Tomcat 8 Filter Configuration

http://i.stack.imgur.com/WbO4U.png

但我希望它看起来像这样:

enter image description here

http://i.stack.imgur.com/cBxLz.png

enter code here

1 个答案:

答案 0 :(得分:1)

如果明确地将轴实例创建为对象而不是仅使用pyplot模块中的函数,则可以更轻松地访问绘图属性。在bbox_to_anchor=(0.5,0.90)中使用ax.legend()作为参数,您可以移动图中的图例。试试这段代码:

import matplotlib.pyplot as plt

pieLabels = ['English Only',
             'Spanish',
             'Other Indo-European',
             'Asian and Pacific Island Languages',
             'Other']  

fig, ax  = plt.subplots(1,1,figsize=(4,4))

ax.pie([1,2,3,4,5], labels=pieLabels, labeldistance=9999999)
ax.set(adjustable='box-forced', aspect='equal')
ax.set_title('Household Languages')

handles, labels = ax.axes.get_legend_handles_labels()
ax.legend(handles, labels, prop={'size':6},
          bbox_to_anchor=(0.5,0.90),
          bbox_transform=fig.transFigure)

plt.show()

它应该产生这个数字:enter image description here

相关问题