Displaying Legend in Matplotlib Basemap
<p>I have a basemap plotted using the following code snippet:</p>

<pre><code>#setting the size
plt.figure(figsize=(20,10))
#iterating through each country in shape file and filling them with color assigned using the colors array
for nshape,seg in enumerate(m.countries):
#getting x,y co-ordinates
     xx,yy = zip(*seg)
#getting the corresponding color
     color = colors[countryisos[nshape]]
#plotting them on the map
     plt.fill(xx,yy,color,edgecolor=color)
#setting the parallels with 10 degree interval from -90 to 90 degrees  
parallels = np.arange(-80.,81,10.)
m.drawparallels(parallels,labels=[True,True,True,True])
#setting the meridians with 20 degree interval from -180 to 180 degrees
meridians = np.arange(10.,351.,20.)
m.drawmeridians(meridians,labels=[True,False,False,True])
#setting the title for the plot
plt.title('Number of Disasters by Country: 1981-2014')
</code></pre>

<p>Three colours are assigned to colors array using this function.</p>

<pre><code>colors={}
#iterating through each country in shape file and setting the corresponding color
for s in countryisos:
    if countvalues[s]&lt;=200:
      colors[s] = 'orange'
    elif countvalues[s]&gt;200 and countvalues[s]&lt;=400:
      colors[s] = 'yellow'
    else:
      colors[s] = 'red'
</code></pre>

<p>How can set a legend for this plot showing three colours each defining a range? I cannot use 'label' parameter inside plot function since it is in a for loop.</p>

<p>Solved it using patches in matplotlib. I just added this code below to display the custom legend.</p>

<pre><code>#setting the legend for the plot using patch
lt = mpatches.Patch(color='orange', label='&lt;200')
btwn = mpatches.Patch(color='yellow', label='200-400')
gt = mpatches.Patch(color='red', label='&gt;400')
plt.legend(handles=[lt,btwn,gt],title='Number of disasters', loc=3)
</code></pre>

python pandas matplotlib