<p dir="auto">I'm trying to move the legend for a <code class="notranslate">sns.histplot</code></p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import seaborn as sns
import pandas as pd
import numpy as np  # for test data
import random  # for test data
import calendar  # for test data

# test dataframe
np.random.seed(365)
random.seed(365)
df = pd.DataFrame({'Days': random.choices(calendar.day_name, k=1000), 'Visitors': np.random.randint(1, 121, size=(1000))})

ax = sns.histplot(data=df, x='Visitors', hue='Days', multiple=&quot;stack&quot;)
ax.legend(bbox_to_anchor=(1.05, 1), loc='upper left')"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">seaborn</span> <span class="pl-k">as</span> <span class="pl-s1">sns</span>
<span class="pl-k">import</span> <span class="pl-s1">pandas</span> <span class="pl-k">as</span> <span class="pl-s1">pd</span>
<span class="pl-k">import</span> <span class="pl-s1">numpy</span> <span class="pl-k">as</span> <span class="pl-s1">np</span>  <span class="pl-c"># for test data</span>
<span class="pl-k">import</span> <span class="pl-s1">random</span>  <span class="pl-c"># for test data</span>
<span class="pl-k">import</span> <span class="pl-s1">calendar</span>  <span class="pl-c"># for test data</span>

<span class="pl-c"># test dataframe</span>
<span class="pl-s1">np</span>.<span class="pl-s1">random</span>.<span class="pl-en">seed</span>(<span class="pl-c1">365</span>)
<span class="pl-s1">random</span>.<span class="pl-en">seed</span>(<span class="pl-c1">365</span>)
<span class="pl-s1">df</span> <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-v">DataFrame</span>({<span class="pl-s">'Days'</span>: <span class="pl-s1">random</span>.<span class="pl-en">choices</span>(<span class="pl-s1">calendar</span>.<span class="pl-s1">day_name</span>, <span class="pl-s1">k</span><span class="pl-c1">=</span><span class="pl-c1">1000</span>), <span class="pl-s">'Visitors'</span>: <span class="pl-s1">np</span>.<span class="pl-s1">random</span>.<span class="pl-en">randint</span>(<span class="pl-c1">1</span>, <span class="pl-c1">121</span>, <span class="pl-s1">size</span><span class="pl-c1">=</span>(<span class="pl-c1">1000</span>))})

