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

时间:2015-03-09 16:33:11

标签: python 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

1 个答案:

答案 0 :(得分:1)

尝试沿着这些方向做点什么。

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,看看如何更充分地使用它。