フィルターのクリア

Error bars and legend order inconsistent?

1 回表示 (過去 30 日間)
Ben
Ben 2023 年 2 月 13 日
コメント済み: Star Strider 2023 年 2 月 14 日
Hi Matlab community,
I'm trying to create a line plot with error bars where the colour of the error bars and the colour of the lines vary based on a the value of the class (in this case a subsampling factor). However, I'm having two errors:
  1. In the legend, the order of the 'patches' do not correspond to the order of the 'labels' - I have no option to do this manually so cannot define the label order manually. The lowest numbers are meant to correspond to the darkest colours.
  2. Some of the patches in the legend show without error bars, whereas others do, despite the fact that errorbars are defined and plotted for each line.
I can't upload sample data unfortunately as the dataset is too large. I've included a sample of my code below as well as a screenshot showing the issue:
for j = 6:10
Norm_Opt{j}= Optimal_Results{j}./Optimal_Results{11}; %Normalise optimal results
Norm_Per25{j}=Per_25_Results{j}./Per_25_Results{11}; %Normalise 2.5% and 97.5% confidence intervals (improves visualisation)
Norm_Per975{j}=Per_975_Results{j}./Per_975_Results{11};
cmapUS = copper(US_FactorM); %Define colormap length based on maximum value
if Output{j}.geo.US >0
nUS = nUS +1
US_Factor(nUS) = Output{j}.geo.US_Factor; %Extract subsampling factor (for FaceColor)
subplot(3,1,1)
plot(x,Norm_Opt{j},'Color',cmapUS(((US_Factor(nUS)/US_FactorM)*length(cmapUS)),:),'LineWidth',3)
eUS(nUS)=errorbar(Norm_Opt{j},Norm_Per975{j});
eUS(nUS).Color = cmapUS(((US_Factor(nUS)/US_FactorM)*length(cmapUS)),:);
eUS(nUS).LineWidth = 2;
axis tight
daspect([0.1 1 1])
lgdUS{nUS}=num2str(US_Factor(nUS)); %Format legend labels
l = legend(lgdUS)
l.FontSize = 14;
hold on
end
end
Any help would be much appreciated, thanks a lot!
Ben

採用された回答

Star Strider
Star Strider 2023 年 2 月 13 日
It is difficult to follow your code.
Perhaps something like this —
plot(x,Norm_Opt{j},'Color',cmapUS(((US_Factor(nUS)/US_FactorM)*length(cmapUS)),:),'LineWidth',3, 'DisplayName',string(US_Factor(nUS)))
then after the loop:
legend('Location','best')
That should display everything correctly, however I cannot test it with your code without the data.
.
  2 件のコメント
Ben
Ben 2023 年 2 月 14 日
Thanks a lot Star - this worked! The only other thing I had to do was specifify 'HandleVisibility' as 'off' for the error bars to remove extra legend entries
Star Strider
Star Strider 2023 年 2 月 14 日
As always, my pleasure!
Another way to do that would be to assign handles to the desired plot calls:
heb{j} = plot(x,Norm_Opt{j},'Color',cmapUS(((US_Factor(nUS)/US_FactorM)*length(cmapUS)),:),'LineWidth',3, 'DisplayName',,string(US_Factor(nUS));
and:
legend([heb{:}], 'Location','best')
Then only the legend 'DisplayName' strings associated with those plot calls would show.
That could work with your code, however I can’t test it to be certain.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Distribution Plots についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by