generate a 3d scatter plot with z-support lines / pin plot / needle plot ...

48 visualizaciones (últimos 30 días)
Andres
Andres el 10 de Dic. de 2024 a las 11:02
Comentada: Star Strider el 11 de Dic. de 2024 a las 13:54
I'd like to plot scattered 3d data. To enhance readability, I want to add support lines in z-direction ("needles") to the data points with matching colors, see the image below.
Is there a specific name for such a plot?
To generate the chart, I used the code below with a for loop. I wonder if you have ideas how to simplify the code.
X = rand(20, 3);
f = figure;
hsc = scatter3(X(:,1),X(:,2),X(:,3), 20, X(:,3));
cmap = colormap('cool');
axis tight
xlabel("x"), ylabel("y"), zlabel("z")
title('needle plot')
box on
hold on
% draw support lines ("needles")
zlo = zlim*[1;0];
c_inter_fun = @(x) ...
interp1(linspace(zlo, zlim*[0;1], length(cmap)).' , cmap, x);
for k = 1:numel(hsc.XData)
plot3(hsc.XData(k)*[1 1], ...
hsc.YData(k)*[1 1], ...
[zlo, hsc.ZData(k)], ...
Color=c_inter_fun(hsc.ZData(k)));
end

Respuesta aceptada

Star Strider
Star Strider el 10 de Dic. de 2024 a las 11:24
See if the stem3 function will do what you want.
You will probably still have to use a loop of you want the individual stems and markers to be different colours, since the stem plots do not offer those options.
X = rand(20, 3);
f = figure;
hsc = stem3(X(:,1),X(:,2),X(:,3));
cmap = colormap('cool');
axis tight
xlabel("x"), ylabel("y"), zlabel("z")
title('needle plot')
box on
cm = cool(size(X,1));
f = figure;
hold on
for k = 1:size(X,1)
hsc = stem3(X(k,1),X(k,2),X(k,3), 'filled', Color=cm(k,:));
end
hold off
cmap = colormap('cool');
axis tight
xlabel("x"), ylabel("y"), zlabel("z")
title('needle plot')
box on
grid on
view(-27,30)
% hold on
% draw support lines ("needles")
% zlo = zlim*[1;0];
% c_inter_fun = @(x) ...
% interp1(linspace(zlo, zlim*[0;1], length(cmap)).' , cmap, x);
% for k = 1:numel(hsc.XData)
% plot3(hsc.XData(k)*[1 1], ...
% hsc.YData(k)*[1 1], ...
% [zlo, hsc.ZData(k)], ...
% Color=c_inter_fun(hsc.ZData(k)));
% end
.
  10 comentarios
Andres
Andres el 11 de Dic. de 2024 a las 13:08
Editada: Andres el 11 de Dic. de 2024 a las 13:08
If you just want to sort the colormap values along the z-value, you don't need to interpolate.
Two sorts
[~, si] = sort(X(:,3));
[~, si] = sort(si);
cm = cmap(si,:);
or one unique
[~, ~, si] = unique(X(:,3));
cm = cmap(si,:);
would do.
As I want to link the colormap values linearly to the z-value, the explicit interpolation seems unavoidable.

Iniciar sesión para comentar.

Más respuestas (1)

Jacob Mathew
Jacob Mathew el 10 de Dic. de 2024 a las 11:13
Hey Andres,
You can use stem3 to achieve the effect you want. Here is an example:
X = rand(20, 3);
figure;
hold on;
% Create stem plot (needles)
stem3(X(:,1), X(:,2), X(:,3), 'Color', 'k');
% Customize plot
colormap('cool');
axis tight;
xlabel('x');
ylabel('y');
zlabel('z');
title('Needle Plot');
box on;
view(45,45); % To view the plot from a angle since plot is not interactive below
You can refer to the documentation of stem3 below:

Categorías

Más información sobre Data Distribution Plots en Help Center y File Exchange.

Productos


Versión

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by