将图例添加到LineCollection图

Gab*_*iel 7 python matplotlib

这是一个衍生问题,与根据colormap设置线条颜色给出的答案相关,其中建议使用一个很好的解决方案根据颜色条绘制多条颜色的线条(请参阅下面的代码和输出图像).

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

legend_list = ['line_1', 'line_2', 'line_3', 'line_4']
Run Code Online (Sandbox Code Playgroud)

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

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()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

Joe*_*ton 10

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

只是为了显示另一个选项,你仍然可以使用a 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()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


unu*_*tbu 5

使用LineCollection比使用plt.plot大量行时更快,但如果使用LineCollection,我无法弄清楚如何添加图例.该传说指南说,使用代理艺术家,但如果你要创建在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')
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

  • 这取决于你想要什么.如果你想要紫色与z值0相关联,那么`color/z.max()`有效.但是如果你想要紫色总是与具有最低z值的线相关联,那么你确实需要像你所做的那样重新缩放.我将修改我的答案以显示另一种方法. (2认同)