Challenge: 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.
12345678910111213141516171819202122232425262728import 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()
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.
Swipe to start coding
- Use the DataFrame
dfas 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.
Lösning
Tack för dina kommentarer!
single
Fråga AI
Fråga AI
Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal
Fantastiskt!
Completion betyg förbättrat till 5.26
Challenge: Visualize Air Quality Trends
Svep för att visa menyn
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.
12345678910111213141516171819202122232425262728import 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()
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.
Swipe to start coding
- Use the DataFrame
dfas 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.
Lösning
Tack för dina kommentarer!
single