Color 2D plot according to external vector

조회 수: 1 (최근 30일)
Sebastien
Sebastien 2011년 12월 9일
I have a (10 x 501) matrix Y of, let's say, absorbances values from spectroscopic data. I have a (1 x 501) vector X of, let's say, corresponding wavenumbers. And I have a (1 x 10) vector Z of properties referring to the samples for which the Y values where recorded. I simply want to do plot(X,Y) and color the different lines according to the corresponding values of Z, i.e. a line corresponding to high Z value will have a different color from a line for which the Z value is small. But 2 samples with same Z values will have the same color for the 2 lines.
Basically I would like to reproduce what is available with scatter(X,Y,size,Z)....but for 2D plot and lines. Thanks for your help

답변 (3개)

Daniel Shub
Daniel Shub 2011년 12월 9일
Something like this might work:
Create some dummy data:
x = sort(randn(1, 501));
y = randn(10, 501)+repmat(5*(1:10)', 1, 501);
z = rand(10,1)*100;
Plot the data
colorMap = jet(length(unique(z)));
set(gcf, 'ColorMap', colorMap);
h = plot(x,y);
Change the color of the lines:
[~, ~, colorNo] = unique(z);
for icolor = 1:length(unique(z))
set(h(colorNo == icolor), 'color', colorMap(mod(icolor-1, length(colorMap))+1, :))
end
set(gca, 'CLim', [min(z), max(z)])
colorbar

Sebastien
Sebastien 2011년 12월 9일
Hi, Thanks for your reply. However, at the end of the code, if I do colorbar, it goes from 1 to 64 range....while the z data in your example are between 1 and 4.... Basically, if I have 4 colors for the lines (which I have), I would expect to have only 4 colors in the colorbar (which I haven't).... Also your z data are integers, which are even replicated, while my data data would be values (e.g. percentages) with 2 decimals and with or without duplicated.... Thanks for your help.
  댓글 수: 1
Daniel Shub
Daniel Shub 2011년 12월 9일
You should really add this as a comment to my answer. See my edited answer.

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


Sebastien
Sebastien 2011년 12월 9일
If I can add....
x=[100:600];
y=rand(25,501);
z=rand(10,1)*100;
And I want to plot (x,y) with the lines having a color related to the z values (from red=high z values to blue = low z values for instance if we keep a jet colormap).
Thanks for your help.
  댓글 수: 3
Sebastien
Sebastien 2011년 12월 13일
Many thanks for the edited code...
However in your code, the colorbar now displays 10 levels, which is fine....but which do not reflect at all the z values....
In the example, z values range from 0 to 100, while colorbar goes from 1 to 11....I would really like to have the colorbar reflecting the z values and the colors of the lines reflecting as well the intensity of the z values....Basically what scatter(X,Y,size,Z) does ....but for 2D plot and lines
Daniel Shub
Daniel Shub 2011년 12월 13일
I edited the code again ...

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

카테고리

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