我正在尝试在Basemap
情节中制作服装图例,因此考虑使用Line2D
。一切正常,但我希望图例仅由一个标记组成并且没有线条,而不是由两个标记之间的线条组成。(见下文)。
这是我用来绘制这个服装图例的代码中最重要的部分:
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()
答案 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()