Line2D服装图例仅一个点

问题描述 投票:0回答:1

我正在尝试在Basemap绘图中制作服装图例,因此正在考虑使用Line2D。可以,但是我希望图例仅由一个标记组成并且不包含线,而不是两个标记之间的线(请参见下文)。

enter image description here

这里是我用来绘制这个服装图例的代码中最重要的部分:

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()
python matplotlib matplotlib-basemap
1个回答
1
投票

使用numpoints=调用中的legend()控制Line2D对象显示的点数。虽然仍然显示一条线。如果要删除线,则在创建Line2D时将其宽度设置为0。

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()
© www.soinside.com 2019 - 2024. All rights reserved.