Creating rgb vector based on Z data for standard colorbar

27 views (last 30 days)
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

Accepted Answer

DGM
DGM on 4 Jun 2021
Edited: DGM on 4 Jun 2021
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.

More Answers (0)

Categories

Find more on Colormaps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!