{"id":11348,"date":"2020-12-10T20:42:13","date_gmt":"2020-12-10T20:42:13","guid":{"rendered":"https:\/\/www.askpython.com\/?p=11348"},"modified":"2020-12-10T20:42:14","modified_gmt":"2020-12-10T20:42:14","slug":"plot-geographical-data-python-plotly","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/examples\/plot-geographical-data-python-plotly","title":{"rendered":"Plot Geographical Data on a Map Using Python Plotly"},"content":{"rendered":"\n<p>In this tutorial, we will learn how to plot geographical data on a map using Python Plotly. For this demonstration, we&#8217;ll plot COVID-19 cases from <a href=\"https:\/\/ourworldindata.org\/\" target=\"_blank\" rel=\"noopener\">ourworldindata.org<\/a> dataset.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Steps to Plot Geographical Data on a Map in Python<\/h2>\n\n\n\n<p>Let&#8217;s get started. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. Import the required libraries<\/h3>\n\n\n\n<p>Let&#8217;s start with <a href=\"https:\/\/www.askpython.com\/python\/python-import-statement\" class=\"rank-math-link\">importing the necessary libraries<\/a>. We need to import the following two libraries:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><a href=\"https:\/\/www.askpython.com\/python-modules\/pandas\/python-pandas-module-tutorial\" class=\"rank-math-link\">Pandas<\/a><\/li><li><a href=\"https:\/\/www.askpython.com\/python-modules\/python-plotly-tutorial\" class=\"rank-math-link\">Plotly.express<\/a> <\/li><\/ol>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport pandas as pd\nimport plotly.express as px\n<\/pre><\/div>\n\n\n<p>Now we can move to the next step, that is downloading the dataset. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. Download and read the dataset<\/h3>\n\n\n\n<p>To download and load the dataset, use the following piece of code.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n!wget https:\/\/covid.ourworldindata.org\/data\/owid-covid-data.csv\n<\/pre><\/div>\n\n\n<p>Output :<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"182\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/12\/download-dataset-1024x182.png\" alt=\"Download Dataset\" class=\"wp-image-11349\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/12\/download-dataset-1024x182.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/12\/download-dataset-300x53.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/12\/download-dataset-768x137.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/12\/download-dataset.png 1112w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Download Dataset<\/figcaption><\/figure><\/div>\n\n\n\n<p><a href=\"https:\/\/www.askpython.com\/python-modules\/python-csv-module\" class=\"rank-math-link\">Read the CSV dataset in Python<\/a> using the pandas read_csv method:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndf = pd.read_csv(&#039;owid-covid-data.csv&#039;)\ndf.head()\n<\/pre><\/div>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"269\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/12\/dataset-1024x269.png\" alt=\"Dataset\" class=\"wp-image-11350\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/12\/dataset-1024x269.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/12\/dataset-300x79.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/12\/dataset-768x202.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/12\/dataset.png 1088w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Dataset<\/figcaption><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">3. Plotting the COVID-19 Dataset on a Map<\/h3>\n\n\n\n<p>Now we can use Plotly to plot the data from the dataset above. <\/p>\n\n\n\n<p>We are plotting a <strong>Choropleth Map<\/strong>. It is a map which uses differences in shading, colouring, or the placing of symbols within predefined areas to indicate the average values of a particular quantity in those areas.<\/p>\n\n\n\n<p>We are going to plot the <strong>number of new cases each day.<\/strong> Plotly lets us do that through animations. <\/p>\n\n\n\n<p>Here&#8217;s the code to plot the colors and shades on the map:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfig = px.choropleth(df, locations=&quot;iso_code&quot;,\n                    color=&quot;new_cases&quot;,\n                    hover_name=&quot;location&quot;,\n                    animation_frame=&quot;date&quot;,\n                    title = &quot;Covid Cases plotted using Plotly&quot;,                 color_continuous_scale=px.colors.sequential.PuRd)\n\n\nfig&#x5B;&quot;layout&quot;].pop(&quot;updatemenus&quot;)\nfig.show()\n<\/pre><\/div>\n\n\n<p><strong>Output :<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"409\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/12\/Feb-1024x409.png\" alt=\"Feb\" class=\"wp-image-11351\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/12\/Feb-1024x409.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/12\/Feb-300x120.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/12\/Feb-768x307.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/12\/Feb.png 1274w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Feb<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"403\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/12\/April-1024x403.png\" alt=\"Plot Geographical Data - April\" class=\"wp-image-11352\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/12\/April-1024x403.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/12\/April-300x118.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/12\/April-768x302.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/12\/April.png 1260w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>April<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"408\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/12\/September--1024x408.png\" alt=\"Plot Geographical Data in Python Plotly - September\" class=\"wp-image-11353\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/12\/September--1024x408.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/12\/September--300x119.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/12\/September--768x306.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/12\/September-.png 1258w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>September <\/figcaption><\/figure>\n\n\n\n<p>The output shows how the map looks over three different months of the year. We can see that the virus was spreading rapidly in China in the month of February, in USA in the month of April and in India in the month of September. <\/p>\n\n\n\n<p>You can also hover over any region of the map and view the number of new cases. <\/p>\n\n\n\n<p>Plotly animations make it convenient to visualize time series data. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4. Complete code to Plot Geographical Data using Python Plotly<\/h3>\n\n\n\n<p>The complete code for this section in given below :<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport pandas as pd\nimport plotly.express as px\n\n#download dataset\n!wget https:\/\/covid.ourworldindata.org\/data\/owid-covid-data.csv\n\n#import dataset\ndf = pd.read_csv(&#039;owid-covid-data.csv&#039;)\n\n#plot\nfig = px.choropleth(df, locations=&quot;iso_code&quot;,\n                    color=&quot;new_cases&quot;,\n                    hover_name=&quot;location&quot;,\n                    animation_frame=&quot;date&quot;,\n                    title = &quot;Covid Cases plotted using Plotly&quot;,                 color_continuous_scale=px.colors.sequential.PuRd)\n\n\nfig&#x5B;&quot;layout&quot;].pop(&quot;updatemenus&quot;)\nfig.show()\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">6. Plotting COVID-19 Asia Data<\/h3>\n\n\n\n<p>You can also set the scope of the map to Asia. Let&#8217;s see how to plot geographical data for the content of Asia.<\/p>\n\n\n\n<p>To set the scope of the plot to Asia, set the parameter <em>&#8216;scope<\/em>&#8216; to &#8216;<em>asia&#8217;<\/em>. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport pandas as pd\nimport plotly.express as px\n\n#download dataset\n!wget https:\/\/covid.ourworldindata.org\/data\/owid-covid-data.csv\n\n#import dataset\ndf = pd.read_csv(&#039;owid-covid-data.csv&#039;)\n\n#select entries with the continent as asia\ndf = df&#x5B;df.continent == &#039;Asia&#039;]\n\n#plot\nfig = px.choropleth(df, locations=&quot;iso_code&quot;,\n                    color=&quot;new_cases&quot;,\n                    hover_name=&quot;location&quot;,\n                    animation_frame=&quot;date&quot;,\n                    title = &quot;Daily new COVID cases&quot;,\n                    scope =&#039;asia&#039;,  color_continuous_scale=px.colors.sequential.PuRd)\n\nfig&#x5B;&quot;layout&quot;].pop(&quot;updatemenus&quot;)\nfig.show()\n<\/pre><\/div>\n\n\n<p>Output : <\/p>\n\n\n\n<figure class=\"wp-block-video aligncenter\"><video height=\"493\" style=\"aspect-ratio: 1300 \/ 493;\" width=\"1300\" controls src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/12\/covid-cases-in-asia-1.mov\"><\/video><figcaption>Covid cases in Asia <\/figcaption><\/figure>\n\n\n\n<p>This video shows new cases of COVID-19 for each day with the help of animation. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion <\/h2>\n\n\n\n<p>This tutorial was on plotting geographical data in Python Plotly. We plotted data from the Covid-19 dataset using Plotly in python. To learn about other types of maps that Plotly lets you create, read their <a href=\"https:\/\/plotly.com\/python\/maps\/\" class=\"rank-math-link\" target=\"_blank\" rel=\"noopener\">official documentation. <\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we will learn how to plot geographical data on a map using Python Plotly. For this demonstration, we&#8217;ll plot COVID-19 cases from ourworldindata.org dataset. Steps to Plot Geographical Data on a Map in Python Let&#8217;s get started. 1. Import the required libraries Let&#8217;s start with importing the necessary libraries. We need to [&hellip;]<\/p>\n","protected":false},"author":14,"featured_media":11356,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-11348","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-examples"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/11348","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/14"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=11348"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/11348\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/11356"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=11348"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=11348"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=11348"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}