MATLAB: Problem with individual colour display in Legend for scatter plot (2014b)

legend 2014b

I am plotting a scatter plot within a loop and then assigning a legend outside the loop for all the data (15 sets). In previous versions of Matlab the legend would match the colours used in the scatter plot to differentiate between the 15 datasets. In the latest release the legend only shows one colour for each dataset (despite the plot showing different ones).
Anyone know a reason/workaround?
code…
C=[1:1:15]; subplot(212); hold on
for i=1:15;
if ~isempty(Pro_PT(1,i).BurstTime);
B=repmat(C(i),length(Pro_PT(1,i).Stats(:,2)),1);
scatter(Pro_PT(1,i).Stats(:,2),Pro_PT(1,i).Stats(:,3),15,B,'filled','o');
leg{i}=['PT ',num2str(i)];L{i}=i;
else
leg{i}=num2str(NaN);L{i}=NaN;
end
end a=~isnan(cell2mat(L)); l=leg(a); legend(l,'Location', 'NorthWest','FontSize',6);

Best Answer

  • Here is the workaround again with the code formatting cleaned up.
    C=[1:1:15]; subplot(212); hold on
    cm = parula(15);
    colormap(cm)
    for i=1:15;
    if ~isempty(Pro_PT(1,i).BurstTime);
    B=repmat(cm(i),length(Pro_PT(1,i).Stats(:,2)),1);
    scatter(Pro_PT(1,i).Stats(:,2),Pro_PT(1,i).Stats(:,3),15,B,'filled','o');
    leg{i}=['PT ',num2str(i)];L{i}=i;
    else
    leg{i}=num2str(NaN);L{i}=NaN;
    end
    end