![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/641960/image.png)
Creating rgb vector based on Z data for standard colorbar
10 ビュー (過去 30 日間)
古いコメントを表示
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
0 件のコメント
採用された回答
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/641960/image.png)
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 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Colormaps についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!