<span class="pl-s1">ax</span> <span class="pl-c1">=</span> <span class="pl-s1">sns</span>.<span class="pl-en">histplot</span>(<span class="pl-s1">data</span><span class="pl-c1">=</span><span class="pl-s1">df</span>, <span class="pl-s1">x</span><span class="pl-c1">=</span><span class="pl-s">'Visitors'</span>, <span class="pl-s1">hue</span><span class="pl-c1">=</span><span class="pl-s">'Days'</span>, <span class="pl-s1">multiple</span><span class="pl-c1">=</span><span class="pl-s">"stack"</span>)
<span class="pl-s1">ax</span>.<span class="pl-en">legend</span>(<span class="pl-s1">bbox_to_anchor</span><span class="pl-c1">=</span>(<span class="pl-c1">1.05</span>, <span class="pl-c1">1</span>), <span class="pl-s1">loc</span><span class="pl-c1">=</span><span class="pl-s">'upper left'</span>)</pre></div>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/12875240/104376507-f6316200-54d9-11eb-9046-73a47576973f.png"><img src="https://user-images.githubusercontent.com/12875240/104376507-f6316200-54d9-11eb-9046-73a47576973f.png" alt="image" style="max-width: 100%;"></a></p>
<ul dir="auto">
<li>
<p dir="auto">Without trying to move the legend<br>
<a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/12875240/104376621-1cef9880-54da-11eb-8666-f4f4aa6d9f70.png"><img src="https://user-images.githubusercontent.com/12875240/104376621-1cef9880-54da-11eb-8666-f4f4aa6d9f70.png" alt="image" style="max-width: 100%;"></a></p>
</li>
<li>
<p dir="auto">Moving the legend in this way works fine with <code class="notranslate">sns.barplot</code></p>
</li>
</ul>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="ax = sns.barplot(data=df, x='Days', y='Visitors', hue='Days', dodge=False, estimator=sum)
ax.legend(bbox_to_anchor=(1.05, 1), loc='upper left')"><pre class="notranslate"><span class="pl-s1">ax</span> <span class="pl-c1">=</span> <span class="pl-s1">sns</span>.<span class="pl-en">barplot</span>(<span class="pl-s1">data</span><span class="pl-c1">=</span><span class="pl-s1">df</span>, <span class="pl-s1">x</span><span class="pl-c1">=</span><span class="pl-s">'Days'</span>, <span class="pl-s1">y</span><span class="pl-c1">=</span><span class="pl-s">'Visitors'</span>, <span class="pl-s1">hue</span><span class="pl-c1">=</span><span class="pl-s">'Days'</span>, <span class="pl-s1">dodge</span><span class="pl-c1">=</span><span class="pl-c1">False</span>, <span class="pl-s1">estimator</span><span class="pl-c1">=</span><span class="pl-s1">sum</span>)
<span class="pl-s1">ax</span>.<span class="pl-en">legend</span>(<span class="pl-s1">bbox_to_anchor</span><span class="pl-c1">=</span>(<span class="pl-c1">1.05</span>, <span class="pl-c1">1</span>), <span class="pl-s1">loc</span><span class="pl-c1">=</span><span class="pl-s">'upper left'</span>)</pre></div>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/12875240/104376995-a4d5a280-54da-11eb-90d6-ab5ee422abe1.png"><img src="https://user-images.githubusercontent.com/12875240/104376995-a4d5a280-54da-11eb-90d6-ab5ee422abe1.png" alt="image" style="max-width: 100%;"></a></p>
<p dir="auto">I was pretty excited about seeing the <code class="notranslate">histplot</code> functionality coming out, and I'm getting <em>beautiful</em> figures with it already.</p>
<p dir="auto">I'm trying to finetune my figures and reposition the legends - but am running into issues when moving the legend.<br>
Below is toy example</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
fig, ax = plt.subplots()
x = np.hstack((np.random.normal(0, 1, 100), np.random.normal(-0.5, 1, 100), np.random.normal(0.5, 1, 100)))
data = pd.DataFrame({'x': x, 'd' : ['a'] * 100 + ['b'] * 100 + ['c'] * 100})
g = sns.histplot(data, x='x', hue=&quot;d&quot;, element=&quot;step&quot;, stat=&quot;probability&quot;, ax=ax)"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">numpy</span> <span class="pl-k">as</span> <span class="pl-s1">np</span>
<span class="pl-k">import</span> <span class="pl-s1">pandas</span> <span class="pl-k">as</span> <span class="pl-s1">pd</span>
<span class="pl-k">import</span> <span class="pl-s1">matplotlib</span>.<span class="pl-s1">pyplot</span> <span class="pl-k">as</span> <span class="pl-s1">plt</span>
<span class="pl-k">import</span> <span class="pl-s1">seaborn</span> <span class="pl-k">as</span> <span class="pl-s1">sns</span>
<span class="pl-s1">fig</span>, <span class="pl-s1">ax</span> <span class="pl-c1">=</span> <span class="pl-s1">plt</span>.<span class="pl-en">subplots</span>()
<span class="pl-s1">x</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">hstack</span>((<span class="pl-s1">np</span>.<span class="pl-s1">random</span>.<span class="pl-en">normal</span>(<span class="pl-c1">0</span>, <span class="pl-c1">1</span>, <span class="pl-c1">100</span>), <span class="pl-s1">np</span>.<span class="pl-s1">random</span>.<span class="pl-en">normal</span>(<span class="pl-c1">-</span><span class="pl-c1">0.5</span>, <span class="pl-c1">1</span>, <span class="pl-c1">100</span>), <span class="pl-s1">np</span>.<span class="pl-s1">random</span>.<span class="pl-en">normal</span>(<span class="pl-c1">0.5</span>, <span class="pl-c1">1</span>, <span class="pl-c1">100</span>)))
<span class="pl-s1">data</span> <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-v">DataFrame</span>({<span class="pl-s">'x'</span>: <span class="pl-s1">x</span>, <span class="pl-s">'d'</span> : [<span class="pl-s">'a'</span>] <span class="pl-c1">*</span> <span class="pl-c1">100</span> <span class="pl-c1">+</span> [<span class="pl-s">'b'</span>] <span class="pl-c1">*</span> <span class="pl-c1">100</span> <span class="pl-c1">+</span> [<span class="pl-s">'c'</span>] <span class="pl-c1">*</span> <span class="pl-c1">100</span>})
<span class="pl-s1">g</span> <span class="pl-c1">=</span> <span class="pl-s1">sns</span>.<span class="pl-en">histplot</span>(<span class="pl-s1">data</span>, <span class="pl-s1">x</span><span class="pl-c1">=</span><span class="pl-s">'x'</span>, <span class="pl-s1">hue</span><span class="pl-c1">=</span><span class="pl-s">"d"</span>, <span class="pl-s1">element</span><span class="pl-c1">=</span><span class="pl-s">"step"</span>, <span class="pl-s1">stat</span><span class="pl-c1">=</span><span class="pl-s">"probability"</span>, <span class="pl-s1">ax</span><span class="pl-c1">=</span><span class="pl-s1">ax</span>)</pre></div>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/4184797/93139918-d1012400-f69e-11ea-9bc8-74b4779adb06.png"><img src="https://user-images.githubusercontent.com/4184797/93139918-d1012400-f69e-11ea-9bc8-74b4779adb06.png" alt="image" style="max-width: 100%;"></a></p>
<p dir="auto">When I move the legend to the upper left, the legend disappears with the warning <code class="notranslate">No handles with labels found to put in legend.</code>.</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
fig, ax = plt.subplots()
x = np.hstack((np.random.normal(0, 1, 100), np.random.normal(-0.5, 1, 100), np.random.normal(0.5, 1, 100)))
data = pd.DataFrame({'x': x, 'd' : ['a'] * 100 + ['b'] * 100 + ['c'] * 100})
g = sns.histplot(data, x='x', hue=&quot;d&quot;, element=&quot;step&quot;, stat=&quot;probability&quot;, ax=ax, legend=False)
ax.legend(loc='upper-left')"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">numpy</span> <span class="pl-k">as</span> <span class="pl-s1">np</span>
<span class="pl-k">import</span> <span class="pl-s1">pandas</span> <span class="pl-k">as</span> <span class="pl-s1">pd</span>
<span class="pl-k">import</span> <span class="pl-s1">matplotlib</span>.<span class="pl-s1">pyplot</span> <span class="pl-k">as</span> <span class="pl-s1">plt</span>
<span class="pl-k">import</span> <span class="pl-s1">seaborn</span> <span class="pl-k">as</span> <span class="pl-s1">sns</span>
<span class="pl-s1">fig</span>, <span class="pl-s1">ax</span> <span class="pl-c1">=</span> <span class="pl-s1">plt</span>.<span class="pl-en">subplots</span>()
<span class="pl-s1">x</span> <span class="pl-c1">=</span> <span class="pl-s1">np</span>.<span class="pl-en">hstack</span>((<span class="pl-s1">np</span>.<span class="pl-s1">random</span>.<span class="pl-en">normal</span>(<span class="pl-c1">0</span>, <span class="pl-c1">1</span>, <span class="pl-c1">100</span>), <span class="pl-s1">np</span>.<span class="pl-s1">random</span>.<span class="pl-en">normal</span>(<span class="pl-c1">-</span><span class="pl-c1">0.5</span>, <span class="pl-c1">1</span>, <span class="pl-c1">100</span>), <span class="pl-s1">np</span>.<span class="pl-s1">random</span>.<span class="pl-en">normal</span>(<span class="pl-c1">0.5</span>, <span class="pl-c1">1</span>, <span class="pl-c1">100</span>)))
<span class="pl-s1">data</span> <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-v">DataFrame</span>({<span class="pl-s">'x'</span>: <span class="pl-s1">x</span>, <span class="pl-s">'d'</span> : [<span class="pl-s">'a'</span>] <span class="pl-c1">*</span> <span class="pl-c1">100</span> <span class="pl-c1">+</span> [<span class="pl-s">'b'</span>] <span class="pl-c1">*</span> <span class="pl-c1">100</span> <span class="pl-c1">+</span> [<span class="pl-s">'c'</span>] <span class="pl-c1">*</span> <span class="pl-c1">100</span>})
<span class="pl-s1">g</span> <span class="pl-c1">=</span> <span class="pl-s1">sns</span>.<span class="pl-en">histplot</span>(<span class="pl-s1">data</span>, <span class="pl-s1">x</span><span class="pl-c1">=</span><span class="pl-s">'x'</span>, <span class="pl-s1">hue</span><span class="pl-c1">=</span><span class="pl-s">"d"</span>, <span class="pl-s1">element</span><span class="pl-c1">=</span><span class="pl-s">"step"</span>, <span class="pl-s1">stat</span><span class="pl-c1">=</span><span class="pl-s">"probability"</span>, <span class="pl-s1">ax</span><span class="pl-c1">=</span><span class="pl-s1">ax</span>, <span class="pl-s1">legend</span><span class="pl-c1">=</span><span class="pl-c1">False</span>)
<span class="pl-s1">ax</span>.<span class="pl-en">legend</span>(<span class="pl-s1">loc</span><span class="pl-c1">=</span><span class="pl-s">'upper-left'</span>)</pre></div>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/4184797/93140027-fe4dd200-f69e-11ea-8135-ffd85a627d30.png"><img src="https://user-images.githubusercontent.com/4184797/93140027-fe4dd200-f69e-11ea-8135-ffd85a627d30.png" alt="image" style="max-width: 100%;"></a></p>
<p dir="auto">This is interesting, because it doesn't appear to be an issue with the other plots<br>
<a href="https://stackoverflow.com/questions/27019079/move-seaborn-plot-legend-to-a-different-position" rel="nofollow">https://stackoverflow.com/questions/27019079/move-seaborn-plot-legend-to-a-different-position</a><br>
<a href="https://stackoverflow.com/questions/53733755/how-to-move-legend-to-outside-of-a-seaborn-scatterplot/53737271" rel="nofollow">https://stackoverflow.com/questions/53733755/how-to-move-legend-to-outside-of-a-seaborn-scatterplot/53737271</a></p>
<p dir="auto">I'm curious if this is unique to <code class="notranslate">histplot</code> (or if I'm overlooking something).</p>