python - "Repel" annotations in matplotlib? -
i saw this package r/ggplot2, lets 1 have multiple annotations on plot , automatically adjust position minimize overlap, , way improve readability. there similar available python/matplotlib?
edit: i've found matplotlib overlapping annotations / text , looks promising, seems result inferior r package.
example:
from matplotlib import pyplot plt import numpy np xs = np.arange(10, step=0.1)+np.random.random(100)*3 ys = np.arange(10, step=0.1)+np.random.random(100)*3 labels = np.arange(100) plt.scatter(xs, ys) x, y, s in zip(xs, ys, labels): plt.text(x, y, s) plt.show()
you can see such short labels create crazy mess when data density high.
[12-11-2016 updated code , second figure again since library has been improved since then]
answer rewritten
i've made small library purpose, works above mentioned ggrepel: https://github.com/phlya/adjusttext
with switched off repelling points produces decent difficult example:
from matplotlib import pyplot plt adjusttext import adjust_text import numpy np np.random.seed(2016) xs = np.arange(10, step=0.1)+np.random.random(100)*3 ys = np.arange(10, step=0.1)+np.random.random(100)*3 labels = np.arange(100) f = plt.figure() scatter = plt.scatter(xs, ys, s=15, c='r', edgecolors='w') texts = [] x, y, s in zip(xs, ys, labels): texts.append(plt.text(x, y, s)) plt.show()
adjust_text(texts, force_points=0.2, force_text=0.2, expand_points=(1,1), expand_text=(1,1), arrowprops=dict(arrowstyle="-", color='black', lw=0.5)) plt.show()
Comments
Post a Comment