필터 지우기
필터 지우기

Colorbar not matching for loop plot colours

조회 수: 6 (최근 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 CenterFile Exchange에서 Colormaps에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by