Color 2D plot according to external vector
Afficher commentaires plus anciens
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
Réponses (3)
Daniel Shub
le 9 Déc 2011
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
1 commentaire
Daniel Shub
le 9 Déc 2011
Edited to address comments
Sebastien
le 9 Déc 2011
0 votes
1 commentaire
Daniel Shub
le 9 Déc 2011
You should really add this as a comment to my answer. See my edited answer.
Sebastien
le 9 Déc 2011
0 votes
3 commentaires
Daniel Shub
le 9 Déc 2011
You should add this to as a comment to my answer and not as an answer.
Sebastien
le 13 Déc 2011
Daniel Shub
le 13 Déc 2011
I edited the code again ...
Catégories
En savoir plus sur Color and Styling dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!