4D plot with matlab
Show older comments
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)
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
Adam Danz
on 2 Apr 2019
Feel free to follow-up here if you get stuck or want to suggest a different solution.
Shirin Muhammad
on 2 Apr 2019
Adam Danz
on 2 Apr 2019
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?
Adam Danz
on 2 Apr 2019
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)])
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)])
Shirin Muhammad
on 2 Apr 2019
Shirin Muhammad
on 2 Apr 2019
Shirin Muhammad
on 2 Apr 2019
Adam Danz
on 2 Apr 2019
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.
Shirin Muhammad
on 2 Apr 2019
Adam Danz
on 2 Apr 2019
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')
Shirin Muhammad
on 2 Apr 2019
Adam Danz
on 2 Apr 2019
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.
Shirin Muhammad
on 2 Apr 2019
Adam Danz
on 2 Apr 2019
I feel like there's a better solution. If you find one, please share! :)
Shirin Muhammad
on 3 Apr 2019
Shirin Muhammad
on 5 Apr 2019
Edited: Shirin Muhammad
on 5 Apr 2019
Adam Danz
on 5 Apr 2019
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.
Shirin Muhammad
on 2 Apr 2019
0 votes
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!