Challenge - 5 Problems
Subplot Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of subplot creation with pandas
What will be the output of this code snippet that creates subplots for two line charts using pandas and matplotlib?
Pandas
import pandas as pd import matplotlib.pyplot as plt data = {'A': [1, 3, 2, 4], 'B': [4, 2, 3, 1]} df = pd.DataFrame(data) axes = df.plot(subplots=True, layout=(2, 1), figsize=(6, 6)) plt.tight_layout() plt.show() print(type(axes))
Attempts:
2 left
💡 Hint
Think about what pandas returns when plotting multiple subplots.
✗ Incorrect
When plotting multiple subplots, pandas returns a numpy ndarray of Axes objects, not a single Axes or list.
❓ data_output
intermediate2:00remaining
Number of subplot axes created
Given this DataFrame and plotting code, how many subplot axes will be created?
Pandas
import pandas as pd import matplotlib.pyplot as plt data = {'X': [5, 6, 7], 'Y': [1, 2, 3], 'Z': [9, 8, 7]} df = pd.DataFrame(data) axes = df.plot(subplots=True, layout=(1, 3), figsize=(9, 3)) plt.close() # Close plot to avoid display print(len(axes.flatten()))
Attempts:
2 left
💡 Hint
Count the number of columns in the DataFrame.
✗ Incorrect
Each column gets its own subplot when subplots=True, so 3 columns mean 3 subplot axes.
🔧 Debug
advanced2:00remaining
Identify the error in subplot layout code
What error will this code raise when trying to create subplots with pandas?
Pandas
import pandas as pd import matplotlib.pyplot as plt data = {'A': [1, 2], 'B': [3, 4], 'C': [5, 6]} df = pd.DataFrame(data) axes = df.plot(subplots=True, layout=(1, 2), figsize=(8, 4)) plt.show()
Attempts:
2 left
💡 Hint
Check if the layout can fit all subplots.
✗ Incorrect
The layout (1, 2) can only hold 2 subplots but 3 are needed, so pandas raises a ValueError.
❓ visualization
advanced2:00remaining
Effect of figsize on subplot appearance
What visual difference will you see when changing figsize from (6, 4) to (12, 8) in this subplot code?
Pandas
import pandas as pd import matplotlib.pyplot as plt data = {'A': [1, 4, 2, 3], 'B': [4, 1, 3, 2]} df = pd.DataFrame(data) axes = df.plot(subplots=True, figsize=(6, 4)) plt.show()
Attempts:
2 left
💡 Hint
Figsize controls the overall figure size in inches.
✗ Incorrect
Increasing figsize makes the whole figure bigger, so subplots appear larger and have more space.
🚀 Application
expert3:00remaining
Creating a 2x2 grid of subplots with different chart types
Which code correctly creates a 2x2 grid of subplots from this DataFrame, plotting a line, bar, histogram, and scatter plot respectively?
Pandas
import pandas as pd import matplotlib.pyplot as plt import numpy as np data = {'A': np.random.randint(1, 10, 10), 'B': np.random.randint(10, 20, 10), 'C': np.random.randn(10), 'D': np.random.randn(10)} df = pd.DataFrame(data)
Attempts:
2 left
💡 Hint
Remember scatter plots require x and y arguments and axes indexing for 2D arrays.
✗ Incorrect
Option A correctly creates a 2x2 grid and assigns each plot to the right axes. Scatter plot uses df.plot.scatter with x and y specified.