I'm trying to get the text annotations in this line graph in matplotlib to show up above the line. I'm using the adjustText
library to repel text from the line, but as seen in the image, some of the text is below the line. I based my code on the accepted answer here.
How can I get each annotation to appear above the line, connected to its point by a gray line? Some of them already are like this, but others are not.
x = list(df_usa['date'][61:-1].str.slice(5,)) # strings
x_range = np.arange(0, len(x))
y = list(df_usa['growth_factor'][61:-1])
plt.figure(figsize=(20,6))
plt.plot(x, y, color="red", alpha=0.5)
plt.xticks(np.arange(0, len(x), 5))
texts = [plt.text(x_range[i], y[i], round(y[i], 2)) for i in range(len(x))]
f = interpolate.interp1d(x_range, y)
f_x = np.arange(min(x_range), max(x_range), 0.005)
f_y = f(f_x)
# Generate non-overlapping data point labels
adjust_text(texts, x=f_x, y=f_y,
only_move={'points': 'y', 'text': 'y'},
force_points=0.15,
autoalign='y',
arrowprops=dict(arrowstyle="-", color="gray", lw=0.5))
plt.show()