Consequences of human activities on Earth leave an indelible mark on our planet's weather patterns, with carbon dioxide (CO2) emissions, primarily from burning fossil fuels for energy, industrial processes, and transportation, playing a central role. These emissions accumulate in the atmosphere, creating a 'blanket' that traps heat, resulting in global warming. As temperatures increase, the likelihood of extreme weather events rises. Therefore, weather forecasting and monitoring is of utmost importance in understanding and responding to these changes.
In this tutorial I will explain how to get and plot current weather and forecast at any city in the world using Python. At the end of this tutorial we will have a weather forecast for a variable such as temperature, humidity, cloud cover, etc, which is plotted on a map or in a graph as shown in the figure 1 and 2.
Figure 1. Map of Temperature Forecast of Several Cities in the World
|Figure 2. Bar chart of Temperature Forecast of Several Cities in the World
Getting Weather Data
The weather data for this tutorial is coming from Open Meteo. Open Meteo is an open-source weather API that offers free access for non-commercial use. To provide the most suitable weather models for a specific location, including ensuring accurate and reliable forecasts, Open-Meteo partners with national weather services with high resolution, ranging from 1 to 11 kilometers. There is no API key required to use this service. You just need a correct API URL to retrieve the data.
Depends on your need, Open Meteo provides a lot of weather variables such as: temperature, humidity, pressure, cloud cover, wind direction and a lot more. Please check the weather forecast API page to see all available weather variables.
As example you want to get a temperature of New York city from 29th August 2023 until 30th August 2023. The API URL will look like this:
In the URL API above you will notice that the location of New York city is defined with a Geographic coordinate in Latitude and Longitude. Then the temperature variable name is temperature_2m, which means the air temperature at 2 meters above ground and at the is defined start and end date.
Copy the API URL and paste it into a browser. You should get a response as below.
Figure 3. Open Meteo Weather API Response
Process The Weather Data
We already have the response data, now let's parse it to make it more readable for user. Firstly let's get the current weather data.
Since the response is in JSON format with a dictionary data structure, the
current data can be retrieved from the response using the key:
current_weather. The code below is used to get the current weather data. From the code you can see some modules are required: request, json and pandas. the request and json modules are used to fetch the data and store it in a JSON format. Pandas is used to convert the data into pandas dataframe.
Running the the code above you should get a result as following:
|Figure 4. Getting Current Weather Data
Get Weather Data for Multiple City
Using the code above we get the weather data for a single city. Now let's get the data for several cities. For example we want to get weather data for New York, Cape Town, Jakarta, Copenhagen, Sydney, New Delhi, Tokyo and Dubai. Do we need to a define a different API URL for each city. The answer is YES, but we can do it in a loop instead of doing it one by one.
The code below is used for getting weather data for multiple cities as mentioned above. In the code we need two additional modules: numpy and time. Then we create variables that stored each city's coordinate. Some API URL parameters can can defined in the code such as start and end date at line 18-19. Weather variable can be set at line 22. Then using each key in the city dictionary data, we do looping to fetch weather data for each city. Lastly at the end we join all data.
Running the code you should get a pandas data frame as in figure 5.
|Figure 5. Weather Data in Pandas Data Frame
Plotting Weather Data on A Map
Congratulation! You already have all weather data for those cities. Of course it's somewhat difficult to see the data in a table rows. To make it easier to see and more interesting, let's visualize it by plotting the data into a map with a slider animation.
For plotting the data, we are using the Plotly library. So you need to install it first, if you don't have it. The code below is used to plot the data on a map. Running the code you should get a result as in figure 1 above.
Plotting Weather Data in A Bar Chart
Lastly let's plot the weather data in a bar chart also with a slider animation, therefore we can observe how the temperature change over time in each city. Using the Plotly module is quite easy to create a bar chart with a slider animation as in the code below. After execute the code you should get the result as in figure 2 above.
That's all this tutorial on how to get and plot weather data at any city in the world using Python using Open Meteo weather data API. In this tutorial you learnt how to get the weather data, process it in Python, plot the weather data into a map and in a bar chart. I hope this tutorial is useful. Thank you!
Anyway I provide the code in a notebook based on request. If you're interested, please make your request here.