Why is my errorbar legend not showing the proper colors?

조회 수: 5 (최근 30일)
Maya Thompson
Maya Thompson 2024년 3월 14일
편집: Voss 2024년 3월 14일
I'm creating an errorbar plot, and I'm sure I did it correctly since the points changed color too. The legend is staying blue for both data sets though. Am I missing something? Thank you :)
box on
errorbar(data.Alkalinity_meq_L_(1:19), data.Salinity_ppt_(1:19), 0.8240, 'horizontal','.','Color','b')
hold on
errorbar(data.Alkalinity_meq_L_(21:24),data.Salinity_ppt_(21:24), 0.8240, 'horizontal','.','Color','r')
hold off
xlabel('Alkalinity (meq/L)')
ylabel('Salinity (ppt)')
xlim([50 600])
ylim([2.5 25])
legend('Milk Lake','Goodenough Lake', 'Location', 'northwest','Orientation','vertical')
ax = gca;
set(gca,'FontSize',14)
  댓글 수: 1
Walter Roberson
Walter Roberson 2024년 3월 14일
Same reason as https://www.mathworks.com/matlabcentral/answers/2092871-incorrect-rendering-of-legend-in-an-error-bar-plot-having-both-verticle-and-horizontal-error-bars#answer_1423491

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

답변 (1개)

Voss
Voss 2024년 3월 14일
편집: Voss 2024년 3월 14일
errorbar(x,y,err), with vector x and y and scalar err, produces as many errorbar objects as there are elements in x (or y).
In this case that gives you 19 blue errorbar objects and 4 red errorbar objects. When you call legend with two labels, the legend applies to the first two objects in your plot, which are the first two errorbars created, both of which are blue.
To fix this, you can create one blue errorbar object (containing 19 data points) and one red errorbar object (containing 4 data points) by specifying err as a vector the same size as x and y.
% some maade-up data
data = struct('Alkalinity_meq_L_',50+550*rand(1,24),'Salinity_ppt_',2.5+22.5*rand(1,24));
box on
errorbar(data.Alkalinity_meq_L_(1:19), data.Salinity_ppt_(1:19), 0.8240*ones(1,19), 'horizontal','.','Color','b')
hold on
errorbar(data.Alkalinity_meq_L_(21:24),data.Salinity_ppt_(21:24), 0.8240*ones(1,4), 'horizontal','.','Color','r')
hold off
xlabel('Alkalinity (meq/L)')
ylabel('Salinity (ppt)')
xlim([50 600])
ylim([2.5 25])
legend('Milk Lake','Goodenough Lake', 'Location', 'northwest','Orientation','vertical')
ax = gca;
set(gca,'FontSize',14)

카테고리

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

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by