Stack 2D line plots to create a 3D plot

조회 수: 19 (최근 30일)
Mai Sakuragi
Mai Sakuragi 2019년 11월 28일
편집: Saurabh Shukla 2021년 6월 9일
I am trying to stack 2D plots and create a 3D plot.
Below is a part of codes to solve the ODEs and compute FFT, and they are the data sets of 2D plots that I am trying to combine.
Fs = 4000;
tspan = 0 : 1/Fs :15;
conds = [0;0;0;0];
omega = 2262.72;
Omega = 148.328;
gamma = 1.519;
tau_opt = 0.1;
topt = 3;
f = @(t,Y) [Y(2) ; -omega^2 * Y(1) + Omega^2 * Y(3) - gamma * Y(2) + sin(omega*t)*exp(-4*log(2)*(t-topt)^2/tau_opt^2) ; Y(4) ; -omega^2 * Y(3) + Omega^2 * Y(1) - gamma * Y(4)];
[ts,ys] = ode45(f, tspan, conds);
x1sol = ys(:, 1);
Fourier1 = fft(x1sol);
x2sol = ys(:, 3);
Fourier2 = fft(x2sol);
Fourier1P = abs(Fourier1).^2 ;
Fourier2P = abs(Fourier2).^2 ;
F1 = 0: Fs/length(Fourier1): Fs - Fs/length(Fourier1);
F2 = 0: Fs/length(Fourier2): Fs - Fs/length(Fourier2);
j = topt+1.0 : 0.05 : topt+2.2
gammafunc = zeros(length(j),length(tspan));
for jj = 1:numel(j)
tTHz = j(jj);
gammafunc(jj,:) = 15*exp(-30*(tspan-tTHz).^2) + gamma;
txt = ['tTHz = ',num2str(tTHz)];
plot(tspan,gammafunc(jj,:),'DisplayName',txt); hold on
xlim([2.5 7.5])
title('γ_1(t)')
end
hold on
legend show
hold off
tsol = zeros(length(tspan), length(j));
G = zeros(length(tspan), 4 ,length(j));
for ii = 1:numel(j)
tTHz = j(ii);
[tsol(:,ii),G(:,:,ii)] = ode45( @(t,G) myode_2(t, G, omega, Omega, gamma, topt, tau_opt, tspan, gammafunc(ii,:)), tspan, conds);
end
fourier1 = zeros(length(tspan), length(j));
fourier2 = zeros(length(tspan), length(j));
fourier1P = zeros(length(tspan), length(j));
fourier2P = zeros(length(tspan), length(j));
f1 = zeros(length(j), length(tspan));
f2 = zeros(length(j), length(tspan));
for ii = 1:numel(j)
fourier1(:,ii) = fft(G(:,1,ii));
fourier2(:,ii) = fft(G(:,3,ii));
fourier1P(:,ii) = abs(fourier1(:,ii)).^2 ;
fourier2P(:,ii) = abs(fourier2(:,ii)).^2 ;
f1(ii,:) = 0: Fs/length(fourier1(:,ii)): Fs - Fs/length(fourier1(:,ii));
f2(ii,:) = 0: Fs/length(fourier2(:,ii)): Fs - Fs/length(fourier2(:,ii));
end
Below I am trying to stack the 2D plots and create a 3D surface plot. Now I am in the process of stacking the 2D plots.
delta_r = zeros(length(tspan), length(j));
for ii = 1:numel(j)
t_THz = j(ii);
delta_r(:,ii) = Fourier1P - fourier1P(:,ii);
end
figure
hold all
delay = zeros(1,length(j));
for ii = 1:numel(j)
t_THz = j(ii);
delay(ii) = t_THz - topt;
plot3(ones(length(delay))*delay(ii),F1, delta_r(:,ii))
end
hold off
xlim([358 363])
ylim([-1e-4 4.5e-4])
view(-50,20)
I have 17 2D plots, but F1 and delta_r(:,ii) are 60001 × 1 arrays. So I get an error saying "error using plot3. Vectors must be the same length"
I only have 17 plots to combine with, so I don't know how to fix the issue.
Any idea?

채택된 답변

Raunak Gupta
Raunak Gupta 2019년 12월 5일
Hi,
From the bottom-most code I understand you are trying to plot 17 different 2D plots with the x coordinate different for different plot and rest y and z coming from that corresponding plot. You may replace the plot3 command in the code with the following
plot3(ones(length(F1),1)*delay(ii),F1, delta_r(:,ii));
This should clear out the error you are facing.
If it is required to plot a 3D surface instead of stacking up which will have continuity between the 17 plots you may find below code useful.
[X,Y] = meshgrid(delay,F1);
surf(X,Y,delta_r,'EdgeColor','none');
Hope it helps.
  댓글 수: 1
Saurabh Shukla
Saurabh Shukla 2021년 6월 9일
편집: Saurabh Shukla 2021년 6월 9일
Hello Raunak, I have a very similar problem (the first case in your answer). I want to do something further.
After my linear plots( in y-z plane) are spread across x axis, I wish to drop a transparent plane in x-z direction, at a fixed y value, and show where this transparent plane has intersections with the line plot.
Is this possible anyhow?

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by