Line2D Costum图例仅一分

时间:2019-11-29 14:03:21

标签: python matplotlib matplotlib-basemap

我正在尝试在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()

1 个答案:

答案 0 :(得分: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()