The stem3 function in MATLAB creates 3D stem plots, but it does not directly support plotting multiple sets of values with different colors in a single call. To achieve your goal of plotting val1 in red and val2 in blue on the same 3D plot, you can call stem3 twice and use hold on to combine the plots.
Here’s how you can do it:
Example Code:
% Example data
A = [1, 1, 10, 15;
2, 2, 20, 25;
3, 3, 30, 35];
% Extract columns for clarity
x = A(:, 1); % X-coordinates
y = A(:, 2); % Y-coordinates
val1 = A(:, 3); % First set of Z-values
val2 = A(:, 4); % Second set of Z-values
% Plot val1 in red
stem3(x, y, val1, 'r', 'LineWidth', 1.5); % 'r' specifies red color
hold on;
% Plot val2 in blue
stem3(x, y, val2, 'b', 'LineWidth', 1.5); % 'b' specifies blue color
% Add labels and title
xlabel('X-axis');
ylabel('Y-axis');
zlabel('Z-axis');
title('Stem3 Plot with Multiple Colors');
% Turn grid on for better visualization
grid on;
% Optional: Adjust view angle
view(3); % Default 3D view
Explanation:
-
Separate Calls to
stem3:- First, plot
val1values in red using'r'. - Then, plot
val2values in blue using'b'.
- First, plot
-
Use
hold on:- This allows the second
stem3plot to be added to the existing figure without overwriting it.
- This allows the second
-
Customization:
- Adjust line width, marker size, or colors for better distinction.
- You can use RGB triplets (e.g.,
[0, 0, 1]for blue) if you need more precise color control.
-
Grid and View:
- Adding a grid and adjusting the view can make the plot easier to interpret.
Output:
The resulting 3D stem plot will have:
- Red stems corresponding to
val1. - Blue stems corresponding to
val2.
If you need further customization or encounter any issues, feel free to ask!
Need a Custom Version or Complete Simulation for This Problem?
Our 500+ PhD engineers build, debug, and optimize working MATLAB scripts and Simulink (.slx) models tailored to your exact assignment rubrics with zero plagiarism.
Explore similar technical troubleshooting questions and verified MATLAB solutions: