擅长:python、mysql、java
<p>编辑:</p>
<p>我强烈建议使用来自ImportanceOfBeingErnest的答案:
<a href="https://stackoverflow.com/questions/4700614/how-to-put-the-legend-out-of-the-plot/43439132#43439132">How to put the legend out of the plot</a></p>
<p>这个更容易理解:</p>
<pre><code>import matplotlib.pyplot as plt
x = [1,2,3]
plt.subplot(211)
plt.plot(x, label="test1")
plt.plot([3,2,1], label="test2")
plt.legend(bbox_to_anchor=(0, 1), loc='upper left', ncol=1)
plt.show()
</code></pre>
<p>现在播放到坐标(x,y)。对于<code>loc</code>,您可以使用:</p>
<pre><code>valid locations are:
right
center left
upper right
lower right
best
center
lower left
center right
upper left
upper center
lower center
</code></pre>