<jupyter_start><jupyter_text>The Problem With MapsMaps have been such a mainstay of our lives for so long now that it's hard to imagine just how complex it is to create one. Keep in mind though, the earth is a 3-dimensional spherical object, and until we reach a point where some type of Star Wars-style, 3-dimensional holographic image is available, and easily portable, we'll be stuck with the problem of "projecting" the globe onto a 2-dimensional surface. What this means is that every map you've ever looked at was an estimation of the real world. In other words, every map you've ever used was flawed in some way, and the decision to use a particular "projection" was always met with a careful analysis of the trade-offs associated with each one. With that in mind, it's easy to understand why the simple task of displaying a map becomes a cost/benefit analysis of the different projection methods. What is a Map Projection?You've now come across the term "projection" at least 3 different times in the last paragraph, so you may be asking yourself "what is a projection?" Well, before I answer that question, let's run through a quick thought experiment to understand the problem of map projection a bit more intuitively. Imagine taking an orange and drawing a map of the world on its surface. Then, take the peel of that orange and try to flatten it out so that you have a perfectly flat map of the world. The problem that you're going to run into is that the peel cannot be flattened without ripping and leaving gaps between different areas of the map. Now, to solve the problem of ripping, we could instead do the same trick as before, but this time with a balloon instead of an orange peel. The problem then becomes, in the areas where there were rips before, now those areas have become stretched and distorted. In other words, the areas furthest from the center of the balloon have become substantially stretched, while those towards the center have remained relatively unchanged. You can think of the balloon approach that we just discussed as an analogy for a map projection. In other words, a map projection is a method for flattening 3D objects. More specifically, a [map projection][1] is a mathematical equation for transforming points in a 3-dimensional space into coordinates on a 2-dimensional surface.It should now be a bit easier to see that the problem of "projecting" the world onto a flat surface is really a question of choosing how best to distort a map given a specific purpose for its use. As an example, the inventor of the mercator projection (seen below), the most popular version of the world map in use today, created a map that substantially exaggerated the size of the northern continents, such as Europe and North America, making them seem far bigger than those in the southern half of the world. This made it relatively easy for sailors to create perfectly straight routes along latitude and longitude lines. The cost, of course, was the exaggerated land mass of the northern continents. So, you see, the simple act of displaying a map is really all a question of trade-offs, and choosing the right map for the job.<!---For anyone interested in learning more about map projectionsThe Environmental Systems Research Institute (ESRI), an international supplier of Geographic Information System (GIS) software, has released a fantastic book entitled ["Understanding Map Projections"][1]. If you're interested in --->[1]: http://downloads2.esri.com/support/documentation/ao_/710Understanding_Map_Projections.pdf<jupyter_code>%matplotlib inline

import numpy as np
from matplotlib.collections import PatchCollection
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap, Polygon

from IPython.display import set_matplotlib_formats
set_matplotlib_formats('retina')

# The current version of NumPy available from conda is issuing a warning 
# message that some behavior will change in the future when used with the 
# current version of matplotlib available from conda. This cell just keeps
# that warning from being displayed.
import warnings
warnings.simplefilter(action="ignore", category=FutureWarning)<jupyter_output><empty_output><jupyter_text>The Mercator Projection<jupyter_code>plt.figure(figsize=(16,8))

# Create a Basemap instance that uses the mercator projection
m = Basemap(projection='merc',
            urcrnrlat=80, llcrnrlat=-80,      # Top and Bottom parallels
            llcrnrlon=-180, urcrnrlon=180)    # Left and Right meridians

# Draw the coastlines and fill the continents all with the same blue.
# The color of inland lakes defaults to the background color of the 
# axis, but we'll set the lake_color here to make sure that it is white.
m.drawcoastlines(color='#0066CC')
m.fillcontinents(color='#0066CC', lake_color='white')

# Draw the boundaries of each country in white
m.drawcountries(color='white')

# Draw lines of latitude (parallels) and longitude (meridians)
m.drawparallels(range(-90,91,30), color='#CCCCCC')
m.drawmeridians(range(-180,181,60), color='#CCCCCC')

# Draws the bounding box around the map and sets the background 
# color (fill_color) for the entire map. 
m.drawmapboundary(fill_color='white')

# Give the map a title
plt.title("Mercator Projection")

# draw tissot's indicatrix to show distortion.
for y in np.linspace(m.ymax/20, 19*m.ymax/20, 9):
    for x in np.linspace(m.xmax/20, 19*m.xmax/20, 9):
        lon, lat = m(x,y,inverse=True)
        poly = m.tissot(lon, lat, 1.5, 100, facecolor='#2ca25f', zorder=10, alpha=0.6);<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.05768194070080863, 'fraction_numerical': 0.013477088948787063, 'mean_word_length': 4.828272251308901, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '358308', 'license': 'MIT', 'n_tokens_mistral': 1530, 'n_tokens_neox': 1419, 'n_words': 789, 'path': '07 - Mapping in matplotlib/0701 - What is a Map Projection?.ipynb', 'repo_name': 'croach/mastering-matplotlib', 'size': '5565'}