Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make colorbar refer to markers in 3D-plot and not the surface in Matlab

I have a plot of the following type:

enter image description here

I have created the black-white surface using the surf-function and the markers I have plotted using plot3-function. For each marker I have defined a colormap between Red-Yellow-Green depending on the value of the marker. As you can see from the plot the colorbar refers at the moment to the surface but I would like it to refer to the markers. How can I do this?

Thank you!

Here is MWE basically showing what I do now:

% Plot the surface with a colormap
Z = peaks;
Z = peaks./max(Z(:));
Z = (Z+1)*3/2;
surf(Z)
colormap(flipud(gray))
shading interp
hold on

% Create the 4-dimensional marker data
x = (50-10).*rand(50,1) + 10;
y = (50-10).*rand(50,1) + 10;
z = (3-1).*rand(50,1) + 1;
q = 5.*rand(50,1); % This dimension is used to select the color

% Create the color map for the markers
c1=[0 1 0]; %G
c2=[1 1 0]; %Y
c3=[1 0 0]; %R
n1 = 20;
n2 = 20;
cmap=[linspace(c1(1),c2(1),n1);linspace(c1(2),c2(2),n1);linspace(c1(3),c2(3),n1)];
cmap(:,end+1:end+n2)=[linspace(c2(1),c3(1),n2);linspace(c2(2),c3(2),n2);linspace(c2(3),c3(3),n2)];
cmap = cmap';

% Select the colors for the markers
marker_colors = zeros(size(50, 1), 3);
q_interval = max(q)-min(q);
q_int = q_interval/(n1+n2);
q_vals = zeros(n1+n2,1);
q_vals(1) = min(q);
for i = 2:size(q_vals,1)
    q_vals(i) = min(q) + (i-1)*q_int;
end
for i = 1:50
    d = abs(q_vals - q(i));
    ind = find(d == min(d));
    marker_colors(i,:) = cmap(ind,:);
    % Plot the marker
    plot3(x(i), y(i), z(i), 'o', 'color', [0 0 0], 'MarkerFaceColor', marker_colors(i,:))
end

% Lastly I plot the colorbar, which refers to the surface... :/ 
colorbar
like image 544
jjepsuomi Avatar asked May 12 '16 09:05

jjepsuomi


People also ask

How do I set the position bar color in Matlab?

To move the colorbar to a different tile, set the Layout property of the colorbar. To display the colorbar in a location that does not appear in the table, use the Position property to specify a custom location. If you set the Position property, then MATLAB sets the Location property to 'manual' .

Which command used to create surface plot with lighting in Matlab?

surfl( Z ) creates a surface and uses the column and row indices of the elements in Z as the x- and y-coordinates. surfl(___,'light') creates a surface with highlights from a MATLAB® light object. This produces different results from the default colormap-based lighting method.

What does Colorbar mean in Matlab?

Colorbars allow you to see the relationship between your data and the colors displayed in your chart. After you have created a colorbar, you can customize different aspects of its appearance, such as its location, thickness, and tick labels.


1 Answers

If you do not rely on plot3 function you can use scatter3 instead.

%% Data for surface
[X,Y]=meshgrid(-2:.1:2, -2:.1:2);
Z=max(0,peaks(X,Y));
C=1-cat(3,Z,Z,Z)/max(Z(:));

%% data for points
x=(rand(50,1)*4)-2;
y=(rand(50,1)*4)-2;
z=max(0,peaks(x,y));
c=0.5*rand(50,1);

%% Colormap
c1=[0 1 0]; %G
c2=[1 1 0]; %Y
c3=[1 0 0]; %R
n1 = 20;
n2 = 20;
cmap=[linspace(c1(1),c2(1),n1);linspace(c1(2),c2(2),n1);linspace(c1(3),c2(3),n1)];
cmap(:,end+1:end+n2)=[linspace(c2(1),c3(1),n2);linspace(c2(2),c3(2),n2);linspace(c2(3),c3(3),n2)];
cmap = cmap';

%% Create surface with texture defined in C
surf(X,Y,Z,C)
shading interp
hold on

%% plot points in coordinates x,y,z with markers with 12 pt in diameter,
%% coloured according to c values, filled and with black marker edges.
scatter3(x,y,z,12,c,'filled','markerEdgeColor','k')

%% set colormap (change will apply only for scatter because surf uses texxture map)
colormap(cmap)
colorbar
like image 166
Crowley Avatar answered Sep 24 '22 03:09

Crowley