matplotlib two legends out of plot
<p>I'm facing problem with showing two legends outside of plot.
Showing multiple legends <strong>inside</strong> plot is easy - its described in matplotlib doc's with examples.
Even showing one legend outside of plot is rather easy as i found here on stackoverflow (ex. <a href="https://stackoverflow.com/questions/10101700/moving-matplotlib-legend-outside-of-the-axis-makes-it-cutoff-by-the-figure-box">here</a>).
But i cant find working example to show two legends outside of the plot.
Methods which work with one legend is not working in this case.</p>

<p>Here is an example.
First of all base code:</p>

<pre><code>import matplotlib.pyplot as plt
import matplotlib.patches as patches
from matplotlib.lines import Line2D
from matplotlib.font_manager import FontProperties

fig1 = plt.figure(figsize=(17,5))
fontP = FontProperties()
fontP.set_size('small')
ax1 = fig1.add_subplot(111, aspect='equal')
ax1.grid()


# stuff for legend
rec1 = patches.Rectangle(
    (0.9, 0.25),   # (x,y)
    0.1,          # width
    0.1,          # height
    label='rectangle',
    **{
        'color': 'blue'
    }

)
ax1.add_patch(rec1)

leg = plt.legend(handles=[rec1], bbox_to_anchor=(0.7, -0.1))
fig1.savefig('sample1.png', dpi=90, bbox_inches='tight')
</code></pre>

<p>But now i want to draw another legend at the right side of plot.
Here is the code:</p>

<pre><code>...
ax1.add_patch(rec1)

l1 = plt.legend(prop=fontP, handles=[rec1], loc='center left',
                box to_anchor=(1.0, 0.5))
plt.gca().add_artist(l1)

...
</code></pre>

<p>And the result:</p>

<p><a href="https://i.stack.imgur.com/8T6Li.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8T6Li.png" alt="Second, truncated legend"></a></p>

<p>As you can see, second legend is truncated.
My conclusion is that matplotlib ignores size and position of objects added with </p>

<pre><code>plt.gca().add_artist(obj)
</code></pre>

<p>How can i fix this?</p>

<p>So far i found a solution but its very nasty:</p>

<p>Create three legends, two of them as additiontal (added by add_artist) and one as normal legend.
As far matplotlib respect position and size of normal legends, move it to the right down corner and <em>hide</em> it with code:</p>

<pre><code>leg.get_frame().set_alpha(0)
</code></pre>

<p>Here are the results (without setting alpha for example purpose):</p>

<p><a href="https://i.stack.imgur.com/Q2EEZ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Q2EEZ.png" alt="Three legends out of plot"></a></p>

<p>It behave exactly how i want it to but as you know its nasty.
Here is the final code:</p>

<pre><code>import matplotlib.pyplot as plt
import matplotlib.patches as patches
from matplotlib.lines import Line2D
from matplotlib.font_manager import FontProperties

fig1 = plt.figure(figsize=(17,5))
fontP = FontProperties()
fontP.set_size('small')
ax1 = fig1.add_subplot(111, aspect='equal')
ax1.grid()

# stuff for additional legends
rec1 = patches.Rectangle(
    (0.9, 0.25),   # (x,y)
    0.1,          # width
    0.1,          # height
    label='rectangle',
    **{
        'color': 'blue'
    }
)
ax1.add_patch(rec1)

# example additional legends
l1 = plt.legend(prop=fontP, handles=[rec1], loc='center left',
bbox_to_anchor=(1.0, 0.5))
l2 = plt.legend(prop=fontP, handles=[rec1], loc=3, bbox_to_anchor=(0.4,
-0.2))

# add legends
plt.gca().add_artist(l1)
plt.gca().add_artist(l2)

# add third legend
leg = plt.legend(handles=[], bbox_to_anchor=(1.3, -0.3))
leg.get_frame().set_alpha(0) # hide legend

fig1.savefig('sample3.png', dpi=90, bbox_inches='tight')
</code></pre>