# How to plot a line between two points on a cartopy map ?

Published: November 07, 2023

Tags: Python; Cartopy; Map;

## Introduction

When working with geospatial data, it is often necessary to plot lines between two points on a map. This can be done using various mapping packages, including cartopy. Cartopy is an open-source Python package designed for working with geospatial data. It provides an easy-to-use interface for creating maps and visualizing geographical data.

## Plotting a Line between Two Points on a Cartopy Map

To plot a line between two points on a cartopy map, we first need to import the necessary packages. This includes cartopy and matplotlib:

````from matplotlib.pyplot import figure`

`import matplotlib.pyplot as plt`
`import cartopy.crs as ccrs`
```

Next, we need to define the two points we want to plot a line between. For example let's plot a line between New-York and Paris:

````ny_lat = 40.7128`
`ny_lon = - 74.0060`

`paris_lat = 48.8566`
`paris_lon = 2.3522`
```

We then need to specify the projection we want to use for our map. Cartopy supports various projections, including PlateCarree (default), Mercator, and Robinson. For this example, we will use the PlateCarree projection.

````fig = figure(num=None, figsize=(12, 10), dpi=100, edgecolor='k')`

`map_proj = ccrs.PlateCarree()`

`ax = plt.axes(projection=map_proj)`
```

we can add any desired features to our map, such as coastlines or gridlines,

````ax.set_global()`
`ax.gridlines()`

`ax.coastlines(linewidth=0.5, color='k', resolution='50m')`
```

and plot a line between the two points:

````plt.plot([ny_lon, paris_lon], [ny_lat, paris_lat],`
`         color='coral', linewidth=2, marker='o')`
```

and display or save the figure:

````plt.title('How to plot a line between two points on a cartopy map ?')`

`plt.savefig("plot_line_cartopy_02.png", bbox_inches='tight', facecolor='white')`

`plt.show()`
```

This will result in a map with a straight line connecting the two points:

### Plotting a Line between New-York and Paris

````from matplotlib.pyplot import figure`

`import numpy as np`
`import matplotlib.pyplot as plt`
`import cartopy.crs as ccrs`

`fig = figure(num=None, figsize=(12, 10), dpi=100, edgecolor='k')`

`map_proj = ccrs.PlateCarree()`

`ny_lat = 40.7128`
`ny_lon = - 74.0060`

`paris_lat = 48.8566`
`paris_lon = 2.3522`

`ax = plt.axes(projection=map_proj)`

`ax.set_global()`
`ax.gridlines()`

`ax.coastlines(linewidth=0.5, color='k', resolution='50m')`

`plt.plot([ny_lon, paris_lon], [ny_lat, paris_lat],`
`         color='coral', linewidth=2, marker='o')`

`plt.title('How to plot a line between two points on a cartopy map ?')`

`plt.savefig("plot_line_cartopy_02.png", bbox_inches='tight', facecolor='white')`

`plt.show()`
```

### Transforming line projection

By default, the line uses PlateCarree as a projection. If you modify the line

````plt.plot([ny_lon, paris_lon], [ny_lat, paris_lat],`
`         color='coral', linewidth=2, marker='o')`
```

to include `transform=ccrs.PlateCarree()`, such as in the example below:

````plt.plot([ny_lon, paris_lon], [ny_lat, paris_lat],`
`         color='coral', linewidth=2, marker='o',transform=ccrs.PlateCarree())`
```

you will obtain the same plot.

On Earth, the shortest distance between two points is not a straight line, but rather a curved line. To achieve this, simply transform the line to Geodetic. Geodetic is a mathematical concept that considers the curvature of the Earth's surface when calculating distance and direction. When plotting a line between two points on a cartopy map, it is important to take into account this curvature in order to accurately represent the shortest distance between the two points.

````plt.plot([ny_lon, paris_lon], [ny_lat, paris_lat],`
`         color='coral', linewidth=2, marker='o',transform=ccrs.Geodetic())`
```

Code:

````from matplotlib.pyplot import figure`

`import numpy as np`
`import matplotlib.pyplot as plt`
`import cartopy.crs as ccrs`
`import matplotlib.patches as mpatches`

`fig = figure(num=None, figsize=(12, 10), dpi=100, edgecolor='k')`

`map_proj = ccrs.PlateCarree()`

`ny_lat = 40.7128`
`ny_lon = - 74.0060`

`paris_lat = 48.8566`
`paris_lon = 2.3522`

`ax = plt.axes(projection=map_proj)`

`ax.set_global()`
`ax.gridlines()`

`ax.coastlines(linewidth=0.5, color='k', resolution='50m')`

`plt.plot([ny_lon, paris_lon], [ny_lat, paris_lat],`
`         color='coral', linewidth=2, marker='o',transform=ccrs.Geodetic())`

`plt.title('How to plot a line between two points on a cartopy map ?')`

`plt.savefig("plot_line_cartopy_03.png", bbox_inches='tight', facecolor='white')`

`plt.show()`
```

This will result in a map with a curved line connecting the two points:

### Alternative map projection: Robinson

Utilizing an alternative map projection, such as Robinson, and incorporating geodesic transformation

````from matplotlib.pyplot import figure`

`import numpy as np`
`import matplotlib.pyplot as plt`
`import cartopy.crs as ccrs`
`import matplotlib.patches as mpatches`

`fig = figure(num=None, figsize=(12, 10), dpi=100, edgecolor='k')`

`map_proj = ccrs.Robinson()`

`ny_lat = 40.7128`
`ny_lon = - 74.0060`

`paris_lat = 48.8566`
`paris_lon = 2.3522`

`ax = plt.axes(projection=map_proj)`

`ax.set_global()`
`ax.gridlines()`

`ax.coastlines(linewidth=0.5, color='k', resolution='50m')`

`plt.plot([ny_lon, paris_lon], [ny_lat, paris_lat],`
`         color='coral', linewidth=2, marker='o',transform=ccrs.Geodetic())`

`plt.title('How to plot a line between two points on a cartopy map ?')`

`plt.savefig("plot_line_cartopy_04.png", bbox_inches='tight', facecolor='white')`

`plt.show()`
```

This will result in a map with a curved line connecting the two points:

Image

of