{"id":134750,"date":"2023-09-13T15:35:05","date_gmt":"2023-09-13T15:35:05","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=1651525"},"modified":"2023-09-13T15:35:05","modified_gmt":"2023-09-13T15:35:05","slug":"python-code-for-getting-historical-weather-data","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2023\/09\/13\/python-code-for-getting-historical-weather-data\/","title":{"rendered":"Python Code for Getting Historical Weather Data"},"content":{"rendered":"\n<div class=\"kk-star-ratings kksr-auto kksr-align-left kksr-valign-top\" data-payload='{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;1651525&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;1&quot;,&quot;legendonly&quot;:&quot;&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;5&quot;,&quot;starsonly&quot;:&quot;&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;5\\\/5 - (1 vote)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;title&quot;:&quot;Python Code for Getting Historical Weather Data&quot;,&quot;width&quot;:&quot;142.5&quot;,&quot;_legend&quot;:&quot;{score}\\\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}'>\n<div class=\"kksr-stars\">\n<div class=\"kksr-stars-inactive\">\n<div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div class=\"kksr-stars-active\" style=\"width: 142.5px;\">\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/div>\n<div class=\"kksr-legend\" style=\"font-size: 19.2px;\"> 5\/5 &#8211; (1 vote) <\/div>\n<\/p><\/div>\n<p>To get historical weather data in Python, install the Meteostat library using <code>pip install meteostat<\/code> or run <code>!pip install meteostat<\/code> with the exclamation mark prefix <code>!<\/code> in a Jupyter Notebook. <\/p>\n<p>If you haven&#8217;t already, also install the Matplotlib library using <code>pip install matplotlib<\/code>.<\/p>\n<p>Then, copy the following code into your programming environment and change the highlighted lines to set your own timeframe (<code>start<\/code>, <code>end<\/code>) and GPS <code>location<\/code>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"7,8,11\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Import Meteostat library and dependencies\nfrom datetime import datetime\nimport matplotlib.pyplot as plt\nfrom meteostat import Point, Daily # Set time period\nstart = datetime(2023, 1, 1)\nend = datetime(2023, 12, 31) # Create Point for Stuttgart, Germany\nlocation = Point(48.787399767583295, 9.205803269767616) # Get daily data for 2023\ndata = Daily(location, start, end)\ndata = data.fetch() # Plot line chart including average, minimum and maximum temperature\ndata.plot(y=['tavg', 'tmin', 'tmax'])\nplt.show()<\/pre>\n<p>This code fetches and visualizes the average, minimum, and maximum temperatures for <em>Stuttgart, Germany<\/em>, for the entire year of 2023 using the <a href=\"https:\/\/dev.meteostat.net\/python\/\">Meteostat library<\/a>.<\/p>\n<p>Here&#8217;s the output:<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" fetchpriority=\"high\" width=\"543\" height=\"449\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/09\/Untitled-5.png\" alt=\"\" class=\"wp-image-1651528\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/09\/Untitled-5.png 543w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/09\/Untitled-5-300x248.png 300w\" sizes=\"(max-width: 543px) 100vw, 543px\" \/><\/figure>\n<\/div>\n<p>Here are the three highlighted lines:<\/p>\n<ul>\n<li><code>start = datetime(2023, 1, 1)<\/code>: This sets the start date to January 1, 2023.<\/li>\n<li><code>end = datetime(2023, 12, 31)<\/code>: This sets the end date to December 31, 2023. Together, these lines define the time period for which we want to fetch the weather data.<\/li>\n<li><code>location = Point(48.787399767583295, 9.205803269767616)<\/code>: This creates a geographical point for Stuttgart, Germany using its latitude and longitude GPS coordinates. The <code><a href=\"https:\/\/dev.meteostat.net\/python\/api\/point\/#parameters\">Point<\/a><\/code> class is used to represent a specific location on Earth.<\/li>\n<\/ul>\n<p>You can use <a href=\"https:\/\/www.google.com\/maps\">Google Maps<\/a> to copy the GPS location of your desired location:<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"804\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/09\/image-72-1024x804.png\" alt=\"\" class=\"wp-image-1651526\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/09\/image-72-1024x804.png 1024w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/09\/image-72-300x236.png 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/09\/image-72-768x603.png 768w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/09\/image-72.png 1525w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n<p>You can try it yourself in the interactive Jupyter notebook (Google Colab):<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/colab.research.google.com\/drive\/1qdbzBwmlfK--eE7X05zmAI7AkMMgAikR?usp=sharing\" target=\"_blank\" rel=\"noreferrer noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"691\" height=\"460\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/09\/image-73.png\" alt=\"\" class=\"wp-image-1651530\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/09\/image-73.png 691w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/09\/image-73-300x200.png 300w\" sizes=\"auto, (max-width: 691px) 100vw, 691px\" \/><\/a><\/figure>\n<\/div>\n<p>If you want to become a Python master, get free cheat sheets, and coding books, check out the free Finxter email academy with 150,000 coders like you:<\/p>\n<p>The post <a rel=\"nofollow\" href=\"https:\/\/blog.finxter.com\/python-code-for-getting-historical-weather-data\/\">Python Code for Getting Historical Weather Data<\/a> appeared first on <a rel=\"nofollow\" href=\"https:\/\/blog.finxter.com\">Be on the Right Side of Change<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>5\/5 &#8211; (1 vote) To get historical weather data in Python, install the Meteostat library using pip install meteostat or run !pip install meteostat with the exclamation mark prefix ! in a Jupyter Notebook. If you haven&#8217;t already, also install the Matplotlib library using pip install matplotlib. Then, copy the following code into your programming [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-134750","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/134750","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=134750"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/134750\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=134750"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=134750"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=134750"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}