0

I want to add an individual tag to each point in a line plot. However, these tags look very messy. Is there any automated way to arrange tags?

df = [(1, 0.02245100099048534), (2, 0.0215730708046943), (3, 0.01631299336674971), 
                     (4, 0.016984572440495845), (5, 0.014226971215895789), (6, 0.013795509799801903), 
                     (7, 0.012542395773929226), (8, 0.013900561275024762), (9, 0.011248011525647568),
                     (10, 0.01141920039207555), (11, 0.01258716267318257), (12, 0.012786334667111814), 
                     (13, 0.012111888111888111), (14, 0.010556074444144952), (15, 0.008746287867627104), 
                     (16, 0.00975691674713464), (17, 0.008427283613113125), (18, 0.008616608275731465),
                     (19, 0.009868600403467641), (20, 0.008864811622753016)]

x_2, y_2 = zip(*df)

markers_on = range(0,20)
plt.plot(x_2, y_2, '-bD', markevery=markers_on)

for label, x, y in zip(y_2, x_2, y_2):
    plt.annotate(
        round(label,4),
        xy=(x, y), xytext=(-20, 20),
        textcoords='offset points', ha='right', va='bottom',
        bbox=dict(boxstyle='round,pad=0.5', fc='yellow', alpha=0.5),
        arrowprops=dict(arrowstyle = '->', connectionstyle='arc3,rad=0')) 
Dinosaurius
  • 8,306
  • 19
  • 64
  • 113
  • 1
    What do you mean my "messy"? What is wrong with the plot you've created? You may want to add a picture of the plot. – Demetri Pananos May 25 '17 at 15:49
  • @DemetriP: tags are overlapped and it's impossible to read them. – Dinosaurius May 25 '17 at 16:06
  • Well there isn't much more you can do easily. Placing non overlapping labels is not easy. However what you can do, is that since your curve is decreasing, is to put your labels on the upper right part of the point so it has less chance to overlap the line, instead of putting the label in the upper-left part (`(-20,20)`). – jrjc May 25 '17 at 16:06
  • There is no build-in option to prevent labels from overlapping. You may want to have a look at this [additional package](https://github.com/Phlya/adjustText), which does work for text, not annotate. – ImportanceOfBeingErnest May 25 '17 at 16:26

0 Answers0