Why is my errorbar legend not showing the proper colors?

7 ビュー (過去 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 Center および File ExchangeErrorbars についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by