Azzera filtri
Azzera filtri

convert point from color map / color bar to color vector

19 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Adam Danz
Adam Danz il 2 Nov 2020
Modificato: Adam Danz il 3 Nov 2020
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

Più risposte (0)

Categorie

Scopri di più su Colormaps in Help Center e File Exchange

Prodotti


Release

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by