Bird
Raised Fist0
Data Analysis Pythondata~20 mins

Line plots in Data Analysis Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Line Plot Mastery
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 Python code using matplotlib?
Data Analysis Python
import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
y = [10, 20, 25, 30]
plt.plot(x, y)
plt.show()
AA bar chart with bars at x=1 to 4 with heights 10 to 30
BA line plot showing points connected by a line from (1,10) to (4,30)
CA scatter plot showing only points at (1,10), (2,20), (3,25), (4,30) without lines
DAn empty plot with no data shown
Attempts:
2 left
💡 Hint
plt.plot() connects points with lines by default.
❓ data_output
intermediate
2:00remaining
Number of lines in a multi-line plot
Given this code, how many separate lines will appear in the plot?
Data Analysis Python
import matplotlib.pyplot as plt
x = [0, 1, 2, 3]
y1 = [1, 3, 2, 4]
y2 = [4, 2, 3, 1]
plt.plot(x, y1)
plt.plot(x, y2)
plt.show()
A2
B4
C3
D1
Attempts:
2 left
💡 Hint
Each plt.plot() call adds one line to the plot.
🔧 Debug
advanced
2:00remaining
Identify the error in this line plot code
What error will this code produce when run?
Data Analysis Python
import matplotlib.pyplot as plt
x = [1, 2, 3]
y = [4, 5]
plt.plot(x, y)
plt.show()
ANo error, plot will display correctly
BTypeError: unsupported operand type(s) for +
CSyntaxError: invalid syntax
DValueError: x and y must have same first dimension
Attempts:
2 left
💡 Hint
x and y lists must be the same length for plotting.
❓ visualization
advanced
2:00remaining
Effect of linestyle parameter in line plots
Which option shows the correct line style for this code?
Data Analysis Python
import matplotlib.pyplot as plt
x = [0, 1, 2, 3]
y = [3, 1, 4, 2]
plt.plot(x, y, linestyle='--')
plt.show()
ANo line, only points shown
BA solid line connecting the points
CA dashed line connecting the points
DA dotted line connecting the points
Attempts:
2 left
💡 Hint
The linestyle='--' means dashed line.
🚀 Application
expert
3:00remaining
Interpreting trends from a line plot
A line plot shows monthly sales over a year with a steady upward slope except for a dip in July. What does this indicate?
ASales generally increased over the year but dropped in July
BSales decreased steadily all year
CSales were constant all year with no changes
DSales peaked in July and dropped afterwards
Attempts:
2 left
💡 Hint
Look at the slope and dips in the line plot.

Practice

(1/5)
1. What is the main purpose of a line plot in data analysis?
easy
A. To display data as separate bars for comparison
B. To list raw data values without visualization
C. To show the distribution of data using boxes
D. To show trends or changes over time by connecting data points

Solution

  1. Step 1: Understand the function of line plots

    Line plots connect points to show how values change over time or order.
  2. Step 2: Compare with other plot types

    Bar plots show separate bars, box plots show distribution, and raw data listing is not a plot.
  3. Final Answer:

    To show trends or changes over time by connecting data points -> Option D
  4. Quick Check:

    Line plot = trends over time [OK]
Hint: Line plots connect points to show trends clearly [OK]
Common Mistakes:
  • Confusing line plots with bar charts
  • Thinking line plots show data distribution
  • Assuming line plots display raw data only
2. Which of the following is the correct way to create a simple line plot using matplotlib in Python?
easy
A. plt.graph(x, y)
B. plt.line(x, y)
C. plt.plot(x, y)
D. plt.chart(x, y)

Solution

  1. Step 1: Recall matplotlib line plot syntax

    The function to create line plots in matplotlib is plt.plot().
  2. Step 2: Check other options

    Functions like plt.line(), plt.graph(), and plt.chart() do not exist in matplotlib.
  3. Final Answer:

    plt.plot(x, y) -> Option C
  4. Quick Check:

    matplotlib line plot = plt.plot() [OK]
