Azzera filtri
Azzera filtri

Updating legend for a plot with markers and errorbar

87 visualizzazioni (ultimi 30 giorni)
I have plotted a line plot with markers and errorbar at each data point using hold on and hold off. However, legend is only updating the marker and line symbol not errorbar. How to fix this issue ? Above is the plot what I got from the code given below.
Here is my code for the above plot:
x = 1:5;
y = 3*x;
err = std(y);
plot(x,y,'k-o','MarkerFaceColor','green')
hold on
errorbar(x,y,err,'Color','k')
hold off
legend('Data 1')

Risposte (3)

Sunny Choudhary
Sunny Choudhary il 23 Giu 2023
Just replace the last line of code with
h = legend('Data 1', 'Error bars');
Here's the full code:
x = 1:5;
y = 3*x;
err = std(y);
% Plot the data with markers and error bars
plot(x,y,'k-o','MarkerFaceColor','green')
hold on
errorbar(x,y,err,'Color','k')
hold off
% Add legend with both markers and error bars
h = legend('Data 1', 'Error bars');

Deep
Deep il 23 Giu 2023
Modificato: Deep il 23 Giu 2023
If I understand correctly, this is the output that you desire.
Here's how you can plot the fixed legend for your data. Hope this helps!
h1 = plot(x, y, 'ko-', 'MarkerFaceColor', 'g');
hold on
h2 = errorbar(x, y, err, 'k.', 'LineStyle', 'none');
hold off
legend([h1 h2], 'Data', 'Error')

dpb
dpb il 23 Giu 2023
Modificato: dpb il 23 Giu 2023
x = 1:5;
y = 3*x;
err = std(y);
plot(x,y,'k-o','MarkerFaceColor','green');
hold on
errorbar(x,y,err,'Color','k');
hold off
legend('Data 1','Error')
The line and the errorbar are two separate graphics entities/objects; legend() can only display the linestyle associated with a given object so you need two text labels, one for each as you've drawn the graphic.
Perhaps you were looking for something more like
figure
err=err*ones(size(y));
hEB=errorbar(x,y,err,'ko-','MarkerFaceColor','g');
legend('Data 1','location','best')
xlim(xlim+0.5*[-1 1])
NOTA BENE:
Besides the issue of having the line and errorbar objects, the original version with a single value for the error magnitude does not match the doc errorbar that states err must be a vector matching length of y.
This usage, while certainly a reasonable extension, has some rather unexpected effects; namely errorbar then returns an array of errorbar objects, one for each point in y and thus doesn't draw a line between the points. This behavior is undoubtedly what caused the use of the otherwise unneeded use of plot(). Looks as though worthy of a bug report, at least a warning should be produced. The addition of err as the vector in the extra line above fixes the problem and then the plotted object looks like expected in legend
ADDENDUM
W/O the vector for error, the result is
figure
err=err(1); % turn err back into single value
hEB=errorbar(x,y,err,'ko-','MarkerFaceColor','g');
legend('Data 1','location','best')
xlim(xlim+0.5*[-1 1])
hEB.LineStyle
ans = '-'
ans = '-'
ans = '-'
ans = '-'
ans = '-'
NB the array of linestyles and no line visible on plot although the legend shows it...bug!!! And, the side effect that the line through the data points is not shown because each is a separate errorbar object, not one as expected.
  1 Commento
dpb
dpb il 24 Giu 2023
ADDENDUM: I did submit bug/enhancement to TMW to cover the issue in one way or another; ideal would be to extend the syntax to accept the single error value and generate the properly-sized error vector/array internally; the minimal would be to at least warn the user of the issue.

Accedi per commentare.

Categorie

Scopri di più su Errorbars in Help Center e File Exchange

Prodotti


Release

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by