Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Challenge: Visualize Air Quality Trends | Environmental Data Exploration
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.

Tehtävä

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.

Ratkaisu

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 1. Luku 5
single

single

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

close

bookChallenge: Visualize Air Quality Trends

Pyyhkäise näyttääksesi valikon

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.

Tehtävä

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.

Ratkaisu

Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 1. Luku 5
single

single

some-alt