Specifying legend information based on point plotted

I have a matrix that was created based on filtering the data by a specified data parameter. I'm now trying to create a scatter plot based on the specified data, but would like the data to plot specified colors based on the year, which is changing through the first column within the matrix. My plotting portion of the script is as follows:
figure(1)
for ii = 1:length(DATA)
if DATA(ii,1) == 1995
c = 'r';
elseif DATA(ii,1) == 1996
c = 'b';
elseif DATA(ii,1) ==1997
c = 'm';
elseif DATA(ii,1) == 1998
c = 'c';
elseif DATA(ii,1) == 2000
c = 'g';
elseif DATA(ii,1) == 2001
c = 'k';
end
scatter(DATA(ii,9),DATA(ii,12),[],c,'filled')
hold on;
end
zeroline = zeros(length(0:maxwaveax+1));
plot([0:maxwaveax+1],zeroline,'--','color','k','LineWidth',1.5);
title('January-March','FontSize',18);
xmax = maxwaveax;
xlabel('Wave Height (m/s)','FontWeight','bold','FontSize',14);
ylabel('Mean Current (m/s','FontWeight','bold','FontSize',14);
xlim([0 xmax])
ylim([-.5 .5])
hold off
I'd like the legend to show the color specified by the year, however, everything I've searched for hasn't been helpful for my specific situation. Also, if you have suggestions regarding the way I'm plotting the specified color by year, I'm open to them.
Thanks!

4 Comments

A figure or data would be helpful
Example plot and data text file are attached. Data file is based off the matrix DATA that was created by the filtering process.
For some reason I can't view the attached image. Can you?
No, I cannot... I've tried attaching as an image instead.

Sign in to comment.

Answers (1)

dpb
dpb on 8 Jul 2018
Edited: dpb on 8 Jul 2018
% 'r' b' 'm' 'c' 'g' 'k' % color order lookup vector letter
c=[[1 0 0]; [0 0 1]; [1 0 1]; [0 1 1]; [0 0 1]; [0 0 0]]; % corresponding RGB triplet
yrs=[1995:1998 2000:2001]; % the years in the data--must be 1:1 with c array
clr=c(interp1(yr,1:length(yr),DATA(:,1),'nearest'),:); % lookup the color from the data
hS=scatter(DATA(:,9),DATA(:,12),[],clr,'filled'); % make the scatter plot
hold on;
Add whatever enhancements desired...
NB: I don't know why, but scatter isn't smart enough to accept a vector or color short names as one would think it should; you have to use the RGB triplets to write a color on a point-by-point basis. Yet another thing I've complained about since like 20+ yr ago and have finally just given up on... :(
ADDENDUM
Actually, the above doesn't quite do what you're wanting; it only creates one scatter object to label. It's possible albeit a little obtuse--
splitapply(@(x,y,c) scatter(x,y,[],c,'filled'),data,randn(size(data)),clr,findgroups(data))
legend(num2str(yr.'))
yields
This is a case where I'd probably do the simpler to have only the single scatter object and write a dummy set of lines for to use for legend
...
hold on;
hL=plot(yr,nan(numel(yr)),'marker','o');
set(hL,{'Color'},cellstr(cc),{'MarkerFaceColor'},cellstr(cc))
legend(num2str(yr.'))
The above by itself makes the following
to give the colors by year; the default line markers also show; if those aren't wanted then use the linestyle 'none' to have markers only.

2 Comments

Thank you for the response. I'm having difficulty using the splitapply command in the way you present it above. I've created a data file based on the matrix example, but with more data points that the one given. So if I'm looking to open the file, and then group the information based on the data matrix and specifying the column to be plotted in the scatter plot, how exactly would I go about doing this?
In your original you used
scatter(DATA(ii,9),DATA(ii,12),[],c,'filled')
In the 'allyears' file, the variable in column 12 have many NaN. Which are the data of actual interest?
>> t=readtable('allyears.txt');
>> sum(isnan(t.Var12))/height(t)
ans =
0.18
>>
That aside, I just tried another variable that seemed to have some variation with your full file--
>> t=readtable('allyears.txt');
>> whos t
Name Size Bytes Class Attributes
t 1581x17 219607 table
>> t.Properties.VariableNames
ans =
1×17 cell array
Columns 1 through 10
{'Var1'} {'Var2'} {'Var3'} {'Var4'} {'Var5'} {'Var6'} {'Var7'} {'Var8'} {'Var9'} {'Var10'}
Columns 11 through 17
{'Var11'} {'Var12'} {'Var13'} {'Var14'} {'Var15'} {'Var16'} {'Var17'}
>> [g,ix]=findgroups(t.Var1);
>> whos g ix
Name Size Bytes Class Attributes
g 1581x1 12648 double
ix 6x1 48 double
>> figure
>> hAx=axes; hold on
>> splitapply(@(x,y) scatter(x,y,'filled'),t.Var9,t.Var11,g)
>> legend(num2str(ix),'location','southeast')
>>
One simple item that is key but perhaps not obvious is the line
hAx=axes; hold on
This is needed before the splitapply or it will end up with only the last group showing as the default setting is 'replace' and splitapply is basically just another looping construct so is just as if one wrote
for i=1:N
plot(x(g==i),y(g==i))
end
in which case it would be easy to see why would end up with only one plot.
Would have to see what you tried, specifically, to know where went astray...

Sign in to comment.

Tags

Asked:

on 8 Jul 2018

Edited:

dpb
on 11 Jul 2018

Community Treasure Hunt

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

Start Hunting!