Error bars and legend order inconsistent?
7 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Ben
el 13 de Feb. de 2023
Comentada: Star Strider
el 14 de Feb. de 2023
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:
- 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.
- 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
0 comentarios
Respuesta aceptada
Star Strider
el 13 de Feb. de 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 comentarios
Star Strider
el 14 de Feb. de 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.
Más respuestas (0)
Ver también
Categorías
Más información sobre Line Plots en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!