MATLAB 2D and 3D Plotting Questions and Answers — Questions and Answers
Question 1: A data analyst wants to display four different 2D plots in a single figure window, arranged in a 2x2 grid. After creating the first plot in the top-left position, which command should they use to select the bottom-right position for the fourth plot?
- subplot(2,2,4) (Correct answer)
- subplot(4,4,4)
- subplot(2,2,1)
- axes(2,2,4)
Correct answer: subplot(2,2,4)
The `subplot(m,n,p)` function divides the current figure into an m-by-n grid and creates axes in the position specified by p. For a 2x2 grid, m=2 and n=2. The positions are numbered row-wise, so the top-left is 1, top-right is 2, bottom-left is 3, and bottom-right is 4.
Question 2: Which of the following commands is used to create a 3D line plot (a helix, for example) from vectors `x`, `y`, and `z`?
- plot(x,y,z)
- mesh(x,y,z)
- plot3(x,y,z) (Correct answer)
- surf(x,y,z)
Correct answer: plot3(x,y,z)
The `plot3` function is specifically designed to plot lines and points in 3D space. It takes vectors representing the (x, y, z) coordinates of the points to be connected. `plot` is for 2D plots, while `mesh` and `surf` are for creating 3D surfaces.
Question 3: A user has generated a 2D plot and wants to ensure that any subsequent plotting commands add new lines to the same plot instead of replacing it. They also want to add a descriptive legend. Which sequence of commands is appropriate?
- figure; plot(x1,y1); plot(x2,y2); legend('Data 1', 'Data 2');
- plot(x1,y1); hold on; plot(x2,y2); legend('Data 1', 'Data 2'); (Correct answer)
- subplot(1,2,1); plot(x1,y1); subplot(1,2,2); plot(x2,y2); legend('Data 1', 'Data 2');
- plot(x1,y1,x2,y2); label('Data 1', 'Data 2');
Correct answer: plot(x1,y1); hold on; plot(x2,y2); legend('Data 1', 'Data 2');
The `hold on` command retains the current plot and all axis properties so that subsequent graphing commands add to the existing graph. Without it, the second `plot` command would overwrite the first. The `legend` command then correctly labels the two data series that have been plotted.
Question 4: What is the primary difference between the `surf(X,Y,Z)` and `mesh(X,Y,Z)` functions when visualizing a 3D dataset?
- `mesh` creates a wireframe with colored edges, while `surf` creates a solid surface with colored faces. (Correct answer)
- `surf` is used for 2D data, while `mesh` is used for 3D data.
- `mesh` connects data points with straight lines, while `surf` uses a polynomial interpolation.
- There is no functional difference; they are aliases for the same plotting function.
Correct answer: `mesh` creates a wireframe with colored edges, while `surf` creates a solid surface with colored faces.
`mesh(X,Y,Z)` creates a 3D wireframe plot where only the lines connecting the points (the 'edges') are colored. In contrast, `surf(X,Y,Z)` creates a 3D surface plot where the faces of the polygons are filled with color.
Question 5: After creating a line plot with the command `p = plot(x, y);`, how can you change the color of the line to red using the plot handle `p`?
- p.LineColor = 'red';
- set(p, 'Color', 'red'); (Correct answer)
- change(p, 'color', 'r');
- color(p, 'red');
Correct answer: set(p, 'Color', 'red');
Modern MATLAB (since R2014b) allows setting properties using dot notation (`p.Color = 'red'`). However, the `set(handle, 'PropertyName', 'PropertyValue')` syntax is the traditional and universally compatible way to modify graphics object properties after they have been created. The property name for the line color is 'Color'.
Question 6: A user wants to create a 2D plot of `sin(x)` and `cos(x)` on the same axes and label them appropriately in a legend. Which of the following code snippets will achieve this correctly?
- x = 0:0.1:2*pi; plot(x, sin(x)); plot(x, cos(x)); legend('sin', 'cos');
- x = 0:0.1:2*pi; plot(x, sin(x), x, cos(x)); legend('sin(x)', 'cos(x)'); (Correct answer)
- x = 0:0.1:2*pi; plot(x, sin(x)); legend('sin(x)'); hold on; plot(x, cos(x)); legend('cos(x)');
- x = 0:0.1:2*pi; plot(x, sin(x) + cos(x)); legend('sin(x) and cos(x)');
Correct answer: x = 0:0.1:2*pi; plot(x, sin(x), x, cos(x)); legend('sin(x)', 'cos(x)');
The `plot` function can accept multiple pairs of x,y data to plot on the same axes in a single command (`plot(x1,y1,x2,y2,...)`). The `legend` function then takes a list of strings to label each line in the order it was plotted. This is a concise and effective way to create the desired plot.
A data analyst wants to display four different 2D plots in a single figure window, arranged in a 2x2 grid.
After creating the first plot in the top-left position, which command should they use to select the bottom-right position for the fourth plot?