Consider the following Python code using Matplotlib. What will be the output when this code runs?
import matplotlib.pyplot as plt fig, ax = plt.subplots() ax.plot([1, 2, 3], [4, 5, 6]) print(type(fig))
Remember, plt.subplots() returns a figure and axes object. The variable fig stores the figure.
The fig variable holds the Figure object, so printing its type shows matplotlib.figure.Figure.
Look at this code snippet. How many axes objects are created?
import matplotlib.pyplot as plt fig, axs = plt.subplots(2, 2) print(len(axs.flatten()))
Think about the grid size in plt.subplots(2, 2).
The code creates a 2x2 grid of axes, so there are 4 axes in total.
Matplotlib renders figure elements in a specific order. Which option correctly orders these steps?
- 1. Drawing axes
- 2. Creating figure canvas
- 3. Rendering figure to screen
- 4. Drawing figure background
Think about what happens first: setting up the canvas, then background, then axes, then showing.
Matplotlib first creates the canvas, then draws the figure background, then draws axes, and finally renders the figure to the screen.
What error will this Matplotlib code produce?
import matplotlib.pyplot as plt fig = plt.figure() ax = fig.add_subplot(111) ax.plot([1, 2, 3], [4, 5]) plt.show()
Check the lengths of the x and y data lists passed to plot().
The x list has 3 elements but y has only 2, causing a ValueError about dimension mismatch.
This code saves a figure. What is the size in pixels of the saved image?
import matplotlib.pyplot as plt fig = plt.figure(figsize=(4, 3), dpi=150) fig.savefig('test.png')
Multiply figure size in inches by dpi to get pixels.
Width in pixels = 4 inches * 150 dpi = 600 pixels; height = 3 inches * 150 dpi = 450 pixels.