the legend shows different colors than the figure
48 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Hello everyone,
I have an errorbar plot, but the legend shows the same color and symbol for all items:
figure (18)
title('Mo21, Vo21final - sily a momenty')
hold on; grid on;
errorbar(Mo21uhel,Mo21final.Fx_original(Mo21idx),Fx,'red-o');
errorbar(Mo21uhel,Mo21final.Fy_original(Mo21idx),Fy,'green-o');
errorbar(Mo21uhel,Mo21final.Mz_original(Mo21idx),Mz,'blue-o');
errorbar(Vo21uhel23,Vo21final23.Fx_original(Vo21idx23),Fx,'red--+');
errorbar(Vo21uhel23,Vo21final23.Fy_original(Vo21idx23),Fy,'green--+');
errorbar(Vo21uhel23,Vo21final23.Mz_original(Vo21idx23),Mz,'blue--+');
errorbar(Vo21uhel45,Vo21final45.Fx_original(Vo21idx45),Fx,'red-+');
errorbar(Vo21uhel45,Vo21final45.Fy_original(Vo21idx45),Fy,'green-+');
errorbar(Vo21uhel45,Vo21final45.Mz_original(Vo21idx45),Mz,'blue-+');
xlabel('\alpha'); ylabel('Fx,Fy,Mz');
legend('Mo21Fx-orig.','Mo21Fy-orig.','Mo21Mz-orig.', ...
'Vo21Fx23-orig.','Vo21Fy23-orig.','Vo21Mz23-orig.', ...
'Vo21Fx45-orig.','Vo21Fy45-orig.','Vo21Mz45-orig.');
The figure: 

Please help. Thank you. Petr
2 Commenti
Mathieu NOE
il 19 Nov 2025 alle 14:57
I don't have your issue with R2025a
tested with dummy data
% dummy data
n = 100;
x = 1:n;
for k=1:9
y{k} = 10*k + 0.1*k*x + 3*sin(x/k)+0.1*randn(size(x));
e{k} = 2*ones(size(x));
end
figure (18)
title('Mo21, Vo21final - sily a momenty')
hold on; grid on;
errorbar(x,y{1},e{1},'red-o');
errorbar(x,y{2},e{2},'green-o');
errorbar(x,y{3},e{3},'blue-o');
errorbar(x,y{4},e{4},'red--+');
errorbar(x,y{5},e{5},'green--+')
errorbar(x,y{6},e{6},'blue--+');
errorbar(x,y{7},e{7},'red-+');
errorbar(x,y{8},e{8},'green-+');
errorbar(x,y{9},e{9},'blue-+');
xlabel('\alpha'); ylabel('Fx,Fy,Mz');
legend('Mo21Fx-orig.','Mo21Fy-orig.','Mo21Mz-orig.', ...
'Vo21Fx23-orig.','Vo21Fy23-orig.','Vo21Mz23-orig.', ...
'Vo21Fx45-orig.','Vo21Fy45-orig.','Vo21Mz45-orig.');
Risposte (1)
Star Strider
il 19 Nov 2025 alle 12:47
Modificato: Star Strider
il 19 Nov 2025 alle 12:49
It would help to have your data and code.
In all likelihood, this is caused by the first entry:
errorbar(Mo21uhel,Mo21final.Fx_original(Mo21idx),Fx,'red-o');
actually having several separate errorbar objects within it. (That may also be true of the others.)
If so, a fix for it is to add a handle to each of them, for example:
heb{1} = errorbar(Mo21uhel,Mo21final.Fx_original(Mo21idx),Fx,'red-o');
and so for the rest, and then in the legend call, adding references to each one as:
legend([heb{1}(1) heb{2}(1) heb{3}(1) heb{4}(1) heb{5}(1) heb{6}(1) heb{7}(1) heb{8}(1) heb{9}(1)], 'Mo21Fx-orig.','Mo21Fy-orig.','Mo21Mz-orig.', ...
'Vo21Fx23-orig.','Vo21Fy23-orig.','Vo21Mz23-orig.', ...
'Vo21Fx45-orig.','Vo21Fy45-orig.','Vo21Mz45-orig.');
(those might need to be vertically concatenated) to specify the first line object in each errorbar call.
EDIT -- Corrected typographical errors.
4 Commenti
Petr Michalek
il 12 Dic 2025 alle 11:57
Spostato: Star Strider
il 12 Dic 2025 alle 11:59
Star Strider
il 12 Dic 2025 alle 12:15
Without having access to the data, I can only guess what the problem could be.
If your vectors used in the fill calls are column vectors, the fliplr call will have no effect. You would need to use flipud (or simply flip, for a vector) instead, and vertically concatenate them, not horizontally concatenate them.
I prefer patch to fill, since I have more control over what it does.
Example --
x_col = (0:0.5:10).';
y1_col = sin(x_col/10*2*pi);
y2_col = sin(x_col/10*2*pi)+0.5;
figure
patch([x_col fliplr(x_col)], [y1_col fliplr(y2_col)], 'r')
title('\itHorizontally\rm\bf-Concatenated Column Vectors, With Flipping')
figure
patch([x_col; flip(x_col)], [y1_col; flip(y2_col)], 'g')
title('\itVertically\rm\bf-Concatenated Column Vectors, With Flipping')
.
Vedere anche
Categorie
Scopri di più su Errorbars 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!





