Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Challenge: Visualize Air Quality Trends | Environmental Data Exploration
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Python for Environmental Science

bookChallenge: Visualize Air Quality Trends

In this challenge, you will explore how to visualize air quality trends using a hardcoded pandas DataFrame. Visualizing air quality index (AQI) data over time helps you quickly identify periods of poor air quality and communicate findings effectively. You will learn to create a line plot of AQI values, highlight days with unhealthy air quality, and add clear titles and labels to your plot.

Begin by importing the required libraries. You will use pandas for data handling and matplotlib for plotting. Next, create a DataFrame that represents daily AQI values for a city. This DataFrame will have two columns: one for dates and one for AQI values. After preparing the data, you will plot AQI over time and emphasize days when AQI exceeds a threshold, such as 100, which typically indicates unhealthy air quality.

12345678910111213141516171819202122232425262728
import pandas as pd import matplotlib.pyplot as plt # Create a DataFrame with daily AQI values data = { "date": pd.date_range(start="2024-06-01", periods=14, freq="D"), "aqi": [75, 82, 95, 110, 120, 99, 85, 102, 130, 88, 92, 105, 97, 115] } df = pd.DataFrame(data) # Set threshold for unhealthy AQI threshold = 100 # Plot AQI over time plt.figure(figsize=(10, 6)) plt.plot(df["date"], df["aqi"], label="AQI", color="blue", marker="o") # Highlight days where AQI exceeds the threshold exceed = df["aqi"] > threshold plt.scatter(df["date"][exceed], df["aqi"][exceed], color="red", label="AQI > 100", zorder=5) # Add titles and labels plt.title("Daily Air Quality Index (AQI) Trends") plt.xlabel("Date") plt.ylabel("AQI Value") plt.legend() plt.tight_layout() plt.show()
copy
Note
Note

The Air Quality Index (AQI) is a standardized indicator for reporting daily air quality. Values above 100 often signal that air pollution may pose health risks, especially for sensitive groups. For more details on AQI categories and their health implications, you can refer to the United States Environmental Protection Agency (EPA) AQI guide.

By practicing this workflow, you gain skills in both data visualization and environmental data interpretation. Being able to highlight critical thresholds in your plots helps stakeholders understand when air quality becomes a concern and supports data-driven decision-making.

Task

Swipe to start coding

  • Use the DataFrame df as shown above.
  • Plot AQI values over time as a blue line with circle markers.
  • Highlight days where AQI exceeds 100 using red markers.
  • Add a title "Daily Air Quality Index (AQI) Trends".
  • Label the x-axis as "Date" and the y-axis as "AQI Value".
  • Include a legend distinguishing the line and highlighted points.
  • Display the plot.

Solution

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 5
single

single

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you explain how the threshold for unhealthy AQI is determined?

What other ways can I highlight or annotate critical days on the plot?

Can you suggest how to interpret the trends shown in the AQI plot?

close

bookChallenge: Visualize Air Quality Trends

Swipe to show menu

In this challenge, you will explore how to visualize air quality trends using a hardcoded pandas DataFrame. Visualizing air quality index (AQI) data over time helps you quickly identify periods of poor air quality and communicate findings effectively. You will learn to create a line plot of AQI values, highlight days with unhealthy air quality, and add clear titles and labels to your plot.

Begin by importing the required libraries. You will use pandas for data handling and matplotlib for plotting. Next, create a DataFrame that represents daily AQI values for a city. This DataFrame will have two columns: one for dates and one for AQI values. After preparing the data, you will plot AQI over time and emphasize days when AQI exceeds a threshold, such as 100, which typically indicates unhealthy air quality.

12345678910111213141516171819202122232425262728
import pandas as pd import matplotlib.pyplot as plt # Create a DataFrame with daily AQI values data = { "date": pd.date_range(start="2024-06-01", periods=14, freq="D"), "aqi": [75, 82, 95, 110, 120, 99, 85, 102, 130, 88, 92, 105, 97, 115] } df = pd.DataFrame(data) # Set threshold for unhealthy AQI threshold = 100 # Plot AQI over time plt.figure(figsize=(10, 6)) plt.plot(df["date"], df["aqi"], label="AQI", color="blue", marker="o") # Highlight days where AQI exceeds the threshold exceed = df["aqi"] > threshold plt.scatter(df["date"][exceed], df["aqi"][exceed], color="red", label="AQI > 100", zorder=5) # Add titles and labels plt.title("Daily Air Quality Index (AQI) Trends") plt.xlabel("Date") plt.ylabel("AQI Value") plt.legend() plt.tight_layout() plt.show()
copy
Note
Note

The Air Quality Index (AQI) is a standardized indicator for reporting daily air quality. Values above 100 often signal that air pollution may pose health risks, especially for sensitive groups. For more details on AQI categories and their health implications, you can refer to the United States Environmental Protection Agency (EPA) AQI guide.

By practicing this workflow, you gain skills in both data visualization and environmental data interpretation. Being able to highlight critical thresholds in your plots helps stakeholders understand when air quality becomes a concern and supports data-driven decision-making.

Task

Swipe to start coding

  • Use the DataFrame df as shown above.
  • Plot AQI values over time as a blue line with circle markers.
  • Highlight days where AQI exceeds 100 using red markers.
  • Add a title "Daily Air Quality Index (AQI) Trends".
  • Label the x-axis as "Date" and the y-axis as "AQI Value".
  • Include a legend distinguishing the line and highlighted points.
  • Display the plot.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 5
single

single

some-alt