Python matplotlib:同一张图上有 2 个不同的图例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12761806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
matplotlib: 2 different legends on same graph
提问by Charles Brunet
I have a plot where different colors are used for different parameters, and where different line styles are used for different algorithms. The goal is to compare the results of the different algorithms performed with similar parameters. It means in total I use 4 different colors, and 3 different line styles, for a total of 12 plots on the same graph.
我有一个图,其中不同的颜色用于不同的参数,不同的线型用于不同的算法。目标是比较使用相似参数执行的不同算法的结果。这意味着我总共使用了 4 种不同的颜色和 3 种不同的线型,在同一个图形上总共有 12 个图。
I actually build the legend based on colors, associating each color with the corresponding parameter. Now I'd like to display a second legend on the same graph, with the meaning of each line style. It is possible to achieve that? How?
我实际上根据颜色构建图例,将每种颜色与相应的参数相关联。现在我想在同一张图上显示第二个图例,每个线型的含义。有可能实现吗?如何?
Here is what my code looks like actually:
这是我的代码实际上的样子:
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()
采纳答案by Mu Mind
There's a section in the matplotlib documentation on that exact subject: http://matplotlib.org/users/legend_guide.html#multiple-legends-on-the-same-axes
matplotlib 文档中有一个关于该确切主题的部分:http: //matplotlib.org/users/legend_guide.html#multiple-legends-on-the-same-axes
Here's code for your specific example:
这是您的特定示例的代码:
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)
Here's an example of its output:

这是其输出的示例:

回答by Maxime Beau
Here is also a more "hands-on" way to do it (i.e. interacting explicitely with any figure axes):
这也是一种更“动手”的方式来做到这一点(即与任何图形轴显式交互):
import itertools
from matplotlib import pyplot
fig, axes = plt.subplot(1,1)
colors = ['b', 'r', 'g', 'c']
cc = itertools.cycle(colors)
plot_lines = []
for p in parameters:
d1 = algo1(p)
d2 = algo2(p)
d3 = algo3(p)
c = next(cc)
axes.plot(d1, '-', color=c)
axes.plot(d2, '--', color=c)
axes.plot(d3, '.-', color=c)
# In total 3x3 lines have been plotted
lines = axes.get_lines()
legend1 = pyplot.legend([lines[i] for i in [0,1,2]], ["algo1", "algo2", "algo3"], loc=1)
legend2 = pyplot.legend([lines[i] for i in [0,3,6]], parameters, loc=4)
axes.add_artist(legend1)
axes.add_artist(legend2)
I like this way of writing it since it allows potentially to play with different axes in a less obscure way. You can first create your set of legends, and then add them to the axes you want with the method "add_artist". Also, I am starting with matplotlib, and for me at least it is easier to understand scripts when objets are explicited.
我喜欢这种写法,因为它允许以一种不太模糊的方式使用不同的轴。您可以先创建一组图例,然后使用“add_artist”方法将它们添加到您想要的轴上。另外,我从 matplotlib 开始,至少对我来说,当对象被显式显示时,它更容易理解脚本。
NB: Be careful, your legends may be cutoff while displaying/saving. To solve this issue, use the method axes.set_position([left, bottom, width, length]) to shrink the subplot relatively to the figure size and make the legends appear.
注意:请注意,您的图例可能会在显示/保存时被截断。要解决此问题,请使用方法 axes.set_position([left, bottom, width, length]) 相对于图形大小缩小子图并使图例出现。

