title: Inline labels in Matplotlib
body: <p>In Matplotlib, it's not too tough to make a legend (<code>example_legend()</code>, below), but I think it's better style to put labels right on the curves being plotted (as in <code>example_inline()</code>, below). This can be very fiddly, because I have to specify coordinates by hand, and, if I re-format the plot, I probably have to reposition the labels. Is there a way to automatically generate labels on curves in Matplotlib? Bonus points for being able to orient the text at an angle corresponding to the angle of the curve.</p>
  
  <pre><code>import numpy as np
  import matplotlib.pyplot as plt
  
  def example_legend():
      plt.clf()
      x = np.linspace(0, 1, 101)
      y1 = np.sin(x * np.pi / 2)
      y2 = np.cos(x * np.pi / 2)
      plt.plot(x, y1, label='sin')
      plt.plot(x, y2, label='cos')
      plt.legend()
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/l50U7.png" alt="Figure with legend"></p>
  
  <pre><code>def example_inline():
      plt.clf()
      x = np.linspace(0, 1, 101)
      y1 = np.sin(x * np.pi / 2)
      y2 = np.cos(x * np.pi / 2)
      plt.plot(x, y1, label='sin')
      plt.plot(x, y2, label='cos')
      plt.text(0.08, 0.2, 'sin')
      plt.text(0.9, 0.2, 'cos')
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/wDyVf.png" alt="Figure with inline labels"></p>