python - Custom legend in Pandas bar plot (matplotlib) -


i have created bar plot pandas show how quantity change countries , set bar color according each country's continent. plot graph using following code. code based on second reply of this question:

s = pd.series(      listofquantities,      listofcountiesnames )  ''' assign color each country based on continent ''' colormapping = {'af':'k','as':'r','eu':'g','oc':'r','na':'b','sa':'y'} colorstring = "" country in listofcountiesnames:     continent = countrytocontinent[country]     colorstring += colormapping[continent]   pd.series.plot(     s,     kind='bar',     color=colorstring,     grid=false, ) 

i want create legend 1 show in attached image (the legend wasn't generated python, added manually). possible draw such custom legends pandas, or can achieve similar other graphing libraries? i'd appreciate suggestions alternative plot types such type of data.

enter image description here

so after series plot add this

import matplotlib.patches mpatches import matplotlib.pyplot 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() 

Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

datatable - Matlab struct computations -