B. The axis='x' should be axis='y' to show x-axis grid lines.
C. true is undefined; use True (capitalized) instead.
D. Missing plt.show() to display the plot.
Solution
Step 1: Check plt.grid parameters
The code raises NameError because true is not defined in Python. Boolean true is True (capitalized).
Step 2: Correct way to enable grid on x-axis
Use plt.grid(True, axis='x') or plt.grid(visible=True, axis='x').
Final Answer:
true is undefined; use True (capitalized) instead. -> Option C
Quick Check:
plt.grid(True, axis='x') is correct [OK]
Hint: Booleans in Python are True/False (capitalized) [OK]
Common Mistakes:
Using lowercase 'true' which causes NameError
Confusing axis='x' with axis='y'
Thinking plt.grid can't be used with plt.plot
5. You want to create a plot with grid lines only on the y-axis, styled as dotted blue lines, but only for major ticks. Which code snippet achieves this correctly?
hard
A. plt.grid(axis='y', linestyle=':', color='blue', which='minor')
B. plt.grid(True, axis='y', linestyle=':', color='blue', which='major')
C. plt.grid(True, axis='y', linestyle='--', color='blue', which='minor')
D. plt.grid(True, axis='x', linestyle=':', color='blue', which='major')
Solution
Step 1: Understand parameters for grid customization
To show grid lines only on the y-axis, use axis='y'. For dotted lines, use linestyle=':'. To color them blue, use color='blue'. To apply only to major ticks, use which='major'.
Step 2: Verify each option
plt.grid(True, axis='y', linestyle=':', color='blue', which='major') matches all requirements. plt.grid(axis='y', linestyle=':', color='blue', which='minor') uses which='minor', incorrect. plt.grid(True, axis='y', linestyle='--', color='blue', which='minor') uses minor ticks and dashed lines, incorrect. plt.grid(True, axis='x', linestyle=':', color='blue', which='major') applies to x-axis, not y-axis.
Final Answer:
plt.grid(True, axis='y', linestyle=':', color='blue', which='major') -> Option B