Challenge - 5 Problems
Line Plot Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a simple line plot code
What will be the output of this code snippet that uses pandas to plot a line graph?
Pandas
import pandas as pd import matplotlib.pyplot as plt data = {'Year': [2018, 2019, 2020, 2021], 'Sales': [250, 300, 400, 350]} df = pd.DataFrame(data) df.plot(x='Year', y='Sales') plt.close() # Prevent plot from displaying in test environment print(type(df.plot(x='Year', y='Sales')))
Attempts:
2 left
💡 Hint
The plot() method returns the axes object where the plot is drawn.
✗ Incorrect
The pandas DataFrame plot() method returns a matplotlib AxesSubplot object representing the plot area.
❓ data_output
intermediate2:00remaining
Number of lines plotted with multiple columns
Given this DataFrame and plot code, how many lines will appear in the plot?
Pandas
import pandas as pd import matplotlib.pyplot as plt data = {'Month': ['Jan', 'Feb', 'Mar'], 'Temp1': [30, 35, 40], 'Temp2': [25, 30, 35], 'Temp3': [20, 25, 30]} df = pd.DataFrame(data) df.plot(x='Month', y=['Temp1', 'Temp2', 'Temp3']) plt.close()
Attempts:
2 left
💡 Hint
Each column in the y list creates one line.
✗ Incorrect
Specifying multiple columns in y creates one line per column, so 3 lines appear.
❓ visualization
advanced2:00remaining
Effect of setting grid=true in line plots
What visual change occurs when you add grid=true to the plot() method in pandas?
Pandas
import pandas as pd import matplotlib.pyplot as plt data = {'Day': [1, 2, 3, 4], 'Value': [10, 15, 7, 12]} df = pd.DataFrame(data) df.plot(x='Day', y='Value', grid=True) plt.close()
Attempts:
2 left
💡 Hint
Grid lines help to visually align points with axis ticks.
✗ Incorrect
Setting grid=true adds horizontal and vertical grid lines behind the plot lines.
🔧 Debug
advanced2:00remaining
Identify the error in this line plot code
What error will this code raise when trying to plot?
Pandas
import pandas as pd import matplotlib.pyplot as plt data = {'Date': ['2023-01-01', '2023-01-02'], 'Value': [100, 200]} df = pd.DataFrame(data) df.plot(x='Date', y='Value') plt.close()
Attempts:
2 left
💡 Hint
Pandas can plot string dates on x-axis without error.
✗ Incorrect
Pandas can plot string dates on x-axis without error by treating them as categories.
🚀 Application
expert3:00remaining
Customize line plot with multiple styles
Which option correctly produces a line plot with two lines: one dashed red and one dotted blue?
Pandas
import pandas as pd import matplotlib.pyplot as plt data = {'X': [1, 2, 3, 4], 'A': [10, 20, 25, 30], 'B': [15, 18, 22, 28]} df = pd.DataFrame(data) # Choose the correct plotting code below:
Attempts:
2 left
💡 Hint
Use the same axes object to plot multiple lines with different styles.
✗ Incorrect
Option A plots both lines on the same axes with specified styles, producing the desired plot.