the legend shows different colors than the figure

조회 수: 35 (최근 30일)
Petr Michalek
Petr Michalek 2025년 11월 19일 10:00
댓글: Petr Michalek 2025년 11월 25일 15:21
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
Mathieu NOE
Mathieu NOE 2025년 11월 19일 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 2025년 11월 25일 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')

댓글을 달려면 로그인하십시오.

답변 (1개)

Star Strider
Star Strider 2025년 11월 19일 12:47
편집: Star Strider 2025년 11월 19일 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
Petr Michalek
Petr Michalek 2025년 11월 20일 8:16
이동: Star Strider 2025년 11월 20일 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 2025년 11월 20일 12:22
Noted.
However that does ntot solve the problem if you want errorbar plots.
.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Errorbars에 대해 자세히 알아보기

제품


릴리스

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by