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