Plot of 3D colour scatter graph
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Nicholas Omoding
le 4 Juil 2021
Commenté : Scott MacKenzie
le 10 Juil 2021
Hello,
I have a 3D data set of surface erosion for over 700,000 points, and this is tabulated in the form (x, y, z, D). That is, for each 3D point (x, y, z), there is a corresponding value of surface erosion suffered (D)?
Please could someone assist me to visualise this data such that erosion depth controls the colour.
Thanks.
2 commentaires
Réponse acceptée
Scott MacKenzie
le 10 Juil 2021
Modifié(e) : Scott MacKenzie
le 10 Juil 2021
I think this is more or less what you're after:
f = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/679873/point_erosion.xlsx';
M = readmatrix(f);
x = M(:,1);
y = M(:,2);
z = M(:,3);
D = M(:,4);
N = 250; % faster with downsampling; looks the same
xv = linspace(min(x), max(x), N);
yv = linspace(min(y), max(y), N);
[Xm,Ym] = ndgrid(xv, yv);
Zm = griddata(x, y, z, Xm, Ym);
Dm = griddata(x, y, D, Xm, Ym);
surf(xv,yv,Zm,Dm, 'edgecolor', 'none');
xlabel('X'); ylabel('Y'); zlabel('Z');
cb = colorbar;
cb.Label.String = 'Erosion';
cb.Label.FontSize = 12;
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/680108/image.jpeg)
5 commentaires
Scott MacKenzie
le 10 Juil 2021
Ok, very interesting. Thanks for this explanation. Good luck with your research.
Scott MacKenzie
le 10 Juil 2021
@Nicholas Omoding Just one final thought. You probably want to add a colorbar to the graph, to reveal what the color data represent. I just tweaked my answer to include this.
Plus de réponses (2)
Image Analyst
le 4 Juil 2021
Modifié(e) : Image Analyst
le 4 Juil 2021
Is this what you want?
% Scatter3 demo where marker color and size varies according to data value.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 15;
% Create sample data.
numPoints = 300;
x = rand(1, numPoints); % Coordinates
y = rand(1, numPoints);
z = rand(1, numPoints);
maxDataValue = 1000; % Whatever you want the most extreme color to represent.
minDataValue = -200; % Whatever you want the most extreme color to represent.
D = minDataValue + (maxDataValue - minDataValue) * rand(1, numPoints); % Data values might not ever reach minDataValue or maxDataValue
% Define a colormap
numColors = 256;
cmap = jet(numColors);
%=====================================================================================================================
% Demo #1 : Vary size and color according to magnitude of data.
% Get the index (color) for each of the D values
DScaled = (D - minDataValue) / (maxDataValue - minDataValue);
colorIndexes = ceil(numColors * DScaled);
colors = cmap(colorIndexes, :);
% Scale marker sizes so that bigger values get bigger markers.
sizes = rescale(D, 50, 150);
subplot(1, 2, 1);
scatter3(x, y, z, sizes, colors, 'filled');
grid on;
axis equal;
colormap(cmap);
colorbar;
caxis([0, maxDataValue])
xlabel('x', 'FontSize', fontSize);
ylabel('y', 'FontSize', fontSize);
zlabel('z', 'FontSize', fontSize);
title('Marker Color and Size Varied According to Data Value', 'FontSize', fontSize);
%=====================================================================================================================
% Demo #2 : Vary size and color according to distance of data from origin.
distances = sqrt(x.^2 + y.^2 + z.^2);
maxDistanceValue = sqrt(3);
% Get the index (color) for each of the D values
DScaled = distances / maxDistanceValue;
colorIndexes = ceil(numColors * DScaled);
colors = cmap(colorIndexes, :);
% Scale marker sizes so that bigger values get bigger markers.
sizes = rescale(D, 50, 150);
subplot(1, 2, 2);
scatter3(x, y, z, sizes, colors, 'filled');
grid on;
axis equal;
colormap(cmap);
colorbar;
caxis([0, maxDistanceValue])
xlabel('x', 'FontSize', fontSize);
ylabel('y', 'FontSize', fontSize);
zlabel('z', 'FontSize', fontSize);
title('Marker Color and Size Varied According to Distance from Origin', 'FontSize', fontSize);
g = gcf;
g.WindowState = 'maximized'
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/674123/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/674128/image.png)
Max Heiken
le 4 Juil 2021
Since the erosion is specified per vertex, I think scatter3 is to be preferred over surf or mesh.
I would start with something straight-forward like
scatter3(x, y, z, [], D, '.')
cb = colorbar;
ylabel(cb, "erosion")
colormap copper % change for aesthetics
With 700000 points in one plot, performance will be bad, so depending on that you might need to perform some data reduction.
After observing the scatter plot, you can of course decide if another type of plot would be more appropriate.
0 commentaires
Voir également
Catégories
En savoir plus sur Discrete Data Plots dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!