Hint: Use plt.plot() to draw line plots in matplotlib [OK]
Common Mistakes:
  • Using non-existent functions like plt.line()
  • Confusing plot with chart or graph functions
  • Forgetting to import matplotlib.pyplot as plt
3. What will be the output of the following code?
import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
y = [10, 20, 15, 25]
plt.plot(x, y, marker='o')
plt.show()
medium
A. A bar chart with bars at x positions 1 to 4
B. A line plot connecting points (1,10), (2,20), (3,15), (4,25) with circle markers
C. A scatter plot with only circle markers at given points
D. An error because marker='o' is invalid

Solution

  1. Step 1: Understand plt.plot with marker

    The code plots a line connecting points with circle markers at each point.
  2. Step 2: Differentiate from scatter and bar plots

    plt.plot with marker='o' adds markers on a line plot, not a scatter or bar chart.
  3. Final Answer:

    A line plot connecting points (1,10), (2,20), (3,15), (4,25) with circle markers -> Option B
  4. Quick Check:

    plt.plot + marker='o' = line with circles [OK]
Hint: plt.plot with marker='o' adds circle markers on line [OK]
Common Mistakes:
  • Thinking plt.plot with marker='o' creates scatter plot
  • Confusing bar chart with line plot
  • Assuming marker='o' causes error
4. Identify the error in this code that tries to plot two lines on the same graph:
import matplotlib.pyplot as plt
x = [1, 2, 3]
y1 = [2, 4, 6]
y2 = [1, 3, 5]
plt.plot(x, y1)
plt.plot(y2, x)
plt.show()
medium
A. The second plt.plot has x and y swapped, causing incorrect plotting
B. Missing plt.show() to display the plot
C. Using plt.plot twice is not allowed
D. Lists x, y1, y2 must be numpy arrays

Solution

  1. Step 1: Check the order of arguments in plt.plot

    plt.plot expects x values first, then y values. The second plot swaps these.
  2. Step 2: Understand impact of swapped arguments

    Swapping causes the line to plot y2 on x-axis and x on y-axis, which is incorrect for intended comparison.
  3. Final Answer:

    The second plt.plot has x and y swapped, causing incorrect plotting -> Option A
  4. Quick Check:

    plt.plot(x, y) order matters [OK]
Hint: Always put x first, y second in plt.plot(x, y) [OK]
Common Mistakes:
  • Swapping x and y arguments in plt.plot
  • Thinking plt.plot twice overwrites the first plot
  • Believing lists must be numpy arrays
5. You have daily sales data for two products over a week:
days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
sales_A = [100, 120, 130, 90, 110, 115, 105]
sales_B = [80, 95, 100, 85, 90, 100, 95]

How would you create a line plot to compare both products' sales clearly with labels and a legend?
hard
A. plt.plot(days, sales_A, label='Product A') plt.plot(days, sales_B, label='Product B') plt.legend() plt.show()
B. plt.plot(sales_A, days) plt.plot(sales_B, days) plt.show()
C. plt.bar(days, sales_A) plt.bar(days, sales_B) plt.show()
D. plt.scatter(days, sales_A) plt.scatter(days, sales_B) plt.legend() plt.show()

Solution

  1. Step 1: Plot both sales lists against days with labels

    Use plt.plot(days, sales_A, label='Product A') and similarly for sales_B to plot lines with labels.
  2. Step 2: Add legend and show plot

    Call plt.legend() to display labels and plt.show() to render the plot.
  3. Final Answer:

    plt.plot(days, sales_A, label='Product A') plt.plot(days, sales_B, label='Product B') plt.legend() plt.show() -> Option A
  4. Quick Check:

    Use labels + legend for multiple lines [OK]
Hint: Add label= and call plt.legend() to compare lines [OK]
Common Mistakes:
  • Swapping x and y in plt.plot
  • Using bar or scatter plots instead of line plots
  • Forgetting to add legend for clarity
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