Plot of 3D colour scatter graph
조회 수: 2 (최근 30일)
이전 댓글 표시
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
채택된 답변
Scott MacKenzie
2021년 7월 10일
편집: Scott MacKenzie
2021년 7월 10일
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
Scott MacKenzie
2021년 7월 10일
Ok, very interesting. Thanks for this explanation. Good luck with your research.
Scott MacKenzie
2021년 7월 10일
@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.
추가 답변 (2개)
Image Analyst
2021년 7월 4일
편집: Image Analyst
2021년 7월 4일
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
2021년 7월 4일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Discrete Data Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!