Answer a question

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.

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),
                    })
Logo

Python社区为您提供最前沿的新闻资讯和知识内容

更多推荐