필터 지우기
필터 지우기

convert point from color map / color bar to color vector

조회 수: 19 (최근 30일)
Dan H
Dan H 2020년 11월 2일
편집: Adam Danz 2020년 11월 3일
Hello,
how can I convert obtain a "standard" 3-dim. color vector data from a some color map / color bar?
Example:
With the "scatter3" command, it is possible to include a continuous coloring of the data points, e.g.
x = 0 : 0.1 : 10;
y = sin(x) + 1;
z = sin(y)^2;
point_size = 2*ones(size(x));
my_color = x;
scatter3(x,y,z, point_size, my_color);
In this example, the variable "my_color" is a vector, and its values are automatically spaced / assigned to some color map .
In case I want to re-use a specific color, e.g. the color that was assigned to the value "5" in the above example, how can I convert this single number to the 3-number color code used for other color parameter (e.g. [0.5, 0.3, 0.7])
plot(x, y, 'color', [0.5, 0.3, 0.7]);
Thanks for any advice,
Dan

채택된 답변

Adam Danz
Adam Danz 2020년 11월 2일
편집: Adam Danz 2020년 11월 3일
Background info
scatter & scatter3 use the axes' colormap along with the color axis limit (caxis|ax.CLim) to set the color of the markers.
To get the color of marker at index n, scale the scatter object's CData which contains the color scale value to the range of the colormap.
Solution
Use an anonymous function that receives an index value and returns the RGB vector associated with that data point. Set the colormap, color limit (caxis|ax.CLim) and the scatter object before creating the anonymous function.
ax = gca();
h = scatter3(___);
cmap = ax.Colormap;
clim = caxis(ax);
getRGB = @(i)cmap(round((h.CData(i)-clim(1))/range(clim) * (size(cmap,1)-1) + 1),:);
% Example: Get RGB values for 4th data point in h
% getRGB(4)
% ans = [0.2576 0.1814 0.7501]
Alternatively the axis and scatter handles could be included as inputs if you expect them to change.
getRGB = @(i,h,ax)ax.Colormap(round((h.CData(i)-ax.CLim(1))/range(ax.CLim) * (size(ax.Colormap,1)-1) + 1),:);
% Example: Get RGB values for 4th data point in h
% getRGB(4,h,ax)
% ans = [0.2576 0.1814 0.7501]
Demo
Circle markers are added to verify that their color matches the original data.
% Set up scatter3 plot
x = 0 : 0.1 : 10;
y = sin(x) + 1;
z = sin(y).^2; %added '.'
point_size = 2*ones(size(x));
my_color = x;
h = scatter3(x,y,z, point_size, my_color); % added handle output
colorbar % added
% Create a function to return the RGB value given an index value
ax = gca();
cmap = ax.Colormap;
clim = caxis(ax);
% create function: rgb = getRGB(i)
% Create this after creating the scatter3 object, colormap, and color limits.
getRGB = @(i)cmap(round((h.CData(i)-clim(1))/range(clim) * (size(cmap,1)-1) + 1),:);
% Test it by adding o markers around each point, matching colors
hold on
for i = 1:numel(h.XData)
plot3(h.XData(i), h.YData(i), h.ZData(i), 'o','Color', getRGB(i))
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Colormaps에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by