Basemap is a toolkit under the Python visualization library Matplotlib. its main function is to draw 2D maps, which are important for visualizing spatial data. basemap itself does not do any plotting, but provides the ability to transform coordinates into one of 25 different map projections. Matplotlib can also be used to plot contours, images, vectors, lines, or points in transformed coordinates. basemap includes the GSSH coastline dataset, as well as datasets from GMT for rivers, states, and national boundaries. These datasets can be used to plot coastlines, rivers, and political boundaries on a map at several different resolutions. basemap uses the Geometry Engine-Open Source (GEOS) library at the bottom to clip coastline and boundary features to the desired map projection area. In addition, basemap provides the ability to read shapefiles.
Basemap cannot be installed using pip install basemap. If Anaconda is installed, you can install basemap using canda install basemap.
For cases where it cannot be installed, you can download the compiled installation package: https://www.lfd.uci.edu/~gohlke/pythonlibs/. You need to install pyproj before installing basemap.
After installation, the following error will be reported when executing the code: KeyError: ‘PROJ_LIB’
Or set it directly in the environment variable.
Use of Basemap
Some example objects in basemap.
- contour() : draw contour lines.
- contourf() : draw filled contours.
- imshow() : draw an image.
- pcolor() : draw a pseudocolor plot.
- pcolormesh() : draw a pseudocolor plot (faster version for regular meshes).
- plot() : draw lines and/or markers.
- scatter() : draw points with markers.
- quiver() : draw vectors.(draw vector map, 3D is surface map)
- barbs() : draw wind barbs (draw wind plume map)
- drawgreatcircle() : draw a great circle (draws a great circle route)
Basemap Basic Usage
- The first line of code introduces the Basemap library and matplotlib, both of which are required.
- This map is created by the Basemap class, which contains many properties. If you use the defaults, the map has lat=0 and lon=0 centers and displays the map using the normal cylindrical projection mode (Plate Carrée projection). The projection formula is as simple as it can be: x=lon, y=lat. However, this projection is neither equal angle nor equal product, especially at high latitudes, and it is not practical because it differs greatly from reality. Changing the projection of the map is very simple, just add the projection parameter and the lat_0, lon_0 parameters to Basemap().
- In this example, the coastlines are drawn with the drawcoastlines() method, and the coastline data is already included in the library file by default.
- Finally, use the methods in pyplot to display and save the image.
Changing the projection is easy, just let’s switch to another projection ortho below and see the effect. In addition we use the methods fillcontinents() and drawmapboundary() to fill in the colors of the ocean and earth.
Projection types supported by Basemap.
How exactly to use and style can be tested for yourself.
Slightly more complex maps.
- The resolution parameter of the basemap class sets the resolution level, they are ‘c’ (original), ’l’ (low), ‘i’ (medium) ‘h’ (high), ‘f’ (full) or None (if no boundaries are used). This option is important, setting high resolution borders on the global map can be very slow.
- We can zoom from to a specific area, here the parameters are.
- llcrnrlat - latitude of the lower left corner
- llcrnrlon - longitude in the lower left corner
- urcrnrlat - latitude in the upper right corner
- urcrnrlon - the longitude of the upper right corner
- Longitude and latitude lines can be drawn with drawmeridians() and drawparallels().
Adding points and lines.
Basemap hands-on: Open Flights data visualization
The official website of Open Flights provides standalone text in the form of airports.dat, airlines.dat, routes.dat, here we use Data Quest provides a packaged .db file.