the legend shows different colors than the figure

34 views (last 30 days)
Petr Michalek
Petr Michalek on 19 Nov 2025 at 10:00
Commented: Petr Michalek about 17 hours ago
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 Comments
Mathieu NOE
Mathieu NOE on 19 Nov 2025 at 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 6 minutes ago
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')

Sign in to comment.

Answers (1)

Star Strider
Star Strider on 19 Nov 2025 at 12:47
Edited: Star Strider on 19 Nov 2025 at 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.
  2 Comments
Petr Michalek
Petr Michalek on 20 Nov 2025 at 8:16
Moved: Star Strider on 20 Nov 2025 at 12:04
When I change the errorbar plot to line plot, the legend shows fine. Just changed the errorbar command to plot and removed the error value.
plot(Mo21uhel,Mo21final.Fx_original(Mo21idx),'red-o');
etc.
Star Strider
Star Strider on 20 Nov 2025 at 12:22
Noted.
However that does ntot solve the problem if you want errorbar plots.
.

Sign in to comment.

Products


Release

R2024b

Community Treasure Hunt

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

Start Hunting!