0

I want to create a legend for arrows of diferent widths that is independent from elements in the plot, so I am using a proxy artist with matplotlib.patch.FancyArrow

I took inspiration from this answer on using HandlerPatch but I can't change the arrow widths. Instead of having a thin and a thick arrow as specified in the arrows handle, I get two arrows with default thickness.

Edit: Width can be specified in the Handler function but then it will be the same for both arrows. I want to specify different marker's properties.

import matplotlib.pyplot as plt
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.50*height,
                            15,
                            0,
                            head_width=width,
                            head_length=10,
                           )
    
    return p

f = plt.figure()
arrows = [mpatches.FancyArrow(0, 0, 0, 0, fc='blue',
                              width=10),
          mpatches.FancyArrow(0, 0, 0, 0, fc='red',
                              width=2)]

plt.legend(handles=arrows, 
           labels=['thick','thin'], 
           handler_map={mpatches.FancyArrow : HandlerPatch(patch_func=make_legend_arrow),
                    })

My result

tiagoams
  • 642
  • 8
  • 18
  • Does the answer you are referring to have the wrong width of the arrow in the function? `p=mpatches.FancyArrow(0,0.50*height,width,0,head_width=0.75*height,length_includes_head=True)` – r-beginners Oct 07 '21 at 12:57
  • @r-beginners if the parameter width is set in the handler function then it will be the same for both arrows in my example. And I don't see a way of passing a different arrow width to the handler. – tiagoams Oct 07 '21 at 13:03
  • Did you mean to make the different arrow widths? I didn't understand it well. I also tried to run the code and it doesn't pass a width value, so why don't you specify the desired width directly in arrows? – r-beginners Oct 07 '21 at 13:35
  • That is what my code is doing with "width=10" and "width=2" but it has no effect. – tiagoams Oct 07 '21 at 13:38
  • How about coding directly in arrows instead of using handler_map? – r-beginners Oct 07 '21 at 13:41
  • As per @Javier's answer to the linked question "The arrow artist does not allow a marker parameter" so only two patches wil be drawn. But thaks for the suggestions :) – tiagoams Oct 08 '21 at 08:37

0 Answers0