Look at the code below that creates two plots in one figure using matplotlib. What will be the shape of the axes object?
import matplotlib.pyplot as plt fig, axes = plt.subplots(1, 2) print(type(axes)) print(axes.shape)
When you create multiple subplots in one row and multiple columns, axes is a 1D array with shape (2,).
The plt.subplots(1, 2) creates 1 row and 2 columns of plots. The axes object is a numpy array with shape (2,).
This code creates a figure with 2 rows and 1 column of plots. Each subplot plots two lines. How many lines are drawn in total?
import matplotlib.pyplot as plt import numpy as np x = np.linspace(0, 10, 100) fig, axes = plt.subplots(2, 1) for ax in axes: ax.plot(x, np.sin(x)) ax.plot(x, np.cos(x)) print(sum(len(ax.lines) for ax in axes))
Count how many lines each subplot has, then multiply by the number of subplots.
Each subplot has 2 lines plotted (sin and cos). There are 2 subplots, so total lines = 2 * 2 = 4.
Choose the code that creates a figure with 3 side-by-side plots using matplotlib. The plots should share the same y-axis.
Remember, rows come first, then columns in plt.subplots(rows, columns).
To create 3 plots side-by-side, you want 1 row and 3 columns. Sharing y-axis means sharey=True.
What error will this code raise?
import matplotlib.pyplot as plt fig, axes = plt.subplots(2, 2) axes.plot([1, 2, 3], [4, 5, 6])
Remember that axes is an array of subplots, not a single subplot.
axes is a 2x2 numpy array of Axes objects. You cannot call plot directly on the array. You must call it on one Axes element.
Which of the following is the best reason to use multiple plots in one figure?
Think about how seeing plots together helps understanding data.
Multiple plots per figure help compare data sets visually in one view, making it easier to find patterns or differences.