I am going to plot two 3D data with the help of below command lines. How can I insert legend for both the 3D data in a single window? plot3(x,y,z); hold on; plot3(xx,yy,zz);
Neeta Dsouza answered .
2025-11-20
You can add a legend to your 3D plot using the legend function in MATLAB. To specify legends for the two datasets in your plot3 commands, follow these steps:
% Sample data
x = 1:10; y = x.^2; z = sqrt(x);
xx = 1:10; yy = 2*x; zz = log(x);
% Plot the first 3D data
plot3(x, y, z, 'r', 'LineWidth', 2); % Red line
hold on;
% Plot the second 3D data
plot3(xx, yy, zz, 'b--', 'LineWidth', 2); % Blue dashed line
% Add legend
legend('First Dataset', 'Second Dataset');
% Add labels (optional)
xlabel('X-axis');
ylabel('Y-axis');
zlabel('Z-axis');
grid on;
Plotting Multiple Lines:
plot3 function is used twice to plot two 3D lines.Adding the Legend:
legend function takes strings corresponding to each dataset, in the same order as the plotting commands.'First Dataset') corresponds to the first call to plot3.'Second Dataset') corresponds to the second call to plot3.Optional Customizations:
'Location', 'northwest' to the legend function.xlabel, ylabel, and zlabel to label the axes.This will create a single 3D plot with two lines, and a legend indicating which line corresponds to each dataset. Let me know if you need further clarification!