用多行设置图例(在python中)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33322349/
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
set legend for plot with several lines (in python)
提问by Claudia
I have several lines plotted in the same figure and I want name this group of lines according to its content. Over those lines I intended yet to plot the average with the errorbar. But two main problem arise:
我在同一个图中绘制了几条线,我想根据其内容命名这组线。在这些线上,我还打算用误差条绘制平均值。但是出现了两个主要问题:
1) My legend does not appear as I intend (even trying to plot a extra point out of the range of the figure I can't give them a name - workaround)
1)我的图例没有按照我的意图出现(即使试图绘制超出图形范围的额外点,我也无法给它们命名 - 解决方法)
2) the plot with the average and errorbars is not superposed. Sometimes it is in front and sometimes it is behind the other curves.
2) 带有平均值和误差条的图没有叠加。有时它在前面,有时它在其他曲线后面。
What should I do to get it fixed? I could do it in Matlab (same problem for Matlab) But did not find the answer for python.
我应该怎么做才能修复它?我可以在 Matlab 中完成(Matlab 的相同问题)但没有找到 python 的答案。
This is the part of the plot of my routine:
这是我的日常情节的一部分:
UYavg = np.nanmean(UYbvall,0)
yerr = np.nanstd(UYbvall,0)
plt.figure()
for i in range(71):
plt.plot(LTbvall[i],UYbvall[i],'r-')
l1 = plt.plot([-2,-1],[1,2],'r-')
l2 = plt.plot(LTbvall[3],UYavg,'b*-')
plt.errorbar(LTbvall[2],UYavg, yerr = yerr,ecolor='b')
plt.xlabel('Tempo (LT)')
plt.xlim(0,24)
plt.ylabel('Uy (m/s)')
plt.title('Vento neutro zonal calculado pelo modelo NWM (BV)')
plt.legend((l1,l2),('Perfis COPEX','Media'), loc = 'best')
EDIT: the answer must be similar to Multiple lines in a plotor make-custom-legend-in-matplotlib
编辑:答案必须类似于Multiple lines in a plot或make-custom-legend-in-matplotlib
采纳答案by Claudia
Well based on another questions (make-custom-legend-in-matplotliband force-errorbars-to-render-last-with-matplotlib) I got it right. The second error should not happen, I think there may be a bug on zorder option. If I select only the larger number for the error bar, the plot of error bar continues hiden. So I had to set a negative number for the zorder for the lines in the for loop.
基于另一个问题(make-custom-legend-in-matplotlib和force-errorbars-to-render-last-with-matplotlib)我做对了。第二个错误不应该发生,我认为 zorder 选项可能存在错误。如果我只为误差线选择较大的数字,误差线图将继续隐藏。所以我不得不为 for 循环中的行的 zorder 设置一个负数。
The lines that fix the problems are:
解决问题的行是:
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
for i in range(71):
ax.plot(LTbvall[i],UXbvall[i],'-',color ='#C0C0C0',label = 'Perfis COPEX',zorder = -32)
ax.plot(LTbvall[3],UXavg,'b*-', label = u'média')
ax.errorbar(LTbvall[3],UXavg, yerr = yerr,ecolor='b',zorder = 10)
#Get artists and labels for legend and chose which ones to display
handles, labels = ax.get_legend_handles_labels()
display = (0,71)
ax.set_xlabel('Tempo (LT)')
ax.set_xlim(0,24)
ax.set_ylabel('Ux (m/s)')
ax.set_title('Vento neutro meridional calculado pelo modelo NWM (BV)')
ax.legend([handle for i,handle in enumerate(handles) if i in display],
[label for i,label in enumerate(labels) if i in display], loc = 'best')
fig.savefig(path[9] + 'Uxbvall_LT_nwm')
plt.clf()
plt.gcf()
plt.close()
The output is as follows:
输出如下:
回答by BlivetWidget
I find the easiest solution is to give the lines labels at creation. Try the following, you will see both lines show up on the legend:
我发现最简单的解决方案是在创建时给线条标签。尝试以下操作,您将看到图例上显示了两行:
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], color='red', label='line one')
plt.plot([4, 6, 8], color='blue', label='line two')
plt.legend()
plt.show()
回答by DilithiumMatrix
I'm surprised you're not getting an error message when you try to create your legend. The plt.plot
command always returns a tuple, so you should be catching l1, = plt.plot(...)
. Does that fix it?
当您尝试创建您的图例时,您没有收到错误消息,我感到很惊讶。该plt.plot
命令始终返回一个元组,因此您应该捕获l1, = plt.plot(...)
. 那能解决吗?