How to plot 3d data with values associated at each point?
Show older comments
I have a list of nodes (3 coordinate points for each one). I also have associated a value at each node. How can I represent a 3d plot of all the nodes, colored with the value associated?
For example:
if true
1.35846579 -0.010903161 -1.35846579 -2.466e-05
2.71686196 -0.033203464 -2.71686196 -4.506e-05
4.07514048 -0.066900126 -4.07514048 -6.485e-05
5.43325424 -0.111991957 -5.43325424 -8.391e-05
6.79115438 -0.168477371 -6.79115438 -0.000102
end
Where the first 3 columns are X,Y,Z coordinates of the points, and last column is the value associated that i want to represent.
Thank you very much
Answers (1)
Star Strider
on 5 May 2014
This is not as neat or efficient as I would like it to be, but it works:
s = [1.35846579 -0.010903161 -1.35846579 -2.466e-05
2.71686196 -0.033203464 -2.71686196 -4.506e-05
4.07514048 -0.066900126 -4.07514048 -6.485e-05
5.43325424 -0.111991957 -5.43325424 -8.391e-05
6.79115438 -0.168477371 -6.79115438 -0.000102];
cr = colormap(jet);
s1 = (abs(s(:,4)/max(s(:,4))));
s2 = ceil(s1*63/max(s1))
mc = cr(s2,:)
figure(1)
hold on
for k1 = 1:size(s,1)
stem3(s(k1,1), s(k1,2), s(k1,3), 'fill', 'MarkerFaceColor', mc(k1,:))
end
hold off
grid on
view(-30, 30)
I used a stem3 plot because it’s easier to see the relative positions of the data. Experiment with it to get the result you want. This works with scatter3 as well.
5 Comments
stefanos theocharis
on 9 Apr 2020
Dear Star Strider, how could this code work with surf command ?
Thanks in regard.
Walter Roberson
on 9 Apr 2020
Edited: Walter Roberson
on 9 Apr 2020
This kind of task cannot be done with surf(). surf() needs grids of coordinates, but this task involves scattered x, y, z triples each of which has an associated value. You can use scatteredInterpolant() to interpolate from the scattered points to what the values would be inside a cuboid, but surf() cannot draw cuboids.
This would be a task better suited for patch() or mesh() -- but both of those require information about the connections between points, which you do not have. Perhaps use one of the triangulation routines and trisurf() .
stefanos theocharis
on 11 Apr 2020
Dear Walter thank you very much for your answer.
my data are 4 columns of 26 elements each one, first 3 are xyz coordinates of points in 3d space and the fourth is an indipendent value associated at each point. How could i present it as a surface or smthg else?
Thanks in regard.
Star Strider
on 11 Apr 2020
Walter Roberson
on 11 Apr 2020
The last time I encountered data in 26 columns like that, it was information about molecules, and included information about which atoms were linked to which other atoms. Would that happen to be the kind of information you have?
Does your data represent a set of points sampled from a volume, and your goal is to interpolate the data throughout the volume? If so then are you wanting to create a voxelized view, or are you wanting to draw isosurfaces?
Does your data represent a set of points on the surface of a convex object, and you want to build faces and edges? Data from a non-convex object that has holes but none-the-less there are distinct implied surfaces? Data from a point cloud that you would like to draw an outside boundary of?
Categories
Find more on Surface and Mesh Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!