필터 지우기
필터 지우기

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 CenterFile Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by