title: Subheadings for categories within matplotlib custom legend
body: <p>I have a a figure that looks like this:</p>
  
  <p><img src="https://i.stack.imgur.com/foEVO.png" alt="enter image description here"></p>
  
  <p>I'd like to make a legend that looks like this:</p>
  
  <p><img src="https://i.stack.imgur.com/3OhdH.png" alt="enter image description here"></p>
  
  <p>How can I do that?</p>
  
  <hr>
  
  <p>UPDATE:</p>
  
  <p>Note that this legend has a frame with an edgecolor: a valid answer will include this.  The legend should also be embedded in the axes.</p>
  
  <p>The legend I want might not be achievable using <code>ax.legend()</code>.  A great answer would be one which shows how to build my desired legend (exactly as shown) manually with patches and texts, or whatever matplotlib methods that make sense.</p>