How do I plot xy plots into one 3D plot?

Hello dear community!
I am trying to make a 3D plot from my data. Each dataset includes depth measurements and absorbance measurements, which I usually plot in 2D as x = depth and y = absorbance. I saved all datasets as equal-size matrices with depth in the first column and absorbance in the second. There are multiple datasets per year, which are more or less evenly spaced (40 datasets per year), so I would like to combine them all in one plot, preferably a surface plot. As an additional variable I thought I could just use a vector (0:1:40) to distribute my depth/absorbance values according to that.
All tutorials that I found include some functions for z, but I don't have any, so I am not sure how to manage this.
Your help is really appreciated, thanks!
Eva

8 Comments

If you have only (x,y) how you can plot into 3D?
I would like to plot a time series, kind of like here:
only instead of bars I would just like to have dots/lines or a surface or a grid between the lines. So the 'year' would stay in the same axis as in the plot above, but 'month' would be 'depth' and 'temperature' would be 'absorbance'
You could define z as a combination of the year and the nth dataset from that year. But the z axis wouldnt have any meaningful value as such.
I don't really know how you mean..? But yeah the axis doesn't have a really meaningful value, exept that it facilitates stacking of my 2D plots (the backgroud is that I just don't want to have 40 single 2D plots per analysis year but just one plot per year)
Have you tried scatter3 or surf ?
For X use depth, for Y use your time vector (0:1:40) and for Z use absorption.
For the Y-vector, consider that each time value has to be repeated N times, if N is the number of points at that time
% e.g.
N = 3;
t = [1 1 1 2 2 2 3 3 3];
You might want to use repelem for this.
Thanks a lot this was really helpful! I tried with a data subset and I think I adjusted all parameters:
data1 = readmatrix('01_24_2008_for_3D_analysis');
data2 = readmatrix ('02_05_2008_for_3D_analysis');
data3 = readmatrix ('02_19_2008_for_3D_analysis');
t = (1:1:3);
depth1 = data1(:,1);
depth2 = data2(:,1);
depth3 = data3(:,1);
crypto1 = data1(:,2);
crypto2 = data2(:,2);
crypto3 = data3(:,2);
combCrypto = [crypto1 crypto2 crypto3];
combDepth = [depth1 depth2 depth3];
n = 1001;
repT = repelem(t, n);
repT = transpose(repT);
X = combDepth;
Y = repT;
Z = combCrypto;
[X,Y] = meshgrid(X,Y);
surf(X,Y,Z);
sadly I still get the error that the dimensions do not agree.
X and Y after meshgrid are 3003x3003 double and Z is 1001x3 double...
Sry, my mistake, the instructions on how to assign X, Y and Z should work with scatter3, but not surf.
For surf, all matrices must be of the same size since you want to have depth and time represented in the x y plane, the matrix size (in your example) for X and Y would need to be 3x1001 (assuming that the data in the files is 1001x2. Thus Z must also be 3x1001.
In general, try to avoid enumerating your variable names. This makes looping over data much easier
filenames = {'01_24_2008_for_3D_analysis', ...
'02_05_2008_for_3D_analysis', ...
'02_19_2008_for_3D_analysis'};
for ii = 1:numel(filenames)
%data = readmatrix(filenames{ii});
data = [(1:5)+rand(1, 5); ii*ones(1, 5)+rand(1, 5)]';
depths(:, ii) = data(:, 1);
crypto(:, ii) = data(:, 2);
end
t = repmat(1:numel(filenames), [size(depths,1) 1]);
figure
surf(depths, t, crypto)
Thanks so much Johannes, works like a charm!

Sign in to comment.

Answers (0)

Asked:

on 30 Jun 2020

Commented:

on 3 Jul 2020

Community Treasure Hunt

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

Start Hunting!