Colorbar not matching for loop plot colours

4 次查看(过去 30 天)
Hi there i have a for loop plotting multiple lines on the same graph, with the help of @Marius I have managed to get each line a different color in a color scheme (jet). However how can I add a colorbar to the plot? I have tried myself right at the end of the script but its not working?
function TempSalplot(datafile)
datafile=importdata(datafile);
A=datafile.data; %depth,distance,salinity,temp are in column 28,2,14,11 of datafile respectively;
figure;
for z=[1:3:max(datafile.data(:,28))]; %max is around 16
col = jet(z);
for depthi=[1:length(A(:,28))];
if A(depthi,28)>z-0.5 & A(depthi,28)<z+0.5;
B(depthi,1)=A(depthi,2);
B(depthi,2)=A(depthi,14);
B(depthi,3)=A(depthi,11);
B(all(B==0,2),:)=[];
[C,ia,idx]=unique(B(:,1),'stable');
val=accumarray(idx,B(:,2),[],@mean);
M12=[C val];
[C,ia,idx]=unique(B(:,1),'stable');
val=accumarray(idx,B(:,3),[],@mean);
M13=[C val];
M=[M12,M13(:,2)];
end
end
plot(M(:,2),M(:,3),'.-','color',col(z,:));
xlabel('Salinity (psu)');
ylabel('Tempurature (degC)');
hold on
end
h=colorbar('ticks',[1:max(z)]); %This doesn't seem to be working?
caxis([1,max(z)]);
end
The colors in the colorbar just dont match the colors on the graph? Print screen of figure -->http://prntscr.com/8qecy1

采纳的回答

Ingrid
Ingrid 2015-10-12
just a quick glance so not sure if this is what you are looking for but you should put the call to create the colors of jet outside of your for-loop
figure;
col = jet(max(datafile.data(:,28)));
for z=[1:3:max(datafile.data(:,28))]; %max is around 16

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Colormaps 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by