title: Adding a legend to PyPlot in Matplotlib in the simplest manner possible
body: <blockquote>
    <p><strong>TL;DR -></strong> How can one create a legend for a line graph in <code>Matplotlib</code>'s <code>PyPlot</code> without creating any extra variables?</p>
  </blockquote>
  
  <p>Please consider the graphing script below:</p>
  
  <pre><code>if __name__ == '__main__':
      PyPlot.plot(total_lengths, sort_times_bubble, 'b-',
                  total_lengths, sort_times_ins, 'r-',
                  total_lengths, sort_times_merge_r, 'g+',
                  total_lengths, sort_times_merge_i, 'p-', )
      PyPlot.title("Combined Statistics")
      PyPlot.xlabel("Length of list (number)")
      PyPlot.ylabel("Time taken (seconds)")
      PyPlot.show()
  </code></pre>
  
  <p>As you can see, this is a very basic use of <code>matplotlib</code>'s <code>PyPlot</code>. This ideally generates a graph like the one below:</p>
  
  <p><img src="https://i.stack.imgur.com/sRcuY.png" alt="Graph"></p>
  
  <p>Nothing special, I know. However, it is unclear what data is being plotted where (I'm trying to plot the data of some sorting algorithms, length against time taken, and I'd like to make sure people know which line is which). Thus, I need a legend, however, taking a look at the following example below(<a href="http://matplotlib.org/users/legend_guide.html" rel="noreferrer">from the official site</a>):</p>
  
  <pre><code>ax = subplot(1,1,1)
  p1, = ax.plot([1,2,3], label="line 1")
  p2, = ax.plot([3,2,1], label="line 2")
  p3, = ax.plot([2,3,1], label="line 3")
  
  handles, labels = ax.get_legend_handles_labels()
  
  # reverse the order
  ax.legend(handles[::-1], labels[::-1])
  
  # or sort them by labels
  import operator
  hl = sorted(zip(handles, labels),
              key=operator.itemgetter(1))
  handles2, labels2 = zip(*hl)
  
  ax.legend(handles2, labels2)
  </code></pre>
  
  <p>You will see that I need to create an extra variable <code>ax</code>. How can I add a legend to my graph <em>without</em> having to create this extra variable and retaining the simplicity of my current script?</p>