# How to zoom into a Specific Region on a Cartopy Global Map using Python ?

Published: November 07, 2023

Tags: Python; Cartopy;

## Introduction

Cartopy is a powerful Python library used for geospatial data analysis, visualization and mapping. With cartopy, you can create maps from various projections including PlateCarree, Robinson, Miller, Mercator and many more. One of the key features of cartopy is its ability to handle global maps with ease, allowing users to zoom in and out of any region smoothly. In this tutorial, we will discuss how to zoom in on a cartopy global map in Python.

## Create a map with cartopy

To begin with, let us import the necessary libraries for creating and plotting our global map using cartopy. We will be using matplotlib for visualization and cartopy for mapping.

Next, we will create our global map by specifying the projection we want to use. In this example, we will be using Robinson projection, which is commonly used for global maps. Run the following code to create a figure and add axes with Robinson projection.

````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()`

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

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

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

`plt.title('How to zoom into a Specific Region on a Cartopy Global Map using Python ?')`

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

`plt.show()`
```

Run the code and you should see a global map with all these features displayed. If you want to customize the map further, you can add more features or change the projection as per your requirement.

Using PlateCarree()

````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()`

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

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

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

`plt.title('How to zoom into a Specific Region on a Cartopy Global Map using Python ?')`

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

`plt.show()`
```

## Zooming into a Specific Region using set_extent()

Now that we have our global map, let's say we want to zoom in on a specific region. This could be any country, continent or even a smaller area such as a city. To do this, we will use cartopy's `set_extent()` function, which allows us to specify the longitude and latitude range we want to zoom in on.

````ax.set_extent([-10, 20, -30, 15])`
```

The above code specifies a longitude range of -10 degrees to 20 degrees and a latitude range of -30 degrees to 15 degrees. Run the code and you will see the map being zoomed in on that specific region.

### Zooming into C.O.N.U.S

The Contiguous United States (CONUS) or Lower 48 is the region located in North America that comprises of the 48 adjoining states of the country, excluding Alaska and Hawaii. It is bordered to the north by Canada, to the east by Atlantic Ocean, to the south by Mexico and Gulf of Mexico, and to the west by Pacific Ocean. To zoom into CONUS for example just add the following line

````ax.set_extent([-124.736342, -66.945392, 24.521208, 49.382808])`
```

Full 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.Robinson()`

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

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

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

`ax.set_extent([-124.736342, -66.945392, 24.521208, 49.382808])`

`plt.title('How to zoom into a Specific Region on a Cartopy Global Map using Python ?')`

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

`plt.show()`
```

Same using PlateCarree projection

````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()`

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

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

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

`ax.set_extent([-124.736342, -66.945392, 24.521208, 49.382808])`

`plt.title('How to zoom into a Specific Region on a Cartopy Global Map using Python ?')`

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

`plt.show()`
```

## Adding Data to the Map

Apart from just visualizing features such as coastlines, borders and gridlines, cartopy also allows us to add our own data to the map. This could be in the form of shapefiles, raster images or any other geospatial data format. We can use cartopy's `add_geometries()` function to add any shapefile to our map. Let us load a shapefile of the countries in Africa and plot it on our map.