将图例添加到LineCollection图

时间:2013-11-09 15:33:21

标签: python matplotlib

这是与Set line colors according to colormap中给出的答案相关的衍生问题,其中提出了一个很好的解决方案,根据颜色条绘制多条颜色的线条(参见下面的代码和输出图像)。

我有一个列表,用于存储与每个绘制线关联的字符串,如下所示:

legend_list = ['line_1', 'line_2', 'line_3', 'line_4']

我想在图表的右上角将这些字符串添加为一个框中的图例(第一个字符串对应于第一个绘制的线,依此类推)。我怎么能这样做?

如果有必要,我愿意不使用LineCollection,但我需要保持颜色栏和每条线的颜色相关联。


代码和输出

import numpy
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection

# The line format you curently have:
lines = [[(0, 1, 2, 3, 4), (4, 5, 6, 7, 8)],
         [(0, 1, 2, 3, 4), (0, 1, 2, 3, 4)],
         [(0, 1, 2, 3, 4), (8, 7, 6, 5, 4)],
         [(4, 5, 6, 7, 8), (0, 1, 2, 3, 4)]]

# Reformat it to what `LineCollection` expects:
lines = [zip(x, y) for x, y in lines]

z = np.array([0.1, 9.4, 3.8, 2.0])

fig, ax = plt.subplots()
lines = LineCollection(lines, array=z, cmap=plt.cm.rainbow, linewidths=5)
ax.add_collection(lines)
fig.colorbar(lines)

# Manually adding artists doesn't rescale the plot, so we need to autoscale
ax.autoscale()

plt.show()

enter image description here

2 个答案:

答案 0 :(得分:10)

@ ubuntu的回答是正确的方法,如果你有少量的行。 (如果你想添加一个传奇,你可能会这样做!)

但是,为了显示其他选项,您仍然可以使用LineCollection,您只需要使用“代理艺术家”作为图例:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection
from matplotlib.lines import Line2D

# The line format you curently have:
lines = [[(0, 1, 2, 3, 4), (4, 5, 6, 7, 8)],
         [(0, 1, 2, 3, 4), (0, 1, 2, 3, 4)],
         [(0, 1, 2, 3, 4), (8, 7, 6, 5, 4)],
         [(4, 5, 6, 7, 8), (0, 1, 2, 3, 4)]]

# Reformat it to what `LineCollection` expects:
lines = [tuple(zip(x, y)) for x, y in lines]

z = np.array([0.1, 9.4, 3.8, 2.0])

fig, ax = plt.subplots()
lines = LineCollection(lines, array=z, linewidths=5,
                       cmap=plt.cm.rainbow, norm=plt.Normalize(z.min(), z.max()))
ax.add_collection(lines)
fig.colorbar(lines)

# Manually adding artists doesn't rescale the plot, so we need to autoscale
ax.autoscale()

def make_proxy(zvalue, scalar_mappable, **kwargs):
    color = scalar_mappable.cmap(scalar_mappable.norm(zvalue))
    return Line2D([0, 1], [0, 1], color=color, **kwargs)
proxies = [make_proxy(item, lines, linewidth=5) for item in z]
ax.legend(proxies, ['Line 1', 'Line 2', 'Line 3', 'Line 4'])

plt.show()

enter image description here

答案 1 :(得分:5)

如果你有大量的行,使用LineCollection比使用plt.plot要快,但是如果使用LineCollection,我还无法弄清楚如何添加图例。 legend guide表示要使用代理艺术家,但如果您必须为LineCollection中的每个线段创建不同的代理艺术家,那么咬住子弹并使用plt.plot可能会更好。

既然你想要一个传奇,你似乎有少量的线条似乎是合理的。实际上,这将是幸运的,因为尝试用plt.plot绘制数千行是一种缓慢的方法。

因此,如果您的行数较少,则以下情况应该可以正常工作:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm

lines = [[(0, 1, 2, 3, 4), (4, 5, 6, 7, 8)],
         [(0, 1, 2, 3, 4), (0, 1, 2, 3, 4)],
         [(0, 1, 2, 3, 4), (8, 7, 6, 5, 4)],
         [(4, 5, 6, 7, 8), (0, 1, 2, 3, 4)]]

z = np.array([0.1, 9.4, 3.8, 2.0])

legend_list = ['line_1', 'line_2', 'line_3', 'line_4']

fig, ax = plt.subplots()
cmap = plt.get_cmap('rainbow')

def normalize(z):
    z = z.copy()
    z -= z.min()
    z /= z.max()
    return z

for (x, y), color, label in zip(lines, normalize(z), legend_list):
    plt.plot(x, y, label=label, color=cmap(color), lw=5)

m = cm.ScalarMappable(cmap=cmap)
m.set_array(z)
plt.colorbar(m)

ax.legend()
plt.savefig('/tmp/test.png')

enter image description here