Why legend doesn't match plot
이전 댓글 표시
Hello, I'm trying to plot the following dataset, but my legend won't match the color of the lines in the plot.
datFile = 'SLMDTB44.dat';
% %
data = load(datFile);
% dataFile = load('allDataarray.mat');
% data = dataFile.allDataarray;
colContin = [184 13 72; % purple
13 184 125]/255; % green
colAppMot = [ 33 33 33]/255; % gray
timBinCenter = -(30*12):12:(3*12);
frameOff = data(:,19)-data(:,18);
report = data(:,20);
staHor = data(:, 7);
staVel = data(:, 8);
%aspRat = data(:, 8);
iniPos = data(:, 10);
motPos = data(:,11);
ori = data(:,13);
allHor = unique(staHor);
allDis = unique(motPos);
%allRat = unique(aspRat);
allOri = unique(ori);
allVel = unique(staVel);
allPos = unique(iniPos);
%Nelder-Mead search options
% options = PAL_minimize('options'); %decrease tolerance (i.e., increase
% options.TolX = 1e-09; %precision).
% options.TolFun = 1e-09;
% options.MaxIter = 10000;
% options.MaxFunEvals = 10000;
%
% % general fitting settings
% searchGrid.alpha = 1:.05:length(allRat); % structure defining grid to
% searchGrid.beta = 10.^(-1:.05:2); % search for initial values
% searchGrid.gamma = 0:.005:.1; % type help PAL_PFML_Fit for more information
% searchGrid.lambda = 0:.005:.1;
% PF = @PAL_Logistic; % PF function
%
% paramsFree = [1 1 1 1]; %[threshold slope guess lapse] 1: free, 0:fixed
figure;
hold('on');
li = 0; % legend index
Presponse=[];
% 1.) invisible motion cases
for h = 1:length(allOri)
for r = 1:length(allVel)
% idx = aspRat==allRat(r) & motDis == d & staHor == allHor(h);
idx = staVel==allVel(r) & motPos > 0 & ori == allOri(h); %& iniPos == 1; %this selects all of the trials starting from the left side
nMot(h,r) = sum(report(idx));
nAll(h,r) = sum(idx);
pMot(h,r) = nMot(h,r)/nAll(h,r);
end
% fit psychometric function
% paramsFitted = PAL_PFML_Fit((1:length(allRat)), nHor(h,:), nAll(h,:), searchGrid, paramsFree, PF,...
% 'lapseLimits',[0 1],'guessLimits',[0 1],'searchOptions',options);
li = li + 1;
% hPlot(li) = plot(searchGrid.alpha,PF(paramsFitted,searchGrid.alpha),'-','color',colContin(h,:),'linewidth',2);
hPlot(li) = plot(1:length(allVel),pMot(h,:),'LineWidth', 2.0)%,'color',colContin(h,:),'MarkerFaceColor',colContin(h,:),'LineWidth',2);
end
Presponse = [Presponse;pMot];
% 1.) no motion cases
for h = 1:length(allOri)
for r = 1:length(allVel)
idx = staVel==allVel(r) & motPos > 0 & ori == allOri(h);
nMot(h,r) = sum(report(idx));
nAll(h,r) = sum(idx);
pMot(h,r) = nMot(h,r)/nAll(h,r);
end
% fit psychometric function
%paramsFitted = PAL_PFML_Fit((1:length(allRat)), nHor, nAll, searchGrid, paramsFree, PF,...
% 'lapseLimits',[0 1],'guessLimits',[0 1],'searchOptions',options);
li = li + 1;
%hPlot(li) = plot(searchGrid.alpha,PF(paramsFitted,searchGrid.alpha),'-','color',colContin(h,:),'linewidth',2);
hPlot(li) = plot(1:length(allVel),pMot(h,:),'LineWidth', 2.0) %,'color'),colContin(h,:),'MarkerFaceColor',colContin(h,:),'LineWidth',2);
end
Presponse = [Presponse;pMot];
% end
xlabel('Velocity )');
%xlim([-0.5 0.5]+[1 length(allRat)]);
ylabel('Proportion of motion reports');
%ylim([-0.05 1.05]);
set(gca,'Xtick',1:length(allVel),'XTickLabels',allVel);
l = legend(hPlot(1:li),{'0', '15', '30', '60', '90'},'Location','east')
댓글 수: 3
Bjorn Gustavsson
2022년 5월 26일
Since we dont have the data-file we cannot test your script. You could also attach the figure as an image, and tell us how many lines you plot in total.
ILARIA SARTORIO
2022년 5월 26일
Walter Roberson
2022년 5월 26일
편집: Walter Roberson
2022년 5월 26일
ylim auto
When you hold on then it marks xlim and ylim to not be automatically updated.
Also you should be checking for inf and nan values in the results.
답변 (1개)
Walter Roberson
2022년 5월 26일
allOri = unique(ori);
The size of that is not known ahead of time.
You construct hPlot(li) elements over two separate loops that are 1:length(allOri), so you have twice as many line() objects as you have unique allOri elements.
l = legend(hPlot(1:li),{'0', '15', '30', '60', '90'},'Location','east')
But you use 5 fixed legends -- even though the one thing you know is that li will be even, so your legends cannot match the plot even by accident.
I suggest that each time you plot() that you use 'DisplayName', such as
hPlot(li) = plot(1:length(allVel),pMot(h,:),'LineWidth', 2.0, ...
'DisplayName', "no motion " + allOri(h))
and then at the end,
legend(hPlot(li), 'show')
카테고리
도움말 센터 및 File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
