Bird
Raised Fist0
Pandasdata~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the plot() function in pandas primarily create?
easy
A. A list of column names
B. A table of data values
C. A summary statistics report
D. A line plot to show data trends over an axis

Solution

  1. Step 1: Understand the purpose of plot() in pandas

    The plot() function is used to create visual graphs from data, most commonly line plots.
  2. Step 2: Identify the type of plot created

    By default, DataFrame.plot() creates a line plot to show how data changes over an ordered axis like time.
  3. Final Answer:

    A line plot to show data trends over an axis -> Option D
  4. Quick Check:

    plot() = line plot [OK]
Hint: plot() makes line plots by default to show trends [OK]
Common Mistakes:
  • Thinking plot() creates tables
  • Confusing plot() with summary functions
  • Assuming plot() lists columns
2. Which of the following is the correct syntax to plot the column 'Sales' from a DataFrame df using the y parameter?
easy
A. df.plot('Sales')
B. df.plot(y='Sales')
C. df.plot(column='Sales')
D. df.plot(x='Sales')

Solution

  1. Step 1: Recall the parameter to select columns in plot()

    The y parameter specifies which column(s) to plot on the y-axis.
  2. Step 2: Match the correct syntax

    Using df.plot(y='Sales') correctly tells pandas to plot the 'Sales' column as the y-axis data.
  3. Final Answer:

    df.plot(y='Sales') -> Option B
  4. Quick Check:

    Use y='column' to plot that column [OK]
Hint: Use y='column' to plot that column in plot() [OK]
Common Mistakes:
  • Using 'column' instead of 'y' parameter
  • Passing column name without parameter
  • Using x='Sales' to plot y-axis
3. Given the code:
import pandas as pd
import matplotlib.pyplot as plt

data = {'Month': ['Jan', 'Feb', 'Mar'], 'Sales': [100, 150, 120]}
df = pd.DataFrame(data)
df.plot(x='Month', y='Sales')
plt.show()

What will the plot show?
medium
A. A scatter plot of sales vs months
B. A bar plot with sales on x-axis and months on y-axis
C. A line plot with months on x-axis and sales on y-axis
D. An error because 'Month' is not numeric

Solution

  1. Step 1: Understand the plot parameters

    The code uses x='Month' and y='Sales' to plot sales against months.
  2. Step 2: Recognize default plot type

    By default, plot() creates a line plot, even if x-axis is categorical like months.
  3. Final Answer:

    A line plot with months on x-axis and sales on y-axis -> Option C
  4. Quick Check:

    plot(x=cat, y=num) = line plot [OK]
Hint: plot() defaults to line plot even with categorical x-axis [OK]
Common Mistakes:
  • Expecting bar or scatter plot by default
  • Thinking non-numeric x causes error
  • Confusing axis labels
4. What is wrong with this code snippet?
df = pd.DataFrame({'A': [1, 2, 3]})
df.plot('A')
medium
A. It will plot column 'A' on y-axis but x-axis will be default index
B. It will cause a syntax error because 'y' parameter is missing
C. It will raise a runtime error because 'A' is not specified as y
D. It will plot column 'A' on x-axis instead of y-axis

Solution

  1. Step 1: Analyze the plot() call with one positional string argument

    When a string is passed positionally to plot(), it specifies the column for the y-axis.
  2. Step 2: Understand default x-axis behavior

    Since no x parameter is specified, the x-axis uses the DataFrame's index.
  3. Final Answer:

    It will plot column 'A' on y-axis but x-axis will be default index -> Option A
  4. Quick Check:

    plot('A') = y='A' vs index [OK]
Hint: plot('col') sets y='col' vs index; specify x/y to control [OK]
Common Mistakes:
  • Assuming 'A' is x-axis without specifying
  • Expecting syntax or runtime error
  • Confusing x and y parameters
5. You have a DataFrame df with columns 'Date', 'Temperature', and 'Humidity'. You want to plot both 'Temperature' and 'Humidity' over 'Date' on the same line plot with a title 'Weather Data'. Which code correctly does this?
hard
A. df.plot(x='Date', y=['Temperature', 'Humidity'], title='Weather Data')
B. df.plot(y=['Temperature', 'Humidity'], x='Date', label='Weather Data')
C. df.plot(x='Date', y='Temperature', 'Humidity', title='Weather Data')
D. df.plot(x='Date', y=['Temperature', 'Humidity']).title('Weather Data')

Solution

  1. Step 1: Select multiple columns for y-axis correctly

    Use a list for the y parameter: y=['Temperature', 'Humidity'].
  2. Step 2: Add title properly in plot()

    The title parameter inside plot() sets the plot title directly.
  3. Step 3: Verify syntax correctness

    df.plot(x='Date', y=['Temperature', 'Humidity'], title='Weather Data') uses correct syntax with x, y as list, and title parameter all in one call.
  4. Final Answer:

    df.plot(x='Date', y=['Temperature', 'Humidity'], title='Weather Data') -> Option A
  5. Quick Check:

    Use y=[cols], x=col, title='text' in plot() [OK]
Hint: Use y=[cols], x=col, title='text' inside plot() [OK]
Common Mistakes:
  • Passing multiple y columns without list
  • Using label instead of title for plot title
  • Using .title() instead of .set_title()
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard