I would like to know how it is possible to label an arrow
and show it in the legend of a plot.
For instance if I do this
arrow(0,1,'dummy',label='My label')
legend()
I do not see anything in the legend. I would like to see in the legend box an arrow next to its label.
You can add arbitrary artists to the legend command, as explained here
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',])
The arrow artist does not allow a marker parameter, so you'll need to do some additional manual tinkering to replace the marker in the legend.
EDIT
To get the custom marker you need to define your own handler_map
. The following code is inspired in the example here:
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),
})
You can add any latex symbol as a marker. For example: for an arrow pointing down you can specify:
scatter( x ,y, c='purple',marker=r'$\downarrow$',s=20, label='arrow' )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With