# Plot 3D Surface with 4D Colour - Can not Create Surface, only Data Points

10 views (last 30 days)
Ioanna-Elena Golema on 8 Mar 2023
Answered: Star Strider on 8 Mar 2023
Hello Guys,
I want to plot my 3 Data points (z, x1, x2) in a 3D plot and use the Data points from c to add a colour bar.
Basically that, which is shown in the attached picture, but i want the points in the picture to be a surface.
I tried using both mesh and surf for achieving that but the error shows me that 'Value must be a vector or 2D array of numeric type'. The thing is, I thought my values (z, x1, x2, c) are already a vector. Therefore I do not know what to change and how I need to proceed.
Here is my code, thank you in advance!:)
clear all
%Diagram 1 Data Points
z=rand(1,39);
x1=rand(1,39);
x2=rand(1,39);
%Diagram 2 Data Points for Colour
c=rand(1,39);
%Set of Data Points
scatter3(x1,x2,z,10,c,'filled');
colorbar
%Interpolate Data onto grid
[X,Y,Z]= meshgrid(30:1:90, 5:0.2:19, 0:1000:6000);
C=griddata(x1,x2,z,c,X,Y,Z);
% mesh(X,Y,Z,C);
% surf(X,Y,Z,C);
colorbar Star Strider on 8 Mar 2023
The surface function allows the apploication of a specific color matrix to a surface plot. (The surf function does not.)
Try something like this —
% clear all
%Diagram 1 Data Points
z=rand(1,39);
x1=rand(1,39);
x2=rand(1,39);
%Diagram 2 Data Points for Colour
c=rand(1,39);
%Set of Data Points
figure
scatter3(x1,x2,z,10,c,'filled');
colormap(turbo) % R2020a+
colorbar %Interpolate Data onto grid
% [X,Y]= meshgrid(30:1:90, 5:0.2:19)%, 0:1000:6000);
x1v = linspace(min(x1), max(x1), numel(x1));
x2v = linspace(min(x2), max(x2), numel(x2));
[X,Y] = meshgrid(x1v,x2v);
Z = griddata(x1(:),x2(:),z(:),X,Y);
C=griddata(x1,x2,c,X,Y);
figure
surf(X,Y,Z)
grid on
xlabel('X')
ylabel('Y')
zlabel('Z')
colormap(turbo) % R2020a+
colorbar
[az,el] = view;
title('Original ‘surf’ Plot') figure
surface(X,Y,Z,C)
grid on
xlabel('X')
ylabel('Y')
zlabel('Z')
colormap(turbo) % R2020a+
colorbar
view(az,el)
title('Original ‘surf’ Plot With Colours Definmec By ‘C’') The vectors used in the meshgrid call did not conform to the actual sizes of the vectors being used to create the matrices. I changed it so it would work. Experiment with your own data with this approach.
.

### 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!