如何在散点图中创建外侧图例(Matplotlib)

How to create out side legend in scatter plot(Matplotlib)

我正在使用以下代码生成散点图。

import matplotlib.pyplot as plt
from numpy.random import random

colors = ['b', 'c', 'y', 'm', 'r']

lo = plt.scatter(random(10), random(10), marker='x', color=colors[0])
ll = plt.scatter(random(10), random(10), marker='o', color=colors[0])
l  = plt.scatter(random(10), random(10), marker='o', color=colors[1])
a  = plt.scatter(random(10), random(10), marker='o', color=colors[2])
h  = plt.scatter(random(10), random(10), marker='o', color=colors[3])
hh = plt.scatter(random(10), random(10), marker='o', color=colors[4])
ho = plt.scatter(random(10), random(10), marker='x', color=colors[4])

plt.legend((lo, ll, l, a, h, hh, ho),
           ('Low Outlier', 'LoLo', 'Lo', 'Average', 'Hi', 'HiHi', 'High Outlier'),
           scatterpoints=1,
           loc='best',
           ncol=3,
           fontsize=8)
plt.savefig('foo111.png')

我想在情节之外显示图例。请帮我实现这个目标。

注意:目前我正在使用 matplotlib(1.4.3) 和 Python 2.7

尝试按照这些思路做一些事情。

ax = plt.subplot(111)

lo = ax.scatter(random(10), random(10), marker='x', color=colors[0])
ll = ax.scatter(random(10), random(10), marker='o', color=colors[0])
...

plt.legend((lo, ll, l, a, h, hh, ho),
       ('Low Outlier', 'LoLo', 'Lo', 'Average', 'Hi', 'HiHi', 'High Outlier'),
       scatterpoints=1,
       loc='center left',
       bbox_to_anchor=(1, 0.5),
       ncol=3,
       fontsize=8)

可能值得看看 the matplotlib legend docs 以了解如何更充分地使用它。