{"id":43,"date":"2022-08-01T12:00:00","date_gmt":"2022-08-01T12:00:00","guid":{"rendered":"https:\/\/blog.marcinpolkowski.com\/?p=43"},"modified":"2022-10-05T20:07:39","modified_gmt":"2022-10-05T20:07:39","slug":"plotting-maps-in-python","status":"publish","type":"post","link":"https:\/\/blog.marcinpolkowski.com\/?p=43","title":{"rendered":"Plotting maps in Python"},"content":{"rendered":"<p>Python is great for processing and visualizing data. Today I&#8217;d like to share simple tips around plotting data on map using Python.<\/p>\n<p>Let&#8217;s consider following example: I want to plot a map of airports around the world. Data for the airports can be obtained from <a href=\"https:\/\/raw.githubusercontent.com\/jpatokal\/openflights\/master\/data\/airports.dat\">OpenFlights<\/a>:<\/p>\n<p><center><\/p>\n<pre style=\"width:90%; overflow:auto; text-align:left;\">        <code class=\"language-python\">\nfrom io import StringIO\nimport pandas as pd\nimport requests\n\ncsvString = requests.get(\"https:\/\/raw.githubusercontent.com\/jpatokal\/openflights\/master\/data\/airports.dat\").text\ncsvStringIO = StringIO(csvString)\nairports = pd.read_csv(csvStringIO, sep=\",\", header=None)\n        <\/code>\n<\/pre>\n<p><\/center><\/p>\n<p>This will download the data and create a Pandas DataFrame. We can plot airport locations:<\/p>\n<p><center><\/p>\n<pre style=\"width:90%; overflow:auto; text-align:left;\">        <code class=\"language-python\">\nimport matplotlib.pyplot as plt\n\nax = plt.subplot(1,1,1)\nax.plot(airports[7], airports[6], 'r.', ms=.5)\nplt.axis([-180, 180, -90, 90])\n        <\/code>\n<\/pre>\n<p><\/center><\/p>\n<p><center><br \/>\n<a href=\"https:\/\/blog.marcinpolkowski.com\/wp-content\/uploads\/2022\/10\/airports_001.png\"><img decoding=\"async\" src=\"https:\/\/blog.marcinpolkowski.com\/wp-content\/uploads\/2022\/10\/airports_001.png\" width=\"50%\"><\/a><br \/>\n<\/center><\/p>\n<p>Nice plot, but not really a map yet. We need to add some contours. <strong>A Global Self-consistent, Hierarchical, High-resolution Geography Database (GSHHG)<\/strong> is a good place to start. You can get data and more info here: <a href=\"http:\/\/www.soest.hawaii.edu\/pwessel\/gshhg\/\">http:\/\/www.soest.hawaii.edu\/pwessel\/gshhg\/<\/a>. I recommend downloading &#8220;GSHHG coastlines, political borders and rivers in shapefile format (zip archive)&#8221;. Now we can plot much nicer map:<\/p>\n<p><center><\/p>\n<pre style=\"width:90%; overflow:auto; text-align:left;\">        <code class=\"language-python\">\nimport geopandas as gp\nimport matplotlib.pyplot as plt\n\ncoastline = gp.read_file('\/media\/RAID\/DATA\/GSHHG_2.3.7\/GSHHS_shp\/f\/GSHHS_f_L1.shp')\nax = plt.subplot(1,1,1)\n\ncoastline.boundary.plot(ax=ax, edgecolor='black', lw=0.5)\n\nax.plot(airports[7], airports[6], 'r.', ms=.5)\nplt.axis([-180, 180, -60, 85])\n        <\/code>\n<\/pre>\n<p><\/center><\/p>\n<p><center><br \/>\n<a href=\"https:\/\/blog.marcinpolkowski.com\/wp-content\/uploads\/2022\/10\/airports_002.png\"><img decoding=\"async\" src=\"https:\/\/blog.marcinpolkowski.com\/wp-content\/uploads\/2022\/10\/airports_002.png\" width=\"80%\"><\/a><br \/>\n<\/center><\/p>\n<p>Or going one step further:<\/p>\n<p><center><\/p>\n<pre style=\"width:90%; overflow:auto; text-align:left;\">        <code class=\"language-python\">\nimport geopandas as gp\nimport matplotlib.pyplot as plt\n\ncoastline = gp.read_file('\/media\/RAID\/DATA\/GSHHG_2.3.7\/GSHHS_shp\/c\/GSHHS_c_L1.shp')\nborders = gp.read_file('\/media\/RAID\/DATA\/GSHHG_2.3.7\/WDBII_shp\/c\/WDBII_border_c_L1.shp')\n\nlakes = gp.read_file('\/media\/RAID\/DATA\/GSHHG_2.3.7\/GSHHS_shp\/c\/GSHHS_c_L2.shp')\n\nax = plt.subplot(1,1,1)\n\ncoastline.boundary.plot(ax=ax, edgecolor='black', lw=0.5)\nborders.plot(ax=ax, edgecolor='black', lw=0.25)\nlakes.boundary.plot(ax=ax, edgecolor='black', lw=0.25)\n\nax.plot(airports[7], airports[6], 'r.', ms=.5)\nplt.axis([-180, 180, -60, 85])\n        <\/code>\n<\/pre>\n<p><\/center><\/p>\n<p><center><br \/>\n<a href=\"https:\/\/blog.marcinpolkowski.com\/wp-content\/uploads\/2022\/10\/airports_003.png\"><img decoding=\"async\" src=\"https:\/\/blog.marcinpolkowski.com\/wp-content\/uploads\/2022\/10\/airports_003.png\" width=\"80%\"><\/a><br \/>\n<\/center><\/p>\n<p>Note that in the last example we used &#8220;crude resolution&#8221; of the coastline and borders &#8211; we don&#8217;t need much detail for a map in such scale.<\/p>\n<p>Another source of shapefiles with borders that I recommend is <a href=\"https:\/\/gadm.org\/\">GADM<\/a>. You might be interested in looking at one of my public repositories for more details: <a href=\"https:\/\/github.com\/gozwei\/GADM-consolidated-shapefile\">GADM-consolidated-shapefile<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python is great for processing and visualizing data. Today I&#8217;d like to share simple tips around plotting data on map using Python. Let&#8217;s consider following example: I want to plot a map of airports around the world. Data for the airports can be obtained from OpenFlights: from io import StringIO import pandas as pd import [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-43","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/blog.marcinpolkowski.com\/index.php?rest_route=\/wp\/v2\/posts\/43","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.marcinpolkowski.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.marcinpolkowski.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.marcinpolkowski.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.marcinpolkowski.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=43"}],"version-history":[{"count":4,"href":"https:\/\/blog.marcinpolkowski.com\/index.php?rest_route=\/wp\/v2\/posts\/43\/revisions"}],"predecessor-version":[{"id":47,"href":"https:\/\/blog.marcinpolkowski.com\/index.php?rest_route=\/wp\/v2\/posts\/43\/revisions\/47"}],"wp:attachment":[{"href":"https:\/\/blog.marcinpolkowski.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=43"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.marcinpolkowski.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=43"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.marcinpolkowski.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=43"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}