Skip to content

Data integration and visualization  #4

@hyndavialthi-jpg

Description

@hyndavialthi-jpg

"""
Weather Data Visualization
Fetches 5-day forecast data from OpenWeatherMap API and visualizes temperature & humidity trends.

Author: Your Name
"""

import requests
import matplotlib.pyplot as plt
from datetime import datetime

-------- CONFIGURATION --------

API_KEY = "YOUR_API_KEY" # Replace with your OpenWeatherMap API key
CITY = "London"
UNITS = "metric" # "metric" for Celsius, "imperial" for Fahrenheit

-------- FETCH DATA --------

url = f"http://api.openweathermap.org/data/2.5/forecast?q={CITY}&appid={API_KEY}&units={UNITS}"
response = requests.get(url)
data = response.json()

Check if API returned an error

if response.status_code != 200 or "list" not in data:
print("Error fetching data:", data.get("message", "Unknown error"))
exit()

-------- PARSE DATA --------

dates = []
temps = []
humidity = []

for entry in data["list"]:
dt = datetime.fromtimestamp(entry["dt"])
temp = entry["main"]["temp"]
hum = entry["main"]["humidity"]

dates.append(dt)
temps.append(temp)
humidity.append(hum)

-------- VISUALIZE --------

plt.figure(figsize=(12, 6))
plt.plot(dates, temps, marker='o', label="Temperature (°C)", color="orange")
plt.plot(dates, humidity, marker='s', label="Humidity (%)", color="blue")

plt.title(f"Weather Forecast for {CITY}", fontsize=14)
plt.xlabel("Date & Time", fontsize=12)
plt.ylabel("Values", fontsize=12)
plt.xticks(rotation=45)
plt.legend()
plt.grid(True)
plt.tight_layout()

plt.show()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions