Bird
Raised Fist0
Matplotlibdata~10 mins

Time series with fill_between in Matplotlib - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to plot a time series line using matplotlib.

Matplotlib
import matplotlib.pyplot as plt
import pandas as pd

dates = pd.date_range('2024-01-01', periods=5)
values = [10, 12, 9, 14, 13]

plt.plot(dates, [1])
plt.show()
Drag options to blanks, or click blank then click option'
A[10, 12, 9, 14]
Bdates
Crange(5)
Dvalues
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'dates' as the y-axis data instead of 'values'.
Using a range or incomplete list instead of the full values list.
2fill in blank
medium

Complete the code to fill the area under the time series line.

Matplotlib
plt.plot(dates, values)
plt.[1](dates, values, alpha=0.3)
plt.show()
Drag options to blanks, or click blank then click option'
Afill
Bfill_between
Cbar
Dscatter
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'fill' which is not a matplotlib function for this purpose.
Using 'bar' or 'scatter' which create different plot types.
3fill in blank
hard

Fix the error in the code to fill between two time series lines.

Matplotlib
values2 = [8, 11, 10, 13, 12]
plt.plot(dates, values)
plt.plot(dates, values2)
plt.fill_between(dates, values, [1], color='gray', alpha=0.2)
plt.show()
Drag options to blanks, or click blank then click option'
Arange(5)
Bvalues
Cvalues2
Ddates
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'dates' or 'range(5)' which are x-axis values, not y-values.
Using the same 'values' list twice, which fills under the same line.
4fill in blank
hard

Fill the blanks to create a dictionary comprehension that maps dates to values only if the value is greater than 10.

Matplotlib
filtered = { [1]: [2] for [3], [4] in zip(dates, values) if [2] > 10 }
Drag options to blanks, or click blank then click option'
Adate
Bvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same variable name for both key and value.
Not matching variable names in the for loop and the key:value pair.
5fill in blank
hard

Fill the blanks to create a dictionary comprehension that maps uppercase dates (as strings) to values only if the value is less than 13.

Matplotlib
result = { [1]: [2] for [3], [4] in zip(dates, values) if [2] < 13 }
Drag options to blanks, or click blank then click option'
Adate.strftime('%Y-%m-%d').upper()
Bvalue
Cdate
Attempts:
3 left
💡 Hint
Common Mistakes
Not converting the date to string before calling upper().
Using inconsistent variable names.

Practice

(1/5)
1. What does the fill_between function do in a time series plot using matplotlib?
easy
A. Creates a bar chart from time series data
B. Plots scatter points on the time series
C. Colors the area between two lines to highlight differences or ranges
D. Removes grid lines from the plot

Solution

  1. Step 1: Understand the purpose of fill_between

    The fill_between function is used to color the area between two lines or curves in a plot.
  2. Step 2: Connect to time series visualization

    In time series plots, it highlights differences or ranges between two data series by filling the area between them.
  3. Final Answer:

    Colors the area between two lines to highlight differences or ranges -> Option C
  4. Quick Check:

    fill_between colors area between lines = A [OK]
Hint: Remember fill_between fills area between lines visually [OK]
Common Mistakes:
  • Confusing fill_between with scatter plot
  • Thinking it creates bar charts
  • Assuming it removes grid lines
2. Which of the following is the correct syntax to fill the area between two time series arrays y1 and y2 over time points x using matplotlib?
easy
A. plt.fill_between(x, y1, y2, color='blue', alpha=0.3)
B. plt.fill_between(x, y1, color='blue', alpha=0.3)
C. plt.fill_between(y1, y2, x, color='blue')
D. plt.fill_between(x, y1, y2, linestyle='--')

Solution

  1. Step 1: Check function parameters order

    The correct order for fill_between is x (time points), then y1 and y2 (values to fill between).
  2. Step 2: Validate optional arguments

    Color and transparency (alpha) are valid optional arguments; linestyle is not used for fill_between.
  3. Final Answer:

    plt.fill_between(x, y1, y2, color='blue', alpha=0.3) -> Option A
  4. Quick Check:

    fill_between(x, y1, y2) with color and alpha = C [OK]
