Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib legend for an arrow

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.

like image 596
Brian Avatar asked Mar 12 '14 10:03

Brian


2 Answers

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),
                    })
like image 65
Javier Avatar answered Sep 21 '22 11:09

Javier


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' )
like image 38
firefly Avatar answered Sep 21 '22 11:09

firefly