<jupyter_start><jupyter_text>In this post, I generate a similar travel map using Cartopy. We will start by importing the required libraries, and then proceed to build up our desired map from a [given example](http://scitools.org.uk/cartopy/docs/v0.5/matplotlib/introductory_examples/01.great_circle.html). At each step, I re-import libraries so the code from that cell can be re-run on its own (see [this thread](https://twitter.com/jakevdp/status/978665789598257152) for a discussion).<jupyter_code>%matplotlib inline
import matplotlib
import cartopy
import pandas as pd
%load_ext watermark
%watermark -iv<jupyter_output>matplotlib  2.2.2
cartopy     0.16.0
pandas      0.22.0<jupyter_text>We start with a simple example from the Cartopy documentation to plot a ground track between two points.<jupyter_code>import matplotlib.pyplot as plt
import cartopy.crs as ccrs

ax = plt.axes(projection=ccrs.Robinson())
ax.set_global()
ax.coastlines()

# [lng1, lng2], [lat1, lat2]
plt.plot([-6.26, -82.99], [53.35, 39.96], color='blue', 
         transform=ccrs.PlateCarree())<jupyter_output><empty_output><jupyter_text>Switching from the `PlateCarree` transformation to the `Geodetic` transformation, we can instead plot a great circle between to points.<jupyter_code>import matplotlib.pyplot as plt
import cartopy.crs as ccrs

ax = plt.axes(projection=ccrs.Robinson())
ax.set_global()
ax.coastlines()

# [lng1, lng2], [lat1, lat2]
plt.plot([-6.26, -82.99], [53.35, 39.96], color='#2E5FAC',  
         transform=ccrs.Geodetic())<jupyter_output><empty_output><jupyter_text>Next we shade the land and water areas. Note that the water defaults to white so line 12 to specify white water is redundant, but I leave this in should you choose to use a different color. I will later remove this when increasing the resolution of the coastlines.<jupyter_code>import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy

ax = plt.axes(projection=ccrs.Robinson())
ax.set_global()
ax.coastlines()

# color land and water
ax.add_feature(cartopy.feature.LAND, zorder=0, edgecolor='#7f7f7f', 
               facecolor='#B1B2B4')
ax.add_feature(cartopy.feature.OCEAN, zorder=0, facecolor='white')

# [lng1, lng2], [lat1, lat2]
plt.plot([-6.26, -82.99], [53.35, 39.96], color='#2E5FAC',  
         transform=ccrs.Geodetic())<jupyter_output><empty_output><jupyter_text>Next we use `set_extent` to zoom in on the area of interest.<jupyter_code>%matplotlib inline
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy

ax = plt.axes(projection=ccrs.Robinson())

ax.set_global()
ax.coastlines()

# [lon_min, lon_max, lat_min, lat_max]
ax.set_extent([-120, 10, 20, 65], crs=ccrs.PlateCarree())

# color land and water
ax.add_feature(cartopy.feature.LAND, zorder=0, edgecolor='#7f7f7f', 
               facecolor='#B1B2B4')
ax.add_feature(cartopy.feature.OCEAN, zorder=0, facecolor='white')

# [lon1, lon2], [lat1, lat2]
plt.plot([-6.26, -82.99], [53.35, 39.96], color='#2E5FAC', marker='o',
         transform=ccrs.Geodetic())<jupyter_output><empty_output><jupyter_text>Next we add country and state borders.<jupyter_code>%matplotlib inline
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import cartopy

ax = plt.axes(projection=ccrs.Robinson())

ax.set_global()
ax.coastlines()

# [lon_min, lon_max, lat_min, lat_max]
ax.set_extent([-120, 10, 20, 65], crs=ccrs.PlateCarree())

# color land and water
ax.add_feature(cartopy.feature.LAND, zorder=0, 
               edgecolor='#7f7f7f', facecolor='#B1B2B4')
ax.add_feature(cartopy.feature.OCEAN, zorder=0, facecolor='white')

# country borders
ax.add_feature(cartopy.feature.BORDERS, zorder=0, 
               edgecolor='#000000', linewidth=.1)

# state borders
states_provinces = cfeature.NaturalEarthFeature(
        category='cultural',
        name='admin_1_states_provinces_lines',
        scale='50m',
        facecolor='none', linewidth=.1)
ax.add_feature(states_provinces, edgecolor='#D3D3D3')

# [lon1, lon2], [lat1, lat2]
plt.plot([-6.26, -82.99], [53.35, 39.96], color='#2E5FAC', 
         marker='o', transform=ccrs.Geodetic())<jupyter_output><empty_output><jupyter_text>Finally, we wrap this all up in a function, add lakes, increase the resolution of the land and coastlines, and load some data into a DataFrame to generate the plot.<jupyter_code>def plot_great_circles(df,fname):
    import matplotlib.pyplot as plt
    import cartopy.crs as ccrs
    import cartopy.feature as cfeature
    import cartopy

    ax = plt.axes(projection=ccrs.Robinson())

    ax.set_global()
    ax.add_feature(cfeature.LAKES, facecolor='white', 
                   edgecolor='black', linewidth=.1)
    ax.coastlines('50m', linewidth=0.2)

    # [lon_min, lon_max, lat_min, lat_max]
    ax.set_extent([-120, 10, 20, 65], crs=ccrs.PlateCarree())

    # add and color high-resolution land
    LAND_highres = cfeature.NaturalEarthFeature('physical', 'land', '50m',
                                                edgecolor='white',
                                                facecolor='#B1B2B',
                                                linewidth=.1
                                               )
    ax.add_feature(LAND_highres, zorder=0, 
                   edgecolor='#7f7f7f', facecolor='#B1B2B4')

    # add country borders
    ax.add_feature(cartopy.feature.BORDERS, zorder=0, 
                   edgecolor='#000000', linewidth=.1)

    # add state borders
    states_provinces = cfeature.NaturalEarthFeature(
        category='cultural',
        name='admin_1_states_provinces_lines',
        scale='50m', 
        zorder=1,
        facecolor='none', 
        linewidth=.1)
    ax.add_feature(states_provinces, edgecolor='#D3D3D3')

    # plot all great circles
    for row in range(df.shape[0]):
        Lat_Orig = df.loc[df.index[row],'Lat_Orig']
        Lng_Orig = df.loc[df.index[row],'Lng_Orig']
        Lat_Des = df.loc[df.index[row],'Lat_Des']
        Lng_Des = df.loc[df.index[row],'Lng_Des']
        plt.plot([Lng_Orig, Lng_Des], [Lat_Orig, Lat_Des], 
                 color='#2E5FAC', linewidth=.5,
                 marker='o', markerfacecolor='#CF5300',
                 markeredgecolor='#CF5300', markersize=1,
                 transform=ccrs.Geodetic())
    
    plt.savefig(fname,bbox_inches='tight', dpi=300)
df = pd.read_csv('2018_travel.csv')
df.head()<jupyter_output><empty_output><jupyter_text>And here is our final plot:<jupyter_code>plot_great_circles(df,fname='2018_Travel.png')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12700596056854654, 'fraction_numerical': 0.039889958734525444, 'mean_word_length': 3.6910394265232975, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 32, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '191926', 'license': 'MIT', 'n_tokens_mistral': 2485, 'n_tokens_neox': 2311, 'n_words': 498, 'path': 'docs/downloads/notebooks/travel-map-cartopy/Cartopy.ipynb', 'repo_name': 'Ronak1958/blog', 'size': '6543'}