Bri*_*ian 12 python matplotlib
我想知道如何标记arrow并在图表的图例中显示它.
例如,如果我这样做
arrow(0,1,'dummy',label='My label')
legend()
Run Code Online (Sandbox Code Playgroud)
我没有看到传说中的任何内容.我想在图例框中看到其标签旁边的箭头.
您可以添加任意艺人传说命令,如解释在这里
import matplotlib.pyplot as plt
f = plt.figure()
arrow = plt.arrow(0, 0, 0.5, 0.6, 'dummy',label='My label')
plt.legend([arrow,], ['My label',])
Run Code Online (Sandbox Code Playgroud)
箭头艺术家不允许使用标记参数,因此您需要进行一些额外的手动修补以替换图例中的标记.
编辑
要获得自定义标记,您需要定义自己的标记handler_map.以下代码的灵感来自此处的示例:
from matplotlib.legend_handler import HandlerPatch
import matplotlib.patches as mpatches
def make_legend_arrow(legend, orig_handle,
xdescent, ydescent,
width, height, fontsize):
p = mpatches.FancyArrow(0, 0.5*height, width, 0, length_includes_head=True, head_width=0.75*height )
return p
f = plt.figure(figsize=(10,6))
arrow = plt.arrow(0,0, 0.5, 0.6, 'dummy', label='My label', )
plt.legend([arrow], ['My label'], handler_map={mpatches.FancyArrow : HandlerPatch(patch_func=make_legend_arrow),
})
Run Code Online (Sandbox Code Playgroud)
您可以添加任何乳胶符号作为标记。例如:对于向下的箭头,您可以指定:
scatter( x ,y, c='purple',marker=r’$\downarrow$’,s=20, label='arrow' )
Run Code Online (Sandbox Code Playgroud)