Hint: Remember order: x, y1, y2 in fill_between [OK]
Common Mistakes:
  • Swapping y1 and y2 with x
  • Using linestyle instead of color or alpha
  • Omitting one of the y arrays
3. What will be the output of this code snippet?
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(5)
y1 = np.array([1, 3, 2, 5, 4])
y2 = np.array([2, 4, 3, 6, 5])
plt.plot(x, y1, label='y1')
plt.plot(x, y2, label='y2')
plt.fill_between(x, y1, y2, color='green', alpha=0.4)
plt.legend()
plt.show()
medium
A. An error because fill_between requires y1 > y2
B. A plot with two lines but no shaded area
C. A plot with only one line and a green shaded area below it
D. A plot with two lines and a green shaded area between them

Solution

  1. Step 1: Understand plot and fill_between behavior

    The code plots two lines y1 and y2 over x, then fills the area between them with green color and transparency.
  2. Step 2: Confirm fill_between works with any order

    fill_between fills area between y1 and y2 regardless of which is greater, so no error occurs.
  3. Final Answer:

    A plot with two lines and a green shaded area between them -> Option D
  4. Quick Check:

    Two lines + green fill_between area = A [OK]
Hint: fill_between shades area between lines regardless of order [OK]
Common Mistakes:
  • Thinking fill_between needs y1 > y2 always
  • Expecting no shaded area without fill_between call
  • Confusing plot lines with fill_between shading
4. Identify the error in this code snippet that tries to fill between two time series:
import matplotlib.pyplot as plt
x = [0, 1, 2, 3]
y1 = [1, 2, 3]
y2 = [2, 3, 4, 5]
plt.fill_between(x, y1, y2, color='red')
plt.show()
medium
A. x and y1 have different lengths causing a ValueError
B. fill_between does not accept list inputs
C. Missing plt.plot calls before fill_between
D. Color argument must be uppercase

Solution

  1. Step 1: Check lengths of x, y1, and y2

    x has 4 points, y1 has 3 points, y2 has 4 points; mismatch causes error.
  2. Step 2: Understand fill_between input requirements

    fill_between requires x, y1, y2 arrays to have the same length to plot correctly.
  3. Final Answer:

    x and y1 have different lengths causing a ValueError -> Option A
  4. Quick Check:

    Length mismatch in inputs = B [OK]
Hint: Check all input arrays have same length before fill_between [OK]
Common Mistakes:
  • Ignoring length mismatch errors
  • Thinking fill_between needs plot calls first
  • Assuming color argument case matters
5. You have two time series arrays y1 and y2 representing daily temperatures for two cities over 7 days. You want to highlight only the days when y1 is higher than y2 using fill_between. Which code snippet correctly achieves this?
hard
A. plt.fill_between(x, y1, y2, condition=(y1 > y2), color='orange')
B. plt.fill_between(x, y1, y2, where=(y1 > y2), color='orange', alpha=0.5)
C. plt.fill_between(x, y1, y2, where=(y1 < y2), color='orange', alpha=0.5)
D. plt.fill_between(x, y1, y2, color='orange', alpha=0.5)

Solution

  1. Step 1: Use the 'where' parameter to specify condition

    The where argument in fill_between allows filling only where the condition is True.
  2. Step 2: Set condition to y1 > y2 and specify color and transparency

    Using where=(y1 > y2) fills only days when y1 is higher than y2 with orange color and alpha 0.5.
  3. Final Answer:

    plt.fill_between(x, y1, y2, where=(y1 > y2), color='orange', alpha=0.5) -> Option B
  4. Quick Check:

    Use where=(y1 > y2) to fill selectively = D [OK]
Hint: Use 'where' to fill conditionally between lines [OK]
Common Mistakes:
  • Using 'condition' instead of 'where'
  • Filling without condition fills entire area
  • Reversing the condition (y1 < y2) by mistake
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