Am1rr3zA
Am1rr3zA

Reputation: 7421

Set annotation for same coordinate points matplotlib

I have 12 different points and 10 of them are related to the first two; I want to set label for each of this 10 points individually, but sometimes two or more of them have the same coordinate yet I want to show all the label for that coordinate (not on top of each other but readable)
As you can see in the below picture two set of points have the same coordinate and the label of them have overlapping

booleanFunction = np.array(["K","I","H" ,"G", "F", "E" , "D" , "M", "B", "A"])
pointsx = np.empty((rs.shape[1],1))
pointsy = np.empty((rs.shape[1],1))
....
....
....
pl.figure()
pl.hold(True)
pl.plot(X1, Y1, 'ro', X2, Y2, 'y<')
pl.plot(pointsx, pointsy, 'b3')

for i in range (len(pointsx)):
    pl.annotate(booleanFunction[i], xy=(pointsx[i], pointsy[i]), xycoords='data', textcoords='data')

dd

Upvotes: 1

Views: 499

Answers (1)

Francesco Montesano
Francesco Montesano

Reputation: 8658

I one of my codes to avoid annotation overlap I do something like this:

xoffset = 0.1
switch = -1
for i in range (len(pointsx)):
    pl.annotate(booleanFunction[i], xy=(pointsx[i], pointsy[i]), 
                xytext=(pointsx[i]+switch*xoffset, pointsy[i]),
                xycoords='data', textcoords='data')

    switch*=-1

This writes your annotated text alternatively shifted left and right xoffset from the point you want to annotate. Of course you can use something similar for the y direction or for both.

Upvotes: 4

Related Questions