2

I would like to plot text entries in matplotlib with appropriate legend, i.e. the text entries would be like markers that should be reproduced together with some explanation in the legend.

I identified three potential ways to do this, none of which offers me the control I would like to have:

  • plot together with mathtext as marker: fine for the legend, but how can I format the text itself (e.g., avoid bold, italic, chose font family, etc.)?;
  • annotate: fine to have full control of the text, but how can I refer to a legend (with the marler appearing exactly as on the plot)?;
  • text: same as for annotate.

    import matplotlib.pyplot as plt
    
    myd = dict((k, v) for (k, v) in zip(('FOO', 'BLA', 'OOP'),
                                        ('Foofighters!', 'BlackPanther', 'Oops I did it again')))
    mycolors = ('r', 'b', 'g')
    
    fig, (ax0, ax1, ax2) = plt.subplots(1, 3, figsize=(10,4), sharex=True, sharey=True)
    
    for i, (k,v) in enumerate(myd.items()):
    
        ax0.plot(i, i, marker='$%s$' % k, ms=20, lw=0, label = v,
                 color=mycolors[i])
        ax0.legend(loc=2)
    
        ax1.annotate(k, xy=(i, i), label=v, ha='center', va='center',
                     color=mycolors[i], fontweight='extra bold')
        ax1.legend(loc=2)
    
        ax2.text(i, i, k, label=v, ha='center', va='center',
                 color=mycolors[i], family='fantasy', rotation=15)
        ax2.legend(loc=2)
    
    fig.tight_layout()
    plt.show()
    

enter image description here

1

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.