Skip to content

Commit 1d88bb1

Browse files
committed
add plotting weather temperature forecast using matplotlib tutorial
1 parent f2a13df commit 1d88bb1

File tree

4 files changed

+94
-0
lines changed

4 files changed

+94
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
117117
- [How to Replace Text in Docx Files in Python](https://www.thepythoncode.com/article/replace-text-in-docx-files-using-python). ([code](general/docx-file-replacer))
118118
- [How to Make a Text Adventure Game in Python](https://www.thepythoncode.com/article/make-a-text-adventure-game-with-python). ([code](general/text-adventure-game))
119119
- [Zipf's Word Frequency Plot with Python](https://www.thepythoncode.com/article/plot-zipfs-law-using-matplotlib-python). ([code](general/zipf-curve))
120+
- [How to Plot Weather Temperature in Python](https://www.thepythoncode.com/article/interactive-weather-plot-with-matplotlib-and-requests). ([code](general/interactive-weather-plot/))
120121

121122

122123

Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# [How to Plot Weather Temperature in Python](https://www.thepythoncode.com/article/interactive-weather-plot-with-matplotlib-and-requests)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import matplotlib.pyplot as plt
2+
from matplotlib.widgets import RadioButtons
3+
import seaborn
4+
seaborn.set_style("darkgrid")
5+
import requests
6+
import json
7+
8+
# Define some Locations to choose from.
9+
# Latitude and Longitude
10+
locations = {
11+
'Schaffhausen': ['47.7', '8.6'],
12+
'Sydney': ['-33.86', '151.20'],
13+
'Kyiv': ['50.4422', '30.5367'],
14+
'Constantine': ['36.368258', '6.560254'],
15+
'Yakutsk': ['62.0', '129.7'],
16+
}
17+
18+
# Setting Up Matplotlib, using the OOP Approach
19+
fig, ax = plt.subplots()
20+
# the plot is created with the first location
21+
p = None
22+
23+
# make a function to get the temperatures of a given location
24+
def getTemperatures(location):
25+
# get the lat and long of the location
26+
lat, lon = locations[location]
27+
req = requests.get(f'https://api.open-meteo.com/v1/forecast?latitude={lat}&longitude={lon}&hourly=temperature_2m')
28+
req = json.loads(req.text)
29+
# get the tempratures
30+
temperatures = req['hourly']['temperature_2m']
31+
# get the times
32+
times = req['hourly']['time']
33+
return times, temperatures
34+
35+
36+
# Make a dictionary with the locations as keys and the getTemperatures() function as values
37+
location2data = {}
38+
for location in locations:
39+
location2data[location] = getTemperatures(location)
40+
41+
42+
def changeLocation(newLocation):
43+
global p
44+
# get the data of the location from the dictionary
45+
times, temperatures = location2data[newLocation]
46+
if p:
47+
p.set_ydata(temperatures)
48+
# reflect changes in the plot
49+
plt.draw()
50+
else:
51+
# Make a Plot and save the first object to a variable
52+
# p will be a Line2D object which can be changed at a later time
53+
p = ax.plot(times, temperatures, ls=':', lw=3)[0]
54+
# set the x-axis to the times
55+
xRange = list(range(0, 168, 24)) + [168]
56+
ax.set_xticks(xRange)
57+
# set the y-axis to the temperatures
58+
yRange = list(range(-20, 55, 5))
59+
ax.set_yticks(yRange)
60+
plt.tick_params(axis="both", which='both', labelrotation=-10) # rotate the labels
61+
# set the title
62+
ax.set_title('Temperatures in ' + newLocation)
63+
64+
65+
# Call the change Location function for the first time
66+
changeLocation('Schaffhausen')
67+
68+
# Making the Radio Buttons
69+
buttons = RadioButtons(
70+
ax=plt.axes([0.1, 0.1, 0.2, 0.2]),
71+
labels=locations.keys()
72+
)
73+
74+
# Connect click event on the buttons to the function that changes location.
75+
buttons.on_clicked(changeLocation)
76+
77+
# adjust the plot size
78+
plt.subplots_adjust(left=0.1, bottom=0.40)
79+
80+
# Label the Plot
81+
ax.set_xlabel('Times [Next Seven Days]')
82+
# ax.xaxis.label.set_color(labelColor)
83+
84+
ax.set_ylabel('Temperatures [Celcius]')
85+
# ax.yaxis.label.set_color(labelColor)
86+
87+
plt.savefig('file.svg', format='svg')
88+
89+
plt.show()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
matplotlib
2+
seaborn
3+
requests

0 commit comments

Comments
 (0)