Creating rgb vector based on Z data for standard colorbar

조회 수: 26 (최근 30일)
Alessandro Maria Laspina
Alessandro Maria Laspina 2021년 6월 4일
편집: DGM 2021년 6월 4일
I have Z data for my x, and y data for which I would like to plot in a standard plot using the z data as a colorbar. I know that using the handle 'color' you can specify a nx3 vector for the n points of x and y data you have. If I use the standard 'colorbar' option, how can I create these color vectors based on the values of z of my data?
For example:
x=linspace(0,10)
y=linspace(0,10)
z=rand(1,100)
% now I want to plot z and convert the values in their respective colors
% where the highest color will be yellow and lowest color blue

채택된 답변

DGM
DGM 2021년 6월 4일
편집: DGM 2021년 6월 4일
This is an old method. I kind of wonder if there's anything newer that makes this simple...
% make a custom colormap
n = 256; % how many levels do you want?
cvu = linspace(0,1,n).';
cvd = flipud(cvu);
cmap = [cvu cvu cvd];
N = 100; % how many points to plot?
x=linspace(0,10,N);
y=linspace(0,10,N);
z = sin(linspace(-pi/2,pi/2,N)); % i picked something else
% instead of using plot(), use surf()
% because it can do color interpolation
h = surf([x(:) x(:)],[y(:) y(:)],[z(:) z(:)]);
set(h,'facecolor','none','edgecolor','interp');
set(h,'linewidth',3); % make it fat so it's easier to demonstrate
view(2); % only show 2-D view
colormap(cmap);
colorbar
To see what the 'interp' option does, set N = 10 and then run it. The plot will still be a uniform gradient. Set 'edgecolor' to 'flat' and run again. The line color will be 10 discrete steps.
Afaik, plot doesn't really have any way to utilize a colormap, interpolated or flat/faceted. Something like scatter() can do color interpolation, but afaik, you can't connect the markers.

추가 답변 (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