Plot a 3D figure on top of an other 2D/3D figure in one plot (different scalings)

8 views (last 30 days)
Hello comunity,
i try to combine two different plots. My task is to plot the second_plot on top of the first_plot and to keep both of the scaling for the axes (X and Y for the first and PHI and THETA for the second). The result should look something like the figure "combined_plot". For the first plot it does not matter if it will be plotted in 2D or 3D, also i know that the axes are interchanged, i will change it by myself. Thank you for your answers
first plot:
second plot:
combined:

Answers (1)

Nirupama
Nirupama on 27 Feb 2024
From the description you have provided, I believe you want to combine a 2D/3D plot with another 3D plot of different scalings.
In MATLAB, you can plot a 3D figure on top of another 2D or 3D figure within the same axes by calling the plot functions for each dataset sequentially.
However, if you need to maintain different scalings for each dataset, you will have to carefully manage the axis limits and possibly the viewing angles to ensure that both datasets are visible and appropriately scaled.
You can refer to the following sample code:
% Example data for the 2D plot
x2D = linspace(0, 2*pi, 100);
y2D = sin(x2D);
% Example data for the 3D plot
[X3D, Y3D] = meshgrid(linspace(-5, 5, 30), linspace(-5, 5, 30));
Z3D = sinc(sqrt(X3D.^2 + Y3D.^2));
% Create the figure
figure;
% Plot the 2D data
plot3(x2D, y2D, zeros(size(x2D)), 'm-', 'LineWidth', 2); % Plot as a flat curve in the 3D space
hold on; % Hold on to plot the 3D data on the same axes
% Plot the 3D data
surf(X3D, Y3D, Z3D, 'EdgeColor', 'none'); % Plot the 3D surface
colormap jet; % Color map for the 3D surface
% Set the viewing angle to better visualize both datasets
view(3); % Set to 3D viewing angle
% Set labels and title
xlabel('X-Axis');
ylabel('Y-Axis');
zlabel('Z-Axis');
title('3D Surface with 2D Plot Overlay');
% Add legend
legend('2D Plot', '3D Surface');
% Release the hold on the current axes
hold off;
In the above example, I first plotted the 2D data as a 3D curve by setting the z-coordinates to zero. Then, I made use of the "hold on" function to keep the current plot and overlay the 3D surface plot on top of it. The "view(3)" function sets the viewing angle to a 3D perspective.
If your 2D and 3D data have very different scales, you will need to carefully adjust the axis limits using "xlim", "ylim" and "zlim" to make sure both datasets are visible and appropriately scaled within the same plot. It is important to note that MATLAB does not support two different z-axis scales in the same axes, so if the scales are vastly different, the data may not be represented accurately.
If the difference in scale is too great and cannot be reconciled within a single set of axes, you can consider using subplots or creating a custom visualization that combines two separate axes objects in a more complex way.
You can refer to the following MATLAB documentation links for more details:
Hope this helps!

Community Treasure Hunt

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

Start Hunting!