0

I am trying to make a costum legend in a Basemap plot so was considering using Line2D. It works ok, but I want that the legend only consist of one marker and no line, instead of a line between two markers (see below).

enter image description here

Here is the most important part of the code I use to plot this costum legend:

from matplotlib.lines import Line2D
import matplotlib.pyplot as plt

fig,ax = plt.subplots()
legend_elements = [Line2D([0],[0],marker='o',markersize=10,color='green',label='Label1'),
                   Line2D([0],[0],marker='s',markersize=12,color='red',label='Label2')]
ax.legend(handles=legend_elements,loc=2)
plt.show()
Bollehenk
  • 285
  • 2
  • 19

1 Answers1

3

use numpoints= in the legend() call to control the number of points shown for a Line2D object. A line is still shown though. If you want to remove the line, set its width to 0 when creating the Line2D.

from matplotlib.lines import Line2D
import matplotlib.pyplot as plt

fig,ax = plt.subplots()
legend_elements = [Line2D([0],[0],marker='o',markersize=10,color='green',label='Label1', lw=0),
                   Line2D([0],[0],marker='s',markersize=12,color='red',label='Label2', lw=0)]
ax.legend(handles=legend_elements,loc=2, numpoints=1)
plt.show()
Diziet Asahi
  • 38,379
  • 7
  • 60
  • 75