How can I change color legend by plotting with errorbar(herrorbar) ?
2 次查看(过去 30 天)
显示 更早的评论
I wrote this code in order to have 2 lines with 2 different color with errorbar (more particulary herrobar in order to have horizontally error bars, it's a function available in mathworks).
herrorbar(moytemp12h,moyalt12h,e./2,'r');
title('Radiosondage moyenné au trajet à latitude 0°N (du 28/05/06 au 01/06/06)');
xlabel('Température (°C)');
ylabel('Altitude (m)');
legend('0h','12h');
Here is the plot :
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/150279/image.png)
In the legend, I would like the line near "12h" red. Moreover, I don't understand why no wanted little vertical bars are plotted in both lines. Thank you for your help.
0 个评论
采纳的回答
Nalini Vishnoi
2015-5-20
编辑:Nalini Vishnoi
2015-5-21
Hi,
The plot you are getting is expected, however, I understand that it is not desired. I looked inside the code for 'herrorbar' and it seems like they are using 'hold on' in order to plot the horizontal error bars. This means that 'blue' lines are plotted twice in the figure for the first call (once for the actual line and second time for the error bars on that line) and after that two 'red' lines are plotted for the next call to 'herrorbar'. If you add more legend entries to the figure, you will see that the next two entries are 'red'. Since the function returns the handle to both the line and the error bars, it can be used to get the desired output by setting the 'IconDisplayStyle' of the 'Annotation' property of the items you do not want to appear in the legend. You can find more information here. Here is a small example on how to do it.
x = 0:pi/10:pi;
y = sin(x);
e = std(y)*ones(size(x));
h1 = herrorbar(x,y,e);
hold on;
h2 = herrorbar(x+2,y+2,e,'r');
set(get(get(h1(2),'Annotation'),'LegendInformation'),... % Getting rid of 2nd blue line
'IconDisplayStyle','off');
set(get(get(h2(2),'Annotation'),'LegendInformation'),... % Getting rid of 2nd red line
'IconDisplayStyle','off');
legend('A','B');
Also, for your second question about why the little vertical bars are not plotted in the legend: the error bars are combinations of simple lines, legend only plots the lines and not the complex/combined result (which you see as error bars).
Please note that this is valid for MATLAB releases prior to R2014b. From R2014b onwards, the behavior of 'hold on' has changed slightly and the above workaround will not be valid.
I hope the above information is helpful.
Nalini
0 个评论
更多回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Line Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!