Nowcast Chart
API: Nowcast API
This example demonstrates how to retrieve short-term precipitation forecasts from the Rainbow Nowcast API and visualize the forecasted precipitation rates over time using a filled line chart in Python with matplotlib
.
Code Snippet
The code below fetches precipitation forecasts for a given location and plots a filled line graph, where:
-
The x-axis represents time (UTC).
-
The y-axis represents precipitation intensity in mm/hr.
-
The area under the curve is filled with a soft blue color to visually emphasize forecasted rainfall periods.
chart.py
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
import os
import requests
from datetime import datetime, timezone
RAINBOW_API_TOKEN = os.getenv("RAINBOW_API_TOKEN")
# Put your location here
LON = -98.41419632222147
LAT = 42.146755295096966
nowcast_url = f"https://api.rainbow.ai/nowcast/v1/precip/{LON}/{LAT}?token={RAINBOW_API_TOKEN}"
response = requests.get(nowcast_url).json()
timestamps = [datetime.fromtimestamp(f["timestampBegin"], timezone.utc).strftime("%H: %M")
for f in response["forecast"]]
precipitation_rates = [f["precipRate"] for f in response["forecast"]]
# Plotting as filled line
fig, ax = plt.subplots(figsize=(10, 6))
ax.fill_between(timestamps, precipitation_rates, color="skyblue", alpha=0.6)
# Stroke (line) on top
ax.plot(timestamps, precipitation_rates, color="skyblue", linewidth=2)
ax.set_xlabel("Time")
ax.set_xlim(timestamps[0], timestamps[-1])
ax.set_ylabel("Precipitation Rate (mm/hr)")
ax.set_ylim(bottom=0.1)
# Show only every nth label (e.g., every 5th)
ax.set_xticks(timestamps[::10])
ax.set_xticklabels(timestamps[::10], rotation=45)
plt.title("Precipitation Rate Over Time (Filled Line)")
plt.tight_layout()
plt.show()