饼图matplotlib上的图例
我试图缩小图例框,并将其放置在饼图的左上角。到目前为止,我已经: pumsData=pd.DataFrame.from_csv('ss13hil.csv')) 我的图表的输出为:饼图matplotlib上的图例,matplotlib,charts,Matplotlib,Charts,我试图缩小图例框,并将其放置在饼图的左上角。到目前为止,我已经: pumsData=pd.DataFrame.from_csv('ss13hil.csv')) 我的图表的输出为: 但我希望它看起来像这样: 如果显式地将axes实例创建为对象,而不是仅使用pyplot模块中的函数,则可以更轻松地访问打印属性。使用ax.legend()中的bbox\u to\u anchor=(0.5,0.90)作为参数,可以移动图中的图例。请尝试以下代码: import matplotlib.pyplot
但我希望它看起来像这样:
如果显式地将axes实例创建为对象,而不是仅使用pyplot模块中的函数,则可以更轻松地访问打印属性。使用
ax.legend()
中的bbox\u to\u anchor=(0.5,0.90)
作为参数,可以移动图中的图例。请尝试以下代码:
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()
它应该会产生这样的结果:你是在问如何使字体变小吗?看到其他人建议使用OO api,我感到非常高兴:)@tcaswell它让一切变得更加简单
enter code here
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()