4D plot with matlab

I have the attached data, in which the first three columns are variables, the fourth column data was obtained as a function of the variables c1,c3, and c4.
Can any one tell me how I can plot them together?
Thank you.

Answers (2)

Adam Danz
Adam Danz on 1 Apr 2019
Edited: Adam Danz on 2 Apr 2019
It sounds like you've got data (stored in column 4) that vary on 3 axes (stored in cols 1:3). Perhaps a 3D contour plot or a 3D surface plot can be used where the (x,y,z) coordinates are defined by your columns 1,2,3 and the color is defined by the data in column 4. Check out the documentation for those functions and let us know if you get stuck.
Another option is to use a 3D scatter plot and color each marker according to the value in the 4th dimension.
figure
h = scatter3(data(:,1), data(:,2), data(:,3), 20, data(:,4), 'MarkerFaceColor', 'Flat');
colorbar();
caxis([min(data(:,4)), max(data(:,4))])

18 Comments

Feel free to follow-up here if you get stuck or want to suggest a different solution.
Many thanks to your answer. really I am beginner in Matlab, Could you explain to me more?
I tried to use contour3(c1,c3,c4), it returned to me an error indicating that c4 must be at least 2*2 matrix.
I think surf() will be easier to implement. Do you have a strong preference to use contour3? Could you attach your data in a mat file?
Here's a quick solution using a 3D scatter plot.
figure
h = scatter3(c1, c3, c4, 20, p1, 'MarkerFaceColor', 'Flat');
colorbar();
caxis([min(p1), max(p1)])
Adam Danz
Adam Danz on 2 Apr 2019
Edited: Adam Danz on 2 Apr 2019
Here's a quick solution suing surf() although the data might not be arranged the way they are intended.
c1mat = reshape(c1, length(unique(c1)), []);
c3mat = reshape(c3, length(unique(c3)), []);
c4mat = reshape(c4, length(unique(c4)), []);
p1mat = reshape(p1, length(unique(c1)), []);
figure;
h = surf(c1mat, c3mat, c4mat, p1mat);
colorbar();
caxis([min(p1), max(p1)])
Thank you again, it's worked. but how I can make it more understandable?
It is important to display at which value of a1, a3 , and a4 I have got minimum value of p1.
regards
I have tried the following
[a1,a3,a4] = peaks(50);
figure
surf(a1,a3,a4)
and I have got the attached figure
How I can add p1 with it?
When only 3 inputs are specified in surf() the color data comes from the 3rd input which is also the z-coordinate.
Check out my implementation or surf or look at the documentation. There is a 4th input where you can specify the color.
However, all 4 inputs are matricies. You have to convert your data to matrix format if you want to use surf(). My implementation does that using reshape.
I checked it and saw the result.
But what I need is that by plotting a1,a3,a4, and p1 together , the reader can easily find the coordinates of a1,a3,and a4 that gave the specific value of p1.
The most important point is minimum value of p1.
Perhaps a much simpler plot like this be suitable. There are 3 subplots: one for c1, c3 & c4. The value of p1 is plotted along the y axis so you can easily see where the minimum is for each C-variable. The title of each subplot indicates the value of c* where p1 is minimum.
figure
[~, mn] = min(p1);
subplot(3,1,1)
plot(c1, p1, 'o')
hold on
plot(c1(mn), p1(mn), 'm*')
title(sprintf('c1=%.2f', c1(mn)))
xlabel('c1')
subplot(3,1,2)
plot(c3, p1, 'o')
hold on
plot(c3(mn), p1(mn), 'm*')
title(sprintf('c3=%.2f', c3(mn)))
ylabel('p1')
xlabel('c3')
subplot(3,1,3)
plot(c4, p1, 'o')
hold on
plot(c4(mn), p1(mn), 'm*')
title(sprintf('c4=%.2f', c4(mn)))
xlabel('c4')
this will be great when I am studing the effect of changing the variables separately. for example as you see in the first subplot minimum p1 is found at a1=0.54, but if some one needs to know the values of c3 and c4 at that point, how they can be found?
for this reason I am seeking for the 4 dimentional plot.
The value of c3 and c4 at that point are shown in subplots 2 and 3. c3 = 1.28 and c4 = 1.41.
This can also be adapted to find other p1 values. For example, if you want to find the (c1,c3,c4) coordinate of the 4th p1 value (-13.476), use
mn = p1==p1(4);
%instead of [~, mn] = min(p1);
and the c1, c3, & c4 coordinates will be marked with a magenta asterisk and their values will appear in the title.
Now I understood you. thank you for your help and time. I am highly appritiate it.
I feel like there's a better solution. If you find one, please share! :)
I think so, I am trying to find another solution. Now I am trying to plot all of them together in one surface, or by contour ploting. I wll going to try mesh as well.
Shirin Muhammad
Shirin Muhammad on 5 Apr 2019
Edited: Shirin Muhammad on 5 Apr 2019
Is there any one who can help me by ploting the data data1.png using contour3?
thanks
After recommending contour3() I realized this isn't really an option since it only receives (X,Y,Z) values. My initial thought was to change the colors to your values in the 4th column but I'm no longer sure that's possible. I looked at the 2nd output to contour3() which is the handle to the plot and it wasn't immediately apparent how to access and change the color values.

Sign in to comment.

Shirin Muhammad
Shirin Muhammad on 2 Apr 2019

0 votes

I don't have any preference to use contour3 . My data is attached. thank you for your time.

Asked:

on 1 Apr 2019

Commented:

on 5 Apr 2019

Community Treasure Hunt

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

Start Hunting!