Python 如何在matplotlib绘图中添加点图例?
我正在尝试使用matplotlib创建自定义绘图。我有图例,但对于点标记,有一条线和围绕点的白色光环。是否有任何方法可以通过移除标记后面和周围的线和光晕来仅添加点标记? 我使用的脚本是Python 如何在matplotlib绘图中添加点图例?,python,matplotlib,plot,Python,Matplotlib,Plot,我正在尝试使用matplotlib创建自定义绘图。我有图例,但对于点标记,有一条线和围绕点的白色光环。是否有任何方法可以通过移除标记后面和周围的线和光晕来仅添加点标记? 我使用的脚本是 from matplotlib.patches import Patch from matplotlib.lines import Line2D legend_elements = [Line2D([0], [0], color='b', lw=4, label='Line'),
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()
输出绘图
只需在legend\u元素的第二个参数中添加linestyle/ls=''
#更新代码
从matplotlib.patches导入修补程序
从matplotlib.lines导入Line2D
legend_elements = [Line2D([0], [0], color='b', lw=4, label='Line'),
图,ax=plt.子批次()
ax.set_facecolor('xkcd:black')
ax.legend(句柄=图例元素,loc='center')
plt.show()
您是否尝试过
color='none'
,例如Line2D([0],[0],marker='o',color='none',label='Scatter',markerfacecolor='g',markersize=10)
?请看一看。@johnC color'none'在我的点周围留下一个黑色圆圈
Line2D([0], [0], marker='o', color='w', label='location',
markerfacecolor='g', markersize=10, ls = ''),
Patch(facecolor='orange', edgecolor='r',
label='Color Patch')]