Skip to content

Hourly temperature chart

Compare air temperature and feels-like temperature over the next 24 hours using Python and Matplotlib.

Weather Forecast API · API reference

Keep your API key on the server

We do not recommend putting API keys in frontend code. Run this example locally or on your server. It reads the key from an environment variable and sends it in a request header.

Hourly air temperature as an orange line alongside a dashed blue feels-like temperature line

The chart uses the API's original hourly values, without smoothing or adding intermediate forecast points. Straight segments connect the readings.

  • Orange line: air temperature in °C.
  • Dashed blue line: feels-like temperature. Light shading shows the difference between the two series.
  • Time: UTC, with horizontal labels.

Python example

Save as temperature_chart.py and replace LON and LAT with your location. The script requires Celsius temperatures and 24 consecutive hourly readings; it stops with an error if readings are missing.

temperature_chart.py
import os
from datetime import datetime, timezone

import matplotlib.dates as mdates
import matplotlib.pyplot as plt
import requests

LON, LAT = -0.1278, 51.5074  # London; replace with your location.

# Request the forecast from the API.
# Keep the key on your server or local machine, never in frontend code.
response = requests.get(
    f"https://api.rainbow.ai/weather/v1/forecast/{LON}/{LAT}",
    params={"forecast_hours": 24},
    headers={"Ocp-Apim-Subscription-Key": os.environ["RAINBOW_API_KEY"]},
    timeout=15,
)
response.raise_for_status()
data = response.json()

# Prepare the temperature data.
if any(
    data["units"][key] != "celsius"
    for key in ("temperature", "feelsLikeTemperature")
):
    raise ValueError("This example expects temperatures in Celsius.")

hourly = sorted(
    data["timelines"]["hourly"],
    key=lambda row: row["startTimestamp"],
)[:24]

if len(hourly) != 24 or any(
    b["startTimestamp"] - a["startTimestamp"] != 3600
    for a, b in zip(hourly, hourly[1:])
):
    raise ValueError("Expected 24 consecutive hourly forecasts.")

times = [
    datetime.fromtimestamp(row["startTimestamp"], timezone.utc)
    for row in hourly
]
temperature = [row["temperature"] for row in hourly]
feels_like = [row["feelsLikeTemperature"] for row in hourly]

# Draw the chart.
plt.style.use("seaborn-v0_8-whitegrid")
fig, ax = plt.subplots(figsize=(10, 6), dpi=160, layout="constrained")

ax.plot(times, temperature, color="#D97745", marker="o", label="Air temperature")
ax.plot(times, feels_like, color="#3B82F6", linestyle="--", label="Feels like")
ax.fill_between(times, temperature, feels_like, color="#94A3B8", alpha=0.12)

ax.set(title="Hourly temperature", ylabel="Temperature (°C)", xlabel="Time (UTC)")
ax.xaxis.set_major_locator(
    mdates.HourLocator(byhour=range(0, 24, 3), tz=timezone.utc)
)
ax.xaxis.set_major_formatter(mdates.DateFormatter("%H:%M", tz=timezone.utc))
ax.legend()

# Save and display the chart.
fig.savefig("temperature-chart.jpg", pil_kwargs={"quality": 94, "optimize": True})
plt.show()

Run locally

Install the dependencies and set your key from Getting started:

Bash
python -m pip install requests "matplotlib>=3.6"
export RAINBOW_API_KEY='YOUR_API_KEY'
python temperature_chart.py

In Windows PowerShell, use $env:RAINBOW_API_KEY='YOUR_API_KEY' instead of export.

The script displays the chart and saves temperature-chart.jpg in the current folder. The forecast will differ when you run it. For a browser app, fetch the forecast through your backend and keep the key on the server.