It raises an error: AttributeError: module 'matplotlib.pyplot' has no attribute 'style'
medium
A. plt.plot() is used incorrectly
B. The style name 'seaborn' is misspelled
C. matplotlib version is too old and lacks style module
D. plt.show() is missing parentheses
Solution
Step 1: Understand the error message
The error says plt has no attribute 'style', meaning the style module is missing.
Step 2: Check possible causes
This usually happens if matplotlib version is old and does not support style sheets.
Final Answer:
matplotlib version is too old and lacks style module -> Option C
Quick Check:
AttributeError on plt.style means old matplotlib [OK]
Hint: AttributeError on plt.style means update matplotlib [OK]
Common Mistakes:
Assuming typo in style name
Blaming plt.plot() syntax
Forgetting plt.show() needs parentheses
5. You want to create two plots side by side: the first with 'ggplot' style and the second with 'seaborn' style. Which code correctly applies different styles to each subplot?
D. with plt.style.context('ggplot'):
plt.subplot(1, 2, 1)
plt.plot([1,2])
with plt.style.context('seaborn'):
plt.subplot(1, 2, 2)
plt.plot([2,1])
plt.show()
Solution
Step 1: Understand style application scope
plt.style.use() sets global style, affecting all plots after it.
Step 2: Use plt.style.context() for local styles
To apply different styles to subplots, use plt.style.context() as a context manager around each plot.
Step 3: Check options
Only with plt.style.context('ggplot'):
plt.subplot(1, 2, 1)
plt.plot([1,2])
with plt.style.context('seaborn'):
plt.subplot(1, 2, 2)
plt.plot([2,1])
plt.show() uses plt.style.context() correctly to apply different styles to each subplot.
Final Answer:
Use plt.style.context() blocks for different subplot styles. -> Option D
Quick Check:
Use style.context() for local style changes [OK]
Hint: Use plt.style.context() to apply styles locally [OK]
Common Mistakes:
Using plt.style.use() multiple times globally
Trying to pass list of styles to plt.style.use()
Not using context manager for local styles
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.