Gokul Anand
Gokul Anand

Reputation: 197

How do I add a point legend in the matplotlib plot?

I am trying to make a custom plot using matplotlib. I have got the legends but for the point marker there is a line and white halo around the point. Is there any way where I can add only the point marker by removing the line and halo behind and around the marker? The script I used is.

from matplotlib.patches import Patch
from matplotlib.lines import Line2D

legend_elements = [Line2D([0], [0], color='b', lw=4, label='Line'),
                   Line2D([0], [0], marker='o', color='w', label='location',
                          markerfacecolor='g', markersize=10),
                   Patch(facecolor='orange', edgecolor='r',
                         label='Color Patch')]

# Create the figure
fig, ax = plt.subplots()
ax.set_facecolor('xkcd:black')

ax.legend(handles=legend_elements, loc='center')

plt.show()

Output plot

enter image description here

enter image description here

Upvotes: 1

Views: 1735

Answers (1)

Ankit Singh
Ankit Singh

Reputation: 86

Just add linestyle/ls = '' in 2nd argument of legend_element

#Updated code

from matplotlib.patches import Patch

from matplotlib.lines import Line2D

                legend_elements = [Line2D([0], [0], color='b', lw=4, label='Line'),
               Line2D([0], [0], marker='o', color='w', label='location',
                      markerfacecolor='g', markersize=10, ls = ''),
               Patch(facecolor='orange', edgecolor='r',
                     label='Color Patch')]

fig, ax = plt.subplots() ax.set_facecolor('xkcd:black')

ax.legend(handles=legend_elements, loc='center')

plt.show()

output image

Upvotes: 1

Related Questions