Skip to content

Instantly share code, notes, and snippets.

@FabianZweckinger
Last active April 14, 2023 15:29
Show Gist options
  • Save FabianZweckinger/fc898eaa72ca1227b061b5cbe80c97be to your computer and use it in GitHub Desktop.
Save FabianZweckinger/fc898eaa72ca1227b061b5cbe80c97be to your computer and use it in GitHub Desktop.
Python request hourly temperature and humidity data at some coordinates using Open Meteo's Api (https://api.open-meteo.com/v1/forecast) and displaying with matplotlib
import requests
import matplotlib.pyplot as plt
import datetime
latitude = 48.3
longitude = 13.3
# Get data from api.open-meteo.com
response = requests.get('https://api.open-meteo.com/v1/forecast'
'?latitude=' + str(latitude) + '&longitude=' + str(longitude) + '&'
'hourly=temperature_2m,relativehumidity_2m,precipitation,windspeed_10m&models=best_match&daily=weathercode,temperature_2m_max,temperature_2m_min,precipitation_sum&timezone=Europe%2FBerlin')
data = response.json()
# Process data:
temps = data['hourly']['temperature_2m'][24:]
humidity = data['hourly']['relativehumidity_2m'][24:]
dates = [datetime.datetime.strptime(date.replace('T', ' '), '%Y-%m-%d %H:%M') for date in data['hourly']['time'][24:]]
# Setup mathplotlib:
fig, temp_ax = plt.subplots()
fig.autofmt_xdate()
temp_ax.set_xlabel("Date")
temp_ax.set_ylabel("Temperature ºC")
humidity_ax = temp_ax.twinx()
humidity_ax.set_ylabel("Humidity %")
all_lines = temps + humidity
temp_ax.legend(all_lines, ["Temperature", "Humidity"])
temp_lines = temp_ax.plot(dates, temps, 'c')
humidity_lines = humidity_ax.plot(dates, humidity, 'y')
# Line at 0
plt.axhline(y=0, color='b', linestyle='-')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment