fig, ax = plt.subplots(1, 1)
l, = ax.plot(x, y)
pos = [(x[-2] + x[-1]) / 2.0, (y[-2] + y[-1]) / 2.0]
xscreen = ax.transData.transform(zip(x[-2::], y[-2::]))
rot = np.rad2deg(np.arctan2(*np.abs(np.gradient(xscreen)[0][0][::-1])))
ltex = plt.text(pos[0], pos[1], line_string, size=9, rotation=rot, color=l.
    get_color(), ha='center', va='center', bbox=dict(ec='1', fc='1'))

def updaterot(event):
    """Event to update the rotation of the labels"""
xs = ax.transData.transform(zip(x[-2::], y[-2::]))
rot = np.rad2deg(np.arctan2(*np.abs(np.gradient(xs)[0][0][::-1])))
ltex.set_rotation(rot)
fig.canvas.mpl_connect('button_release_event', updaterot)
plt.show()
Печатать строку поверх линии графика (имитировать метки контурного графика)