0
0
Pandasdata~20 mins

Line plots with plot() in Pandas - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Line Plot Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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')))
A<class 'list'>
B<class 'matplotlib.axes._subplots.AxesSubplot'>
C<class 'pandas.core.frame.DataFrame'>
D<class 'matplotlib.figure.Figure'>
Attempts:
2 left
💡 Hint
The plot() method returns the axes object where the plot is drawn.
data_output
intermediate
2: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()
A1
B0
C4
D3
Attempts:
2 left
💡 Hint
Each column in the y list creates one line.
visualization
advanced
2: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()
AThe plot lines become dashed instead of solid.
BThe plot background color changes to gray.
CGrid lines appear behind the plot lines to help read values.
DThe plot title is automatically added.
Attempts:
2 left
💡 Hint
Grid lines help to visually align points with axis ticks.
🔧 Debug
advanced
2: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()
ANo error, plot will display correctly.
BTypeError because 'Date' column is string, not datetime.
CValueError because x and y columns have different lengths.
DKeyError because 'Value' column is missing.
Attempts:
2 left
💡 Hint
Pandas can plot string dates on x-axis without error.
🚀 Application
expert
3: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:
Aax = df.plot(x='X', y='A', style='r--')\ndf.plot(x='X', y='B', style='b:', ax=ax)\nplt.close() # Correct: both lines on same axes with styles
Bdf.plot(x='X', y='A', style='r--')\ndf.plot(x='X', y='B', style='b:')\nplt.close() # Incorrect because second plot overwrites first
Cdf.plot(x='X', y=['A', 'B'], style=['r--', 'b:'])\nplt.close() # Incorrect: style list must match columns exactly
Ddf.plot(x='X', y='A', linestyle='--', color='red')\ndf.plot(x='X', y='B', linestyle=':', color='blue')\nplt.close() # Incorrect: second plot overwrites first
Attempts:
2 left
💡 Hint
Use the same axes object to plot multiple lines with different styles.