title: How to put the legend on first subplot of seaborn.FacetGrid?
body: <p>I have a pandas DataFrame <code>df</code> which I visualize with subplots of a <a href="http://stanford.edu/~mwaskom/software/seaborn/generated/seaborn.barplot.html" rel="nofollow noreferrer"><code>seaborn.barplot</code></a>. <em>My problem is that I want to move my legend inside one of the subplots.</em></p>
  
  <p>To create subplots based on a condition (in my case <em>Area</em>), I use <a href="http://stanford.edu/~mwaskom/software/seaborn/generated/seaborn.FacetGrid.html#seaborn.FacetGrid" rel="nofollow noreferrer"><code>seaborn.FacetGrid</code></a>. This is the code I use:</p>
  
  <pre><code>import matplotlib.pyplot as plt
  import matplotlib
  import seaborn as sns
  # .. load data
  grid = sns.FacetGrid(df, col="Area", col_order=['F1','F2','F3'])
  bp = grid.map(sns.barplot,'Param','Time','Method')
  bp.add_legend()
  bp.set_titles("{col_name}")
  bp.set_ylabels("Time (s)")
  bp.set_xlabels("Number")
  sns.plt.show()
  </code></pre>
  
  <p>Which generates this plot:</p>
  
  <p><a href="https://i.stack.imgur.com/iJrij.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/iJrij.png" alt="barplot-all"></a></p>
  
  <p>You see that the legend here is totally at the right, but I would like to have it inside one of the plots (for example the left one) since my original data labels are quite long and the legend occupies too much space. This is the example for only 1 plot where the legend is inside the plot:</p>
  
  <p><a href="https://i.stack.imgur.com/s1KS2.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/s1KS2.png" alt="barplot1"></a></p>
  
  <p>and the code:</p>
  
  <pre><code>mask = df['Area']=='F3'
  ax=sns.barplot(x='Param',y='Time',hue='Method',data=df[mask])
  sns.plt.show()
  </code></pre>
  
  <hr>
  
  <p><strong>Test 1</strong>:
  I tried the example of an <a href="https://stackoverflow.com/a/32301804/1771479">answer</a> where they have the legend in one of the subplots: </p>
  
  <pre><code>grid = sns.FacetGrid(df, col="Area", col_order=['F1','F2','F3'])
  bp = grid.map(sns.barplot,'Param','Time','Method')
  Ax = bp.axes[0]
  Boxes = [item for item in Ax.get_children()
       if isinstance(item, matplotlib.patches.Rectangle)][:-1]
  legend_labels  = ['So1', 'So2', 'So3', 'So4', 'So5']
  # Create the legend patches
  legend_patches = [matplotlib.patches.Patch(color=C, label=L) for
                C, L in zip([item.get_facecolor() for item in Boxes],
                            legend_labels)]
  
  # Plot the legend
  plt.legend(legend_patches)    
  sns.plt.show()
  </code></pre>
  
  <p>Note that I changed <code>plt.legend(handles=legend_patches)</code> did not work for me therefore I use <code>plt.legend(legend_patches)</code> as commented in this <a href="https://stackoverflow.com/a/25551618/1771479">answer</a>. The result however is:</p>
  
  <p><a href="https://i.stack.imgur.com/1xn0G.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/1xn0G.png" alt="bar test with legend in 3rd"></a></p>
  
  <p>As you see the legend is in the third subplot and neither the colors nor labels match.</p>
  
  <hr>
  
  <p><strong>Test 2</strong>:</p>
  
  <p>Finally I tried to create a subplot with a column wrap of 2 (<code>col_wrap=2</code>) with the idea of having the legend in the right-bottom square:</p>
  
  <pre><code>grid = sns.FacetGrid(df, col="MapPubName", col_order=['F1','F2','F3'],col_wrap=2)
  </code></pre>
  
  <p>but this also results in the legend being at the right:</p>
  
  <p><a href="https://i.stack.imgur.com/zadEH.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zadEH.png" alt="barplot 2cols"></a></p>
  
  <hr>
  
  <p><strong>Question</strong>: How can I get the legend inside the first subplot? Or how can I move the legend to anywhere in the grid?</p>