serhii.net

In the middle of the desert you can say anything you want

29 May 2023

Seaborn matplotlib labeling data points

Trivial option: Label data points with Seaborn & Matplotlib | EasyTweaks.com

TL;DR

for i, label in enumerate (data_labels):
    ax.annotate(label, (x_position, y_position))

BUT! Overlapping texts are sad: 2023-05-29-222417_447x384_scrot.png

SO sent me to the library Home · Phlya/adjustText Wiki and it’s awesome

fig, ax = plt.subplots()
plt.plot(x, y, 'bo')
texts = [plt.text(x[i], y[i], 'Text%s' %i, ha='center', va='center') for i in range(len(x))]
# adjust_text(texts)
adjust_text(texts, arrowprops=dict(arrowstyle='->', color='red'))

Not perfect but MUCH cleaner: 2023-05-29-222524_501x407_scrot.png 2023-05-29-222542_527x399_scrot.png

More advanced tutorial: adjustText/Examples.ipynb at master · Phlya/adjustText · GitHub

Pypy doesn’t have the latest version, which has:

Nel mezzo del deserto posso dire tutto quello che voglio.
comments powered by Disqus