Skip to content

Open weather map #2

@potnurubargavi-jpg

Description

@potnurubargavi-jpg

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

API_KEY = "YOUR_API_KEY" # Replace with your key
CITY = "London"

Fetch weather data

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

Parse needed info

dates = [datetime.fromtimestamp(x["dt"]) for x in data["list"]]
temps = [x["main"]["temp"] for x in data["list"]]
humidity = [x["main"]["humidity"] for x in data["list"]]

Plot

plt.figure(figsize=(10, 5))
plt.plot(dates, temps, label="Temp (°C)", color="orange")
plt.plot(dates, humidity, label="Humidity (%)", color="blue")
plt.title(f"5-Day Forecast for {CITY}")
plt.xlabel("Date & Time")
plt.xticks(rotation=45)
plt.legend()
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