Python matplotlib:同一图形上的两个不同图例

Python matplotlib:同一图形上的两个不同图例,python,matplotlib,legend,Python,Matplotlib,Legend,我有一个绘图,其中不同的颜色用于不同的参数,不同的线条样式用于不同的算法。目的是比较使用相似参数执行的不同算法的结果。这意味着我总共使用了4种不同的颜色和3种不同的线条样式,在同一个图形上总共有12个绘图 我实际上是基于颜色构建图例,将每种颜色与相应的参数相关联。现在,我想在同一个图形上显示第二个图例,以及每个线条样式的含义。有可能做到这一点吗?怎么做 下面是我的代码实际上的样子: colors = ['b', 'r', 'g', 'c'] cc = cycle(c) for p in para

我有一个绘图,其中不同的颜色用于不同的参数,不同的线条样式用于不同的算法。目的是比较使用相似参数执行的不同算法的结果。这意味着我总共使用了4种不同的颜色和3种不同的线条样式,在同一个图形上总共有12个绘图

我实际上是基于颜色构建图例,将每种颜色与相应的参数相关联。现在,我想在同一个图形上显示第二个图例,以及每个线条样式的含义。有可能做到这一点吗?怎么做

下面是我的代码实际上的样子:

colors = ['b', 'r', 'g', 'c']
cc = cycle(c)
for p in parameters:

    d1 = algo1(p)
    d2 = algo2(p)
    d3 = algo3(p)

    pyplot.hold(True)
    c = next(cc)
    pyplot.plot(d1, '-', color=c, label="d1")
    pyplot.plot(d1, '--', color=c)
    pyplot.plot(d2, '.-', color=c)

pyplot.legend()

以下是特定示例的代码:

import itertools
from matplotlib import pyplot

colors = ['b', 'r', 'g', 'c']
cc = itertools.cycle(colors)
plot_lines = []
for p in parameters:

    d1 = algo1(p)
    d2 = algo2(p)
    d3 = algo3(p)

    pyplot.hold(True)
    c = next(cc)
    l1, = pyplot.plot(d1, '-', color=c)
    l2, = pyplot.plot(d2, '--', color=c)
    l3, = pyplot.plot(d3, '.-', color=c)

    plot_lines.append([l1, l2, l3])

legend1 = pyplot.legend(plot_lines[0], ["algo1", "algo2", "algo3"], loc=1)
pyplot.legend([l[0] for l in plot_lines], parameters, loc=4)
pyplot.gca().add_artist(legend1)
下面是其输出的一个示例: 这里还有一种更“动手”的方式(即与任何图形轴进行明确交互):

我喜欢这种写作方式,因为它允许以一种不那么晦涩的方式使用不同的轴。您可以先创建一组图例,然后使用“添加艺术家”方法将它们添加到所需的轴上。另外,我从matplotlib开始,对我来说,当对象被明确化时,至少更容易理解脚本


注意:小心,在显示/保存时,图例可能会被截断。要解决此问题,请使用方法axes.set_position([left,bottom,width,length])将子地块相对于地物大小缩小并显示图例。

您也可以使用
line.get_label()


使用双重影轴怎么样

import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots()

colors = ['b', 'r', 'g', ]
styles = ['-', '--', '-.']

for cc, col in enumerate(colors):
    for ss, sty in enumerate(styles):
        print(cc, ss)
        ax.plot([0, 1], [cc, ss], c=colors[cc], ls=styles[ss])

for cc, col in enumerate(colors):
    ax.plot(np.NaN, np.NaN, c=colors[cc], label=col)

ax2 = ax.twinx()
for ss, sty in enumerate(styles):
    ax2.plot(np.NaN, np.NaN, ls=styles[ss],
             label='style ' + str(ss), c='black')
ax2.get_yaxis().set_visible(False)

ax.legend(loc=1)
ax2.legend(loc=3)

plt.show()

因此,关键在于
添加艺术家
。。。出于某些疯狂的原因,matplotlib决定它知道得更好,并删除了原始图例,然后您必须在以后将其添加回。谢谢你的帮助,我要喝杯啤酒。
import matplotlib.pyplot as plt

plt.figure()

colors = ['b', 'r', 'g', 'c']
parameters = [1,2,3,4]
for p in parameters:

  color = colors[parameters.index(p)]
  plt.plot([1,10],[1,p], '-', c=color, label='auto label '+str(p))

lines = plt.gca().get_lines()
include = [0,1]
legend1 = plt.legend([lines[i] for i in include],[lines[i].get_label() for i in include], loc=1)
legend2 = plt.legend([lines[i] for i in [2,3]],['manual label 3','manual label 4'], loc=4)
plt.gca().add_artist(legend1)
plt.show()

import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots()

colors = ['b', 'r', 'g', ]
styles = ['-', '--', '-.']

for cc, col in enumerate(colors):
    for ss, sty in enumerate(styles):
        print(cc, ss)
        ax.plot([0, 1], [cc, ss], c=colors[cc], ls=styles[ss])

for cc, col in enumerate(colors):
    ax.plot(np.NaN, np.NaN, c=colors[cc], label=col)

ax2 = ax.twinx()
for ss, sty in enumerate(styles):
    ax2.plot(np.NaN, np.NaN, ls=styles[ss],
             label='style ' + str(ss), c='black')
ax2.get_yaxis().set_visible(False)

ax.legend(loc=1)
ax2.legend(loc=3)

plt.show()