Clear Filters
Clear Filters

Error bars and legend order inconsistent?

1 view (last 30 days)
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

Accepted Answer

Star Strider
Star Strider on 13 Feb 2023
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 Comments
Ben
Ben on 14 Feb 2023
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 on 14 Feb 2023
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Data Distribution Plots in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!