the legend shows different colors than the figure

48 visualizzazioni (ultimi 30 giorni)
Petr Michalek
Petr Michalek il 19 Nov 2025 alle 10:00
Commentato: Star Strider il 12 Dic 2025 alle 12:15
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
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.');
Petr Michalek
Petr Michalek il 25 Nov 2025 alle 15:21
I think I found the solution similar to yours. My error values are the same for each errorbar plot, so I multiplied them with the vector of ones.
jednicky = ones(1,73);
figure (1)
title('Mo21 hladky - male modely')
hold on; grid on;
errorbar(uhel_sort,Mo21hladky.Fx_original(index_sort),Fxerr*jednicky,'red-+')
errorbar(uhel_sort,Mo21hladky.Fy_original(index_sort),Fyerr*jednicky,'green-+')
errorbar(uhel_sort,Mo21hladky.Mz_original(index_sort),Mzerr*jednicky,'blue-+')
legend('Mo21-Fx','Mo21-Fy','Mo21-Mz')
xlabel('\alpha'); ylabel('Fx,Fy,Mz')

Accedi per commentare.

Risposte (1)

Star Strider
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
Petr Michalek il 12 Dic 2025 alle 11:57
Spostato: Star Strider il 12 Dic 2025 alle 11:59
Hello,
I have another issue with the legend, when I combine the errorbar and fill command to fill the error band wind color. My code:
jednicky31 = ones(1,31);
jednicky61 = ones(1,61);
figure (16)
subplot(2,1,1)
title('Mu50, Vu50v2 hladky - Cd + chyby')
hold on; grid on;
errorbar(Mu50uhel,Mu50pfinal.Cd_original(Mu50idx),mu50hlchyby.Cderr.*jednicky61,'red-+')
fill([Mu50uhel fliplr(Mu50uhel)],[Mu50pfinal.Cd_original(Mu50idx)+mu50hlchyby.Cderr.*jednicky61 ...
fliplr(Mu50pfinal.Cd_original(Mu50idx)-mu50hlchyby.Cderr.*jednicky61)],'red','FaceAlpha',0.2,'EdgeColor','none');
errorbar(Vu50pv2uhel23,Vu50pv2final23.Cd_original(Vu50pv2idx23),vu50pv223hlchyby.Cderr.*jednicky31,'green-+')
fill([Vu50pv2uhel23 fliplr(Vu50pv2uhel23)],[Vu50pv2final23.Cd_original(Vu50pv2idx23)+vu50pv223hlchyby.Cderr.*jednicky31 ...
fliplr(Vu50pv2final23.Cd_original(Vu50pv2idx23)-vu50pv223hlchyby.Cderr.*jednicky31)],'green','FaceAlpha',0.2,'EdgeColor','none');
errorbar(Vu50pv2uhel45,Vu50pv2final45.Cd_original(Vu50pv2idx45),vu50pv245hlchyby.Cderr.*jednicky61,'blue-+')
fill([Vu50pv2uhel45 fliplr(Vu50pv2uhel45)],[Vu50pv2final45.Cd_original(Vu50pv2idx45)+vu50pv245hlchyby.Cderr.*jednicky61 ...
fliplr(Vu50pv2final45.Cd_original(Vu50pv2idx45)-vu50pv245hlchyby.Cderr.*jednicky61)],'blue','FaceAlpha',0.2,'EdgeColor','none');
xlabel('\alpha'); ylabel('Cd err');
legend('Mu50-Cd orig.', 'Vu50v223-Cd orig.', 'Vu50v245-Cd orig.');
subplot(2,1,2)
title('Mu50, Vu50v2 hladky - Cl + chyby')
hold on; grid on;
errorbar(Mu50uhel,Mu50pfinal.Cl_original(Mu50idx),mu50hlchyby.Clerr.*jednicky61,'red-+')
fill([Mu50uhel fliplr(Mu50uhel)],[Mu50pfinal.Cl_original(Mu50idx)+mu50hlchyby.Clerr.*jednicky61 ...
fliplr(Mu50pfinal.Cl_original(Mu50idx)-mu50hlchyby.Clerr.*jednicky61)],'red','FaceAlpha',0.2,'EdgeColor','none');
errorbar(Vu50pv2uhel23,Vu50pv2final23.Cl_original(Vu50pv2idx23),vu50pv223hlchyby.Clerr.*jednicky31,'green-+');
fill([Vu50pv2uhel23 fliplr(Vu50pv2uhel23)],[Vu50pv2final23.Cl_original(Vu50pv2idx23)+vu50pv223hlchyby.Clerr.*jednicky31 ...
fliplr(Vu50pv2final23.Cl_original(Vu50pv2idx23)-vu50pv223hlchyby.Clerr.*jednicky31)],'green','FaceAlpha',0.2,'EdgeColor','none');
errorbar(Vu50pv2uhel45,Vu50pv2final45.Cl_original(Vu50pv2idx45),vu50pv245hlchyby.Clerr.*jednicky61,'blue-+')
fill([Vu50pv2uhel45 fliplr(Vu50pv2uhel45)],[Vu50pv2final45.Cl_original(Vu50pv2idx45)+vu50pv245hlchyby.Clerr.*jednicky61 ...
fliplr(Vu50pv2final45.Cl_original(Vu50pv2idx45)-vu50pv245hlchyby.Clerr.*jednicky61)],'blue','FaceAlpha',0.2,'EdgeColor','none');
xlabel('\alpha'); ylabel('Cl err');
legend('Mu50-Cl orig.', 'Vu50v223-Cl orig.', 'Vu50v245-Cl orig.');
The figure looks like this:
Star Strider
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')
.

Accedi per commentare.

Categorie

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

Prodotti


Release

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by