Colorbar not matching for loop plot colours

3 ビュー (過去 30 日間)
aaron Harvey
aaron Harvey 2015 年 10 月 12 日
コメント済み: aaron Harvey 2015 年 10 月 12 日
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
  1 件のコメント
aaron Harvey
aaron Harvey 2015 年 10 月 12 日
Thank-you. I have to round to an integer but that did the trick.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeColormaps についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by