简体   繁体   中英

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

在此处输入图像描述

在此处输入图像描述

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM