필터 지우기
필터 지우기

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

조회 수: 2 (최근 30일)
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);

채택된 답변

Alexander Mueller
Alexander Mueller 2015년 3월 3일
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
  댓글 수: 3
Benjamin Kraus
Benjamin Kraus 2015년 3월 3일
Tim,
There was a typo in Alexander's code.
B=repmat(cm(i),length(Pro_PT(1,i).Stats(:,2)),1);
should be:
B=repmat(cm(i,:),length(Pro_PT(1,i).Stats(:,2)),1);
(note "cm(i,:)" instead of "cm(i)").
It would also work to just use this:
B=cm(i,:);
Both approaches are specifying the color using an RGB value. The first provides one RGB value for each point in the scatter plot, but all the RGB values are the same. Because all the points in each call to 'scatter' are the same color, the second approach specifies just one color for the each set of points (instead of each individual point).
- Ben
Tim
Tim 2015년 3월 4일
Hi Ben, Thanks for the correction and I can confirm this gives the right result. Frustrating and odd that the latest Matlab version has a bug/does not work like previous versions, but thank you for the workaround.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Alexander Mueller
Alexander Mueller 2015년 3월 3일
Tim Since you know the number of unique colors you'll need, 15, you can generate an explicit list of RGB colors and pass them into scatter as an Mx3 matrix.
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

카테고리

Help CenterFile Exchange에서 Legend에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by