<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style type="text/css" style="display:none;"><!-- P {margin-top:0;margin-bottom:0;} --></style>
</head>
<body dir="ltr">
<div id="divtagdefaultwrapper" style="font-size:12pt;color:#000000;font-family:Calibri,Helvetica,sans-serif;" dir="ltr">
<p style="margin-top:0;margin-bottom:0"></p>
<div id="divtagdefaultwrapper" style="font-size: 12pt; color: rgb(0, 0, 0); font-family: Calibri, Helvetica, sans-serif, EmojiFont, "Apple Color Emoji", "Segoe UI Emoji", NotoColorEmoji, "Segoe UI Symbol", "Android Emoji", EmojiSymbols;" dir="ltr">
<div class="markdown-here-wrapper" data-md-url="outlook.live.com" style="">
<p style="margin: 0px 0px 1.2em !important;">Hi,<br>
I tried to make a plot with two subplots but only one legend since the two subplots express the same set of information.<br>
I put the legend outside of the two subplots for better interpretation. I have found that by using
<code style="font-size: 0.85em; font-family: Consolas, Inconsolata, Courier, monospace;margin: 0px 0.15em; padding: 0px 0.3em; white-space: pre-wrap; border: 1px solid rgb(234, 234, 234); background-color: rgb(248, 248, 248); border-radius: 3px; display: inline;">
fig.legend</code> instead of<br>
the <code style="font-size: 0.85em; font-family: Consolas, Inconsolata, Courier, monospace;margin: 0px 0.15em; padding: 0px 0.3em; white-space: pre-wrap; border: 1px solid rgb(234, 234, 234); background-color: rgb(248, 248, 248); border-radius: 3px; display: inline;">
ax.legend</code> method. In the saved plot, the legend box are just cut off. </p>
<p style="margin: 0px 0px 1.2em !important;">I tried to fix this by using <code style="font-size: 0.85em; font-family: Consolas, Inconsolata, Courier, monospace;margin: 0px 0.15em; padding: 0px 0.3em; white-space: pre-wrap; border: 1px solid rgb(234, 234, 234); background-color: rgb(248, 248, 248); border-radius: 3px; display: inline;">
bbox_extra_artists</code> argument in the <code style="font-size: 0.85em; font-family: Consolas, Inconsolata, Courier, monospace;margin: 0px 0.15em; padding: 0px 0.3em; white-space: pre-wrap; border: 1px solid rgb(234, 234, 234); background-color: rgb(248, 248, 248); border-radius: 3px; display: inline;">
savefig</code> method, following the instruction<br>
<a href="https://stackoverflow.com/questions/10101700/moving-matplotlib-legend-outside-of-the-axis-makes-it-cutoff-by-the-figure-box">here</a>.<br>
But it does not work. The following code illustrate the problem,</p>
<pre style="font-size: 0.85em; font-family: Consolas, Inconsolata, Courier, monospace;font-size: 1em; line-height: 1.2em;margin: 1.2em 0px;"><code style="font-size: 0.85em; font-family: Consolas, Inconsolata, Courier, monospace;margin: 0px 0.15em; padding: 0px 0.3em; white-space: pre-wrap; border: 1px solid rgb(234, 234, 234); background-color: rgb(248, 248, 248); border-radius: 3px; display: inline;white-space: pre; overflow: auto; border-radius: 3px; border: 1px solid rgb(204, 204, 204); padding: 0.5em 0.7em; display: block !important;">import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl

x = np.arange(-5, 5, 0.1)
y1 = np.sin(x)
y2 = np.cos(x)

y3 = np.sin(2*x)
y4 = np.cos(2*x)

fig, (ax1, ax2) = plt.subplots(ncols=1, nrows=2, figsize=(10, 6))

ax1.plot(x, y1, label='sin')
ax1.plot(x, y2, label='cos')

ax2.plot(x, y3, label='sin')
ax2.plot(x, y4, label='cos')

handles, labels = ax1.get_legend_handles_labels()

# legend = fig.legend(handles, labels, loc='lower left', ncol=2, frameon=False,
#               bbox_to_anchor=(0.12, 0.88))
# plt.savefig('test.jpg', bbox_extra_artists=(legend,), bbox_inches='tight')

legend = ax1.legend(handles, labels, ncol=2, frameon=False,
                    loc='lower left', bbox_to_anchor=(-0.01, 1.0))
plt.savefig('test.jpg', bbox_inches='tight')
</code></pre>
<p style="margin: 0px 0px 1.2em !important;">If I use the above code, the generated image is fine. But if I use the commented code instead, the legend is gone in the generated image.</p>
<div title="MDH:PHAgc3R5bGU9Im1hcmdpbi10b3A6MDttYXJnaW4tYm90dG9tOjAiPjwvcD48ZGl2IHN0eWxlPSJm
b250LWZhbWlseTogJnF1b3Q7TWljcm9zb2Z0IFlhSGVpIFVJJnF1b3Q7OyBmb250LXNpemU6IDE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b2xvcjogcmdiKDIwNCwgMjMyLCAyMDcpOyI+PGJyPjwvZGl2
Pjxicj48cD48L3A+" style="height:0;width:0;max-height:0;max-width:0;overflow:hidden;font-size:0em;padding:0;margin:0;">
</div>
</div>
</div>
<p></p>
</div>
</body>
</html>