How it works…

In the first line of code, we import the cartopy.crs module with the ccrs alias. We then create a Figure object and an Axes object with the following call:

ax = fig.add_subplot(1, 1, 1, 
projection=ccrs.Mollweide())

Notice that we need to specify the projection option in the add_subplot() method call. Cartopy makes available several different projections for drawing global maps.

Next, we add to the figure the continent coastlines and a stock image of the globe with the following commands:

ax.coastlines()
ax.stock_img()
The first time you run these commands, cartopy downloads the data it needs to produce the map.

The next step is to add data to the map. In a more realistic example, we would be using a dataset that includes some geographical information. In this example, we simply mark two cities in the globe and draw the shortest path on the Earth's surface connecting these two cities.

We start by defining the longitude and latitude of the cities. Then, we use the standard Matplotlib plot() and text() functions to add the graphical elements to the map. Notice the use of transform=ccrs.Geodetic() to map the coordinates to the correct locations on the map.

As demonstrated in this example, cartopy has a simple interface to generate maps and add geographical information. The full documentation of the API can be found at http://scitools.org.uk/cartopy/.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset