How can I change color legend by plotting with errorbar(herrorbar) ?
5 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Q3me
il 19 Mag 2015
Modificato: Nalini Vishnoi
il 21 Mag 2015
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 :
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 Commenti
Risposta accettata
Nalini Vishnoi
il 20 Mag 2015
Modificato: Nalini Vishnoi
il 21 Mag 2015
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 Commenti
Vedere anche
Categorie
Scopri di più su 2-D and 3-D Plots in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!