Search code examples
python-3.xlegendcartopylegend-properties

Multiple marks on a legend


I created a legend for some marks that I plotted in an image from satellite data. I can't figure out how to have multiple marks for the different places I'm ploting.

import matplotlib.patches as mpatches

colors = ["g", "w", "y", "b", "w", "g"]
texts = ["San Luis","Tupungato", "Tierra Alta", "Tokio", "Cordoba","Sierras de Cordoba"]
patches = [plt.plot([],[], marker="o", ms=10, ls="", mec=None, color=colors[i], 
          label="{:s}".format(texts[i]) )[0]  for i in range(len(texts)) ]
plt.legend(handles=patches, bbox_to_anchor=(0.5, 0.5), 
                   loc='center right', ncol=2, facecolor="plum", numpoints=1 )

I want to have a different marks for each of the elements instead of showing all of them with circles.


Solution

  • You need to specify each plot() with varying marker option. Here is the updated code and sample output plot.

    from mpl_toolkits.basemap import Basemap
    import matplotlib.pyplot as plt
    
    colors = ["g", "w", "y", "b", "w", "g"]
    texts = ["San Luis","Tupungato", "Tierra Alta", "Tokio", "Cordoba","Sierras de Cordoba"]
    
    # a list of marker shapes
    markers = ["o", "^", "v", "<", ">", "s"]
    
    patches = [plt.plot([],[], marker=markers[i], ms=10, ls="", mec=None, color=colors[i], 
              label="{:s}".format(texts[i]) )[0]  for i in range(len(texts)) ]
    
    plt.legend(handles=patches, bbox_to_anchor=(1, 1), 
                       loc='upper right', ncol=2, facecolor="plum", numpoints=1 )
    

    enter image description here