# Plot of 3D colour scatter graph

2 Ansichten (letzte 30 Tage)
Nicholas Omoding am 4 Jul. 2021
Kommentiert: Scott MacKenzie am 10 Jul. 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 Kommentare1 älteren Kommentar anzeigen1 älteren Kommentar ausblenden
Nicholas Omoding am 10 Jul. 2021
Z is the elevation of the points whilst erosion (relative change at each point is denoted by D). Please see the attached sub-set of the data.

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

Scott MacKenzie am 10 Jul. 2021
Bearbeitet: Scott MacKenzie am 10 Jul. 2021
I think this is more or less what you're after:
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;
##### 5 Kommentare4 ältere Kommentare anzeigen4 ältere Kommentare ausblenden
Scott MacKenzie am 10 Jul. 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.

Melden Sie sich an, um zu kommentieren.

### Weitere Antworten (2)

Image Analyst am 4 Jul. 2021
Bearbeitet: Image Analyst am 4 Jul. 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'
##### 1 KommentarKeine anzeigenKeine ausblenden
Nicholas Omoding am 10 Jul. 2021
Thanks for the response. Please kindly see the subset of my data attached.

Melden Sie sich an, um zu kommentieren.

Max Heiken am 4 Jul. 2021
Since the erosion is specified per vertex, I think scatter3 is to be preferred over surf or mesh.
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 Kommentare-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Surface and Mesh Plots finden Sie in Help Center und File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by