{"id":676,"date":"2020-12-31T10:54:47","date_gmt":"2020-12-31T07:54:47","guid":{"rendered":"https:\/\/www.etemkeskin.com\/?p=676"},"modified":"2020-12-31T11:32:36","modified_gmt":"2020-12-31T08:32:36","slug":"data-visualisation-on-top-of-choropleth-map-using-python-folium","status":"publish","type":"post","link":"https:\/\/www.etemkeskin.com\/index.php\/2020\/12\/31\/data-visualisation-on-top-of-choropleth-map-using-python-folium\/","title":{"rendered":"Data Visualisation on Top of Choropleth Map Using Python Folium"},"content":{"rendered":"\n<p>While analyzing data, we can enrich our visuals by using Maps along with our charts. We will see how we can visualize our data on Top of Choropleth Map using the <strong>Folium<\/strong> package in Python.<\/p>\n\n\n\n<p>The Folium package uses the JavaScript <code>leaflet.js<\/code> library in the background.<\/p>\n\n\n\n<p>In the examples in this post, the choropleth map of the European Union countries will be used as a map. You can download the map data from the link <a href=\"https:\/\/github.com\/etemkeskin\/data_analyse\/blob\/master\/europe_union\/european-union-countries.json \">https:\/\/github.com\/etemkeskin\/data_analyse\/blob\/master\/europe_union\/european-union-countries.json<\/a> .You can try the examples in this post on <strong>jupyter notebook<\/strong>.<\/p>\n\n\n\n<p>If you do not have a folium package on your computer, you can install it with  <code>pip install folium<\/code>.<\/p>\n\n\n\n<p>First, we import the python packages to be used in the project.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import json\nimport folium\nimport numpy as np\nimport pandas as pd<\/pre>\n\n\n\n<p>Then, load our map data from the file to our project. The json structure of the map is important. You can create your own geojson file for the map from <a href=\"https:\/\/geojson.io\/\">https:\/\/geojson.io\/<\/a>.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">geo_str = json.dumps(json.load(open(\"european-union-countries.json\", 'r'))) # map data<\/pre>\n\n\n\n<p>We can create our map by writing the codes below. Since we will be using the map of Europe to determine where the map will start first, we have given the coordinates of Munich. We also set the zoom value to 3.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">mapeu = folium.Map(location=[48, 11], # Munich coordinates\n                  tiles=\"Mapbox Bright\",\n                  zoom_start=3)<\/pre>\n\n\n\n<p><br>We get our first choropleth map by adding GeoJSON data to our map.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">folium.GeoJson(\n    geo_str,\n    name='geojson'\n).add_to(mapeu)\nmapeu<\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"456\" height=\"455\" src=\"https:\/\/www.etemkeskin.com\/wp-content\/uploads\/2020\/12\/image-22.png\" alt=\"\" class=\"wp-image-682\"\/><\/figure>\n\n\n\n<p>We will add data to this map and color them according to the populations of the countries. Estimated population data of European countries are also included in the <strong>european-union-countries.json<\/strong> file. We create a dataframe from our json file by running the codes below.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"541\" height=\"583\" src=\"https:\/\/www.etemkeskin.com\/wp-content\/uploads\/2020\/12\/image-26.png\" alt=\"\" class=\"wp-image-689\"\/><\/figure>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Data preparation\njson_dict= json.load(open(\"european-union-countries.json\", 'r'))\ndf = pd.DataFrame([])  \nfor i,j in enumerate(json_dict[\"features\"]):\n    df = df_all3.append(pd.DataFrame(j[\"properties\"], index=[i]))<\/pre>\n\n\n\n<p>df.head(5)<\/p>\n\n\n\n<p>We can see the structure of the dataframe below;<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"1118\" height=\"428\" src=\"https:\/\/www.etemkeskin.com\/wp-content\/uploads\/2020\/12\/image-23.png\" alt=\"\" class=\"wp-image-683\"\/><\/figure>\n\n\n\n<p>We will use 7 color segments based on population on the map. For this color scale, we create a list by population.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"1094\" height=\"104\" src=\"https:\/\/www.etemkeskin.com\/wp-content\/uploads\/2020\/12\/image-24.png\" alt=\"\" class=\"wp-image-684\"\/><\/figure>\n\n\n\n<p>Now we can see the population distribution of the European Union countries on the map. In the code below, we match the country code column in the Datafram with the country code in the json file.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">folium.Choropleth(\n            geo_data=geo_str, # map data\n            data=df, # dataframe\n            columns=['gu_a3','pop_est'], # used columns\n            fill_color='YlGn',\n            key_on='feature.properties.gu_a3',#geojson country code\n            bins = scale, #color scale\n            legend_name='Estimated Population'\n                        ).add_to(mapeu)\nmapeu<\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"977\" height=\"586\" src=\"https:\/\/www.etemkeskin.com\/wp-content\/uploads\/2020\/12\/image-25.png\" alt=\"\" class=\"wp-image-685\"\/><\/figure>\n\n\n\n<h4>SOURCES<\/h4>\n\n\n\n<ol><li>https:\/\/geojson.io\/<\/li><li>https:\/\/www.openstreetmap.org\/<\/li><li>https:\/\/python-visualization.github.io\/folium\/quickstart.html<\/li><\/ol>\n","protected":false},"excerpt":{"rendered":"<p>While analyzing data, we can enrich our visuals by using Maps along with our charts. We will see how we can visualize our data on Top of Choropleth Map using the Folium package in Python. The Folium package uses the JavaScript leaflet.js library in the background. In the examples in this post, the choropleth map [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1,4],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v16.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Data Visualisation on Top of Choropleth Map Using Python Folium - blog website<\/title>\n<meta name=\"description\" content=\"While analyzing data, we can enrich our visuals by using Maps along with our charts. We will see how we can visualize our data on Top of Choropleth Map using the Folium package in Python. The Folium package uses the JavaScript leaflet.js library in the background.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.etemkeskin.com\/index.php\/2020\/12\/31\/data-visualisation-on-top-of-choropleth-map-using-python-folium\/\" \/>\n<meta property=\"og:locale\" content=\"tr_TR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Data Visualisation on Top of Choropleth Map Using Python Folium - blog website\" \/>\n<meta property=\"og:description\" content=\"While analyzing data, we can enrich our visuals by using Maps along with our charts. We will see how we can visualize our data on Top of Choropleth Map using the Folium package in Python. The Folium package uses the JavaScript leaflet.js library in the background.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.etemkeskin.com\/index.php\/2020\/12\/31\/data-visualisation-on-top-of-choropleth-map-using-python-folium\/\" \/>\n<meta property=\"og:site_name\" content=\"blog website\" \/>\n<meta property=\"article:published_time\" content=\"2020-12-31T07:54:47+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-12-31T08:32:36+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.etemkeskin.com\/wp-content\/uploads\/2020\/12\/image-22.png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Tahmini okuma s\u00fcresi\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 dakika\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.etemkeskin.com\/#website\",\"url\":\"https:\/\/www.etemkeskin.com\/\",\"name\":\"blog website\",\"description\":\"Etem KESK\\u0130N\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/www.etemkeskin.com\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"tr\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/www.etemkeskin.com\/index.php\/2020\/12\/31\/data-visualisation-on-top-of-choropleth-map-using-python-folium\/#primaryimage\",\"inLanguage\":\"tr\",\"url\":\"https:\/\/www.etemkeskin.com\/wp-content\/uploads\/2020\/12\/image-22.png\",\"contentUrl\":\"https:\/\/www.etemkeskin.com\/wp-content\/uploads\/2020\/12\/image-22.png\",\"width\":456,\"height\":455},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.etemkeskin.com\/index.php\/2020\/12\/31\/data-visualisation-on-top-of-choropleth-map-using-python-folium\/#webpage\",\"url\":\"https:\/\/www.etemkeskin.com\/index.php\/2020\/12\/31\/data-visualisation-on-top-of-choropleth-map-using-python-folium\/\",\"name\":\"Data Visualisation on Top of Choropleth Map Using Python Folium - blog website\",\"isPartOf\":{\"@id\":\"https:\/\/www.etemkeskin.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.etemkeskin.com\/index.php\/2020\/12\/31\/data-visualisation-on-top-of-choropleth-map-using-python-folium\/#primaryimage\"},\"datePublished\":\"2020-12-31T07:54:47+00:00\",\"dateModified\":\"2020-12-31T08:32:36+00:00\",\"author\":{\"@id\":\"https:\/\/www.etemkeskin.com\/#\/schema\/person\/dcbc30282861ce578b96a79ce8789629\"},\"description\":\"While analyzing data, we can enrich our visuals by using Maps along with our charts. We will see how we can visualize our data on Top of Choropleth Map using the Folium package in Python. The Folium package uses the JavaScript leaflet.js library in the background.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.etemkeskin.com\/index.php\/2020\/12\/31\/data-visualisation-on-top-of-choropleth-map-using-python-folium\/#breadcrumb\"},\"inLanguage\":\"tr\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.etemkeskin.com\/index.php\/2020\/12\/31\/data-visualisation-on-top-of-choropleth-map-using-python-folium\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.etemkeskin.com\/index.php\/2020\/12\/31\/data-visualisation-on-top-of-choropleth-map-using-python-folium\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Ana sayfa\",\"item\":\"https:\/\/www.etemkeskin.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Data Visualisation on Top of Choropleth Map Using Python Folium\"}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.etemkeskin.com\/#\/schema\/person\/dcbc30282861ce578b96a79ce8789629\",\"name\":\"etemkeskin\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/www.etemkeskin.com\/#personlogo\",\"inLanguage\":\"tr\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/6af0148b790691ed24ae245fb3dc773b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/6af0148b790691ed24ae245fb3dc773b?s=96&d=mm&r=g\",\"caption\":\"etemkeskin\"},\"url\":\"https:\/\/www.etemkeskin.com\/index.php\/author\/etemkeskinyahoo-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/posts\/676"}],"collection":[{"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/comments?post=676"}],"version-history":[{"count":43,"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/posts\/676\/revisions"}],"predecessor-version":[{"id":727,"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/posts\/676\/revisions\/727"}],"wp:attachment":[{"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/media?parent=676"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/categories?post=676"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/tags?post=676"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}