python - Matplotlib overlapping annotations / text -
i'm trying stop annotation text overlapping in graphs. method suggested in accepted answer matplotlib overlapping annotations looks extremely promising, bar graphs. i'm having trouble converting "axis" methods on want do, , don't understand how text lines up.
import sys import matplotlib.pyplot plt # start new plot plt.clf() plt.xlabel("proportional euclidean distance") plt.ylabel("percentage timewindows attended") plt.title("test plot") = [(0, 1.0, 0.4), (25, 1.0127692669427917, 0.41), (50, 1.016404709797609, 0.41), (75, 1.1043426359673716, 0.42), (100, 1.1610446924342996, 0.44), (125, 1.1685687930691457, 0.43), (150, 1.3486407784550272, 0.45), (250, 1.4013999168008104, 0.45)] together.sort() x,y,z in together: plt.annotate(str(x), xy=(y, z), size=8) eucs = [y (x,y,z) in together] covers = [z (x,y,z) in together] p1 = plt.plot(eucs,covers,color="black", alpha=0.5) plt.savefig("test.png")
images (if works) can found here (this code):
and here (more complicated):
i wanted post here solution, small library wrote implement kind of things: https://github.com/phlya/adjusttext example of process (from older version of library - i'm lazy make new one) can seen here:
here example image:
import matplotlib.pyplot plt adjusttext import adjust_text import numpy np = [(0, 1.0, 0.4), (25, 1.0127692669427917, 0.41), (50, 1.016404709797609, 0.41), (75, 1.1043426359673716, 0.42), (100, 1.1610446924342996, 0.44), (125, 1.1685687930691457, 0.43), (150, 1.3486407784550272, 0.45), (250, 1.4013999168008104, 0.45)] together.sort() text = [x (x,y,z) in together] eucs = [y (x,y,z) in together] covers = [z (x,y,z) in together] p1 = plt.plot(eucs,covers,color="black", alpha=0.5) texts = [] x, y, s in zip(eucs, covers, text): texts.append(plt.text(x, y, s)) plt.xlabel("proportional euclidean distance") plt.ylabel("percentage timewindows attended") plt.title("test plot") adjust_text(texts, only_move='y', arrowprops=dict(arrowstyle="->", color='r', lw=0.5)) plt.show()
if want perfect figure, can fiddle around little. first, let's make text repel lines - create lots of virtual points along them using scipy.interpolate.interp1d.
we want avoid moving labels along x-axis, because, well, why not illustrative purposes. use parameter only_move={'points':'y', 'text':'y'}
. if want move them along x axis in case overlapping text, use move_only={'points':'y', 'text':'xy'}
. in beginning function chooses optimal alignment of texts relative original points, want happen along y axis too, hence autoalign='y'
. reduce repelling force points avoid text flying far away due our artificial avoidance of lines. together:
from scipy import interpolate p1 = plt.plot(eucs,covers,color="black", alpha=0.5) texts = [] x, y, s in zip(eucs, covers, text): texts.append(plt.text(x, y, s)) f = interpolate.interp1d(eucs, covers) x = np.arange(min(eucs), max(eucs), 0.0005) y = f(x) plt.xlabel("proportional euclidean distance") plt.ylabel("percentage timewindows attended") plt.title("test plot") adjust_text(texts, x=x, y=y, autoalign='y', only_move={'points':'y', 'text':'y'}, force_points=0.15, arrowprops=dict(arrowstyle="->", color='r', lw=0.5)) plt.show()
Comments
Post a Comment