我绘制了相同类型的信息,但针对不同的国家,使用Matplotlib绘制了多个子图。也就是说,我在一个3x3网格上有9个图,所有的线都是相同的(当然,每条线的值不同)。
然而,我还没有弄清楚如何将一个图例(因为所有九个子图都有相同的线条)放在图形上一次。
我怎么做呢?
我绘制了相同类型的信息,但针对不同的国家,使用Matplotlib绘制了多个子图。也就是说,我在一个3x3网格上有9个图,所有的线都是相同的(当然,每条线的值不同)。
然而,我还没有弄清楚如何将一个图例(因为所有九个子图都有相同的线条)放在图形上一次。
我怎么做呢?
当前回答
对于在具有多个轴的图形中自动定位单个图例,例如使用subplots()获得的图例,以下解决方案非常有效:
plt.legend(lines, labels, loc = 'lower center', bbox_to_anchor = (0, -0.1, 1, 1),
bbox_transform = plt.gcf().transFigure)
使用bbox_to_anchor和bbox_transform=plt.gcf()。transFigure,你正在定义一个新的边界框的大小你的数字作为loc的参考。使用(0,-0.1,1,1)将这个边界框略微向下移动,以防止图例被放置在其他艺术家之上。
OBS:在使用fig.set_size_inch()之后和使用fig.tight_layout()之前使用这个解决方案
其他回答
还有一个很好的函数get_legend_handles_labels(),你可以在最后一个轴上调用(如果你迭代它们),它会收集你从label=参数中需要的一切:
handles, labels = ax.get_legend_handles_labels()
fig.legend(handles, labels, loc='upper center')
Figlegend可能就是您要找的:matplotlib.pyplot.figlegend
一个例子是在图图例演示。
另一个例子:
plt.figlegend(lines, labels, loc = 'lower center', ncol=5, labelspacing=0.)
Or:
fig.legend(lines, labels, loc = (0.5, 0), ncol=5)
博士TL;
lines_labels = [ax.get_legend_handles_labels() for ax in fig.axes]
lines, labels = [sum(lol, []) for lol in zip(*lines_labels)]
fig.legend(lines, labels)
我注意到,其他答案都没有显示一个图像,其中一个图例引用了不同子图中的许多曲线,所以我必须给你看一个……为了让你好奇……
现在,如果我已经戏弄你们够多了,这就是代码
from numpy import linspace
import matplotlib.pyplot as plt
# each Axes has a brand new prop_cycle, so to have differently
# colored curves in different Axes, we need our own prop_cycle
# Note: we CALL the axes.prop_cycle to get an itertoools.cycle
color_cycle = plt.rcParams['axes.prop_cycle']()
# I need some curves to plot
x = linspace(0, 1, 51)
functs = [x*(1-x), x**2*(1-x),
0.25-x*(1-x), 0.25-x**2*(1-x)]
labels = ['$x-x²$', '$x²-x³$',
'$\\frac{1}{4} - (x-x²)$', '$\\frac{1}{4} - (x²-x³)$']
# the plot,
fig, (a1,a2) = plt.subplots(2)
for ax, f, l, cc in zip((a1,a1,a2,a2), functs, labels, color_cycle):
ax.plot(x, f, label=l, **cc)
ax.set_aspect(2) # superfluos, but nice
# So far, nothing special except the managed prop_cycle. Now the trick:
lines_labels = [ax.get_legend_handles_labels() for ax in fig.axes]
lines, labels = [sum(lol, []) for lol in zip(*lines_labels)]
# Finally, the legend (that maybe you'll customize differently)
fig.legend(lines, labels, loc='upper center', ncol=4)
plt.show()
If you want to stick with the official Matplotlib API, this is perfect, otherwise see note no.1 below (there is a private method...) The two lines lines_labels = [ax.get_legend_handles_labels() for ax in fig.axes] lines, labels = [sum(lol, []) for lol in zip(*lines_labels)] deserve an explanation, see note 2 below. I tried the method proposed by the most up-voted and accepted answer, # fig.legend(lines, labels, loc='upper center', ncol=4) fig.legend(*a2.get_legend_handles_labels(), loc='upper center', ncol=4) and this is what I've got
注1 如果您不介意使用matplotlib的私有方法。传说模块…这真的非常非常简单
from matplotlib.legend import _get_legend_handles_labels
...
fig.legend(*_get_legend_handles_and_labels(fig.axes), ...)
注2
我把这两行棘手的代码封装在一个函数中,只有四行代码,但是注释了很多
def fig_legend(fig, **kwdargs):
# Generate a sequence of tuples, each contains
# - a list of handles (lohand) and
# - a list of labels (lolbl)
tuples_lohand_lolbl = (ax.get_legend_handles_labels() for ax in fig.axes)
# E.g., a figure with two axes, ax0 with two curves, ax1 with one curve
# yields: ([ax0h0, ax0h1], [ax0l0, ax0l1]) and ([ax1h0], [ax1l0])
# The legend needs a list of handles and a list of labels,
# so our first step is to transpose our data,
# generating two tuples of lists of homogeneous stuff(tolohs), i.e.,
# we yield ([ax0h0, ax0h1], [ax1h0]) and ([ax0l0, ax0l1], [ax1l0])
tolohs = zip(*tuples_lohand_lolbl)
# Finally, we need to concatenate the individual lists in the two
# lists of lists: [ax0h0, ax0h1, ax1h0] and [ax0l0, ax0l1, ax1l0]
# a possible solution is to sum the sublists - we use unpacking
handles, labels = (sum(list_of_lists, []) for list_of_lists in tolohs)
# Call fig.legend with the keyword arguments, return the legend object
return fig.legend(handles, labels, **kwdargs)
我认识到sum(list_of_lists,[])是一个非常低效的方法来扁平化列表的列表,但①我喜欢它的紧凑性,②通常是几个子图中的几条曲线,③Matplotlib和效率?: -)
使用Matplotlib 2.2.2,可以使用gridspec特性来实现这一点。
在下面的例子中,目标是以2x2的方式排列四个子情节,并在底部显示图例。在底部创建一个“人造”轴,将图例放置在固定的位置。“人造”轴然后关闭,所以只有传说显示。结果:
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
# Gridspec demo
fig = plt.figure()
fig.set_size_inches(8, 9)
fig.set_dpi(100)
rows = 17 # The larger the number here, the smaller the spacing around the legend
start1 = 0
end1 = int((rows-1)/2)
start2 = end1
end2 = int(rows-1)
gspec = gridspec.GridSpec(ncols=4, nrows=rows)
axes = []
axes.append(fig.add_subplot(gspec[start1:end1, 0:2]))
axes.append(fig.add_subplot(gspec[start2:end2, 0:2]))
axes.append(fig.add_subplot(gspec[start1:end1, 2:4]))
axes.append(fig.add_subplot(gspec[start2:end2, 2:4]))
axes.append(fig.add_subplot(gspec[end2, 0:4]))
line, = axes[0].plot([0, 1], [0, 1], 'b') # Add some data
axes[-1].legend((line,), ('Test',), loc='center') # Create legend on bottommost axis
axes[-1].set_axis_off() # Don't show the bottom-most axis
fig.tight_layout()
plt.show()
你只需要在循环之外请求一次图例。
例如,在这种情况下,我有4个子情节,具有相同的线,和一个图例。
from matplotlib.pyplot import *
ficheiros = ['120318.nc', '120319.nc', '120320.nc', '120321.nc']
fig = figure()
fig.suptitle('concentration profile analysis')
for a in range(len(ficheiros)):
# dados is here defined
level = dados.variables['level'][:]
ax = fig.add_subplot(2,2,a+1)
xticks(range(8), ['0h','3h','6h','9h','12h','15h','18h','21h'])
ax.set_xlabel('time (hours)')
ax.set_ylabel('CONC ($\mu g. m^{-3}$)')
for index in range(len(level)):
conc = dados.variables['CONC'][4:12,index] * 1e9
ax.plot(conc,label=str(level[index])+'m')
dados.close()
ax.legend(bbox_to_anchor=(1.05, 0), loc='lower left', borderaxespad=0.)
# it will place the legend on the outer right-hand side of the last axes
show()