📖 Scenario: Imagine you are a weather analyst. You have daily temperature data for a city. You want to understand how temperature changes over time.
🎯 Goal: You will create a simple time series plot using matplotlib to see why time series data needs special handling compared to regular data.
📋 What You'll Learn
Create a dictionary with dates as keys and temperatures as values
Create a list of dates from the dictionary keys
Create a list of temperatures from the dictionary values
Plot the temperature over time using matplotlib
Label the x-axis as 'Date' and y-axis as 'Temperature (°C)'
Show the plot
💡 Why This Matters
🌍 Real World
Time series data is everywhere: weather, stock prices, sales over months, and more. Understanding how to handle and plot it is important.
💼 Career
Data scientists and analysts often work with time series data to find patterns and make forecasts.
Progress0 / 4 steps
1
Create the temperature data dictionary
Create a dictionary called temperature_data with these exact entries: '2024-01-01': 5, '2024-01-02': 7, '2024-01-03': 6, '2024-01-04': 8, '2024-01-05': 7
Matplotlib
Hint
Use curly braces to create a dictionary. Dates are keys as strings, temperatures are integer values.
2
Extract dates and temperatures into lists
Create a list called dates from the keys of temperature_data and a list called temperatures from the values of temperature_data
Matplotlib
Hint
Use the keys() and values() methods of the dictionary and convert them to lists.
3
Plot the temperature over time
Import matplotlib.pyplot as plt. Use plt.plot(dates, temperatures) to plot the data. Then label the x-axis as 'Date' and y-axis as 'Temperature (°C)'
Matplotlib
Hint
Use plt.plot() to create the line plot. Use plt.xlabel() and plt.ylabel() to add labels.
4
Show the plot
Use plt.show() to display the plot
Matplotlib
Hint
Use plt.show() to display the plot window.
Practice
(1/5)
1. Why do time series data need special handling when plotting with matplotlib?
easy
A. Because time series data always have missing values
B. Because time series data have dates or times that must be shown correctly on the x-axis
C. Because time series data cannot be plotted as lines
D. Because time series data do not change over time
Solution
Step 1: Understand the nature of time series data
Time series data represent values changing over time, so the x-axis must show dates or times properly.
Step 2: Recognize the importance of correct axis formatting
Using dates on the x-axis helps to see trends and patterns clearly, which normal numeric axes cannot show well.
Final Answer:
Because time series data have dates or times that must be shown correctly on the x-axis -> Option B
Quick Check:
Time series need date/time x-axis = A [OK]
Hint: Time series need date/time x-axis for clear trends [OK]
Common Mistakes:
Thinking time series always have missing values
Assuming time series cannot be line plotted
Ignoring the importance of date/time axis
2. Which of the following is the correct way to plot a time series in matplotlib using dates on the x-axis?
easy
A. plt.plot(dates, values) where dates is a list of datetime objects
B. plt.plot(values, dates) where dates is a list of datetime objects
C. plt.plot(values) without specifying dates
D. plt.plot(dates, values) where dates is a list of strings without conversion
Solution
Step 1: Identify correct argument order for plt.plot
The first argument is x-axis data (dates), the second is y-axis data (values).
Step 2: Ensure dates are datetime objects for proper plotting
Dates must be datetime objects for matplotlib to format the x-axis correctly, not plain strings.
Final Answer:
plt.plot(dates, values) where dates is a list of datetime objects -> Option A
Quick Check:
Dates first as datetime objects = D [OK]
Hint: Plot with dates first as datetime objects [OK]
Common Mistakes:
Swapping x and y arguments
Using strings for dates without conversion
Plotting values only without dates
3. What will be the output of this code snippet?
import matplotlib.pyplot as plt
import pandas as pd
dates = pd.date_range('2024-01-01', periods=3)
values = [10, 20, 15]
plt.plot(dates, values)
plt.gca().get_xticklabels()[0].get_text()
medium
A. A runtime error because get_text() is invalid
B. The string '2024-01-01'
C. The integer 10
D. An empty string or date label depending on matplotlib version
Solution
Step 1: Understand what get_xticklabels()[0].get_text() returns
This gets the text of the first x-axis tick label after plotting.
Step 2: Recognize matplotlib behavior with datetime x-axis
Matplotlib often formats date labels dynamically; initially, labels may be empty strings or formatted dates depending on version and rendering.
Final Answer:
An empty string or date label depending on matplotlib version -> Option D
Quick Check:
First x tick label text varies = C [OK]
Hint: Date labels may be empty or formatted strings [OK]
Common Mistakes:
Expecting exact date string always
Assuming get_text() causes error
Confusing tick label text with data values
4. Identify the error in this code that tries to plot a time series: