2D and 3D Plotting Flashcards
6 cards from real MATLAB practice questions. Tap to flip, then mark Knew It or Still Learning โ missed cards come back until you master them.
Read the first 6 2D and 3D Plotting flashcards as text
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?
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.
Which of the following commands is used to create a 3D line plot (a helix, for example) from vectors `x`, `y`, and `z`?
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.
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?
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.
What is the primary difference between the `surf(X,Y,Z)` and `mesh(X,Y,Z)` functions when visualizing a 3D dataset?
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.
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`?
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'.
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?
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.