2015-04-15 77 views
7

我已经创建了熊猫棒图,其中显示了某些国家和地区的数量变化情况,并根据各个国家/地区设置了酒吧颜色。我使用下面的代码绘制图表。该代码是基于this question第二应答:Pandas酒吧阴谋(matplotlib)中的自定义传说

s = pd.Series(
    listOfQuantities, 
    listOfCountiesNames 
) 

''' Assign color to each country based on the continent ''' 
colormapping = {'AF':'k','AS':'r','EU':'g','OC':'r','NA':'b','SA':'y'} 
colorstring = "" 
for country in listOfCountiesNames: 
    continent = countryToContinent[country] 
    colorstring += colormapping[continent] 


pd.Series.plot(
    s, 
    kind='bar', 
    color=colorstring, 
    grid=False, 
) 

我想创建像一个我显示附加的图像在图例中(不是由蟒产生的图例,我手动添加)。是否有可能用熊猫来绘制这样的自定义图例,还是我可以实现与其他图形库类似的东西?此外,我会很欣赏这种类型的数据的替代剧情类型的建议。

enter image description here

回答

11

所以,你的序列图后,你可以添加这个

import matplotlib.patches as mpatches 
import matplotlib.pyplot as plt 

NA = mpatches.Patch(color='blue', label='North America') 
EU = mpatches.Patch(color='green', label='Europe') 
AP = mpatches.Patch(color='red', label='Asia/Pacific') 
SA = mpatches.Patch(color='yellow', label='South America') 
plt.legend(handles=[NA,EU,AP,SA], loc=2) 

plt.show()