I would like to label the lines of my plots on a secondary y-axis to the right of my plot, with the label of each line at the height of its final point, as well as the color of that line as shown in this image in matplotlib.
I have tried using an annotate solution based on this answer, however this places the label inside of the plot and overlaps for certain points.
import matplotlib.pyplot as plt
import numpy as np
l = ['label {:d}'.format(i) for i in range(1,5)]
x = [0,1]
yy = np.transpose([np.arange(1,5),np.arange(1,5)])
l2 = 'overlap'
y2 = [3.95,3.95]
fig, ax = plt.subplots()
for y,l in zip(yy,l):
ax.plot(x,y)
ax.annotate(xy=(x[-1],y[-1]), xytext=(20,0), textcoords='offset points', text=l, va='center')
ax.plot(x,y2)
ax.annotate(xy=(x[-1],y2[-1]), xytext=(20,0), textcoords='offset points', text=l2, va='center')
plt.show()
I am assuming there is a better way to accomplish this goal, could you please point me towards a solution?
Edited sample code to highlight specific overlap issue.