将图例添加到LineCollection图中 [英] Add legends to LineCollection plot

查看:295
本文介绍了将图例添加到LineCollection图中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是与>根据颜色图设置线条颜色中给出的答案相关的派生问题. 提出了一个很好的解决方案,即根据颜色条来绘制多条带有颜色的线(请参见下面的代码和输出图像).

This is a derivative question related to the answer given in Set line colors according to colormap where a great solution was suggested to plot several lines with colors according to a colorbar (see code and output image below).

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

I have a list that stores a string associated with each plotted line, like so:

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

,我想将这些字符串作为图例添加到图的右上角的方框中(其中第一个字符串对应于第一条绘制的线,依此类推).我该怎么办?

and I'd like to add these strings as legends in a box (where the first string corresponds to the first plotted line and so on) in the upper right corner of the plot. How could I do this?

我愿意在必要时不使用LineCollection,但是我需要保留颜色条和与其关联的每一行的颜色.

I'd be open to not use LineCollection if it was necessary, but I need to keep the colorbar and the colors of each line associated to it.

代码和输出

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()

推荐答案

@ubuntu的答案是正确的方法,如果您的行数很少. (如果您想添加图例,则可以这样做!)

@ubuntu's answer is right approach if you have a small number of lines. (And if you're wanting to add a legend, you presumably do!)

尽管只是显示另一个选项,您仍然可以使用LineCollection,只需要为图例使用代理艺术家"即可.

Just to show the other option, though, you can still use a LineCollection, you just need to use "proxy artists" for the legend:

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()

这篇关于将图例添加到LineCollection图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