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.
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
Post a Comment