matplotlib+cartopy+geopandas for professional map visualization

Someone on Zhihu asked how to achieve a refined map? Existing excel, tableau, and powerbi can only apply limited templates. Is there any tool that can achieve high customization?

In addition to professional Gis software, I can think of relatively perfect is to use Python to achieve.

If you want to make publication-level map visualization charts and deal with large data sets, it is recommended to use the combination of matplotlib+cartopy+geopandas, from GIS data processing, to Geo, Map map drawing, to visual image display generation, they can be perfectly solved.

matplotlib, cartopy, and geopandas are all third-party tool libraries for python, which are very powerful in the field of visualization.

matplotlib is the basic library for python chart visualization, and I believe many people are familiar with it. It can create static, dynamic, interactive charts, supports customization of all chart elements, and is very friendly to map making.

cartopy is a professional geospatial visualization library based on the matplotlib interface. It utilizes PROJ, Numpy and Shapely libraries to draw publication-grade geographic charts.

geopandas is a geospatial data processing and analysis library built on the pandas data type, which can process, analyze and visualize shapefile and geojson data.

In general, matplotlib is used for chart design, cartopy is used for map display, and geopandas is used for gis data processing, which can be used in combination with professional gis software.

And they are extremely customizable, you can design almost all the map details by yourself, which is impossible for tableau, finereport, and excel.

Because it is a map tool based on the python ecology, it is not a problem to deal with large data sets. Even if the data at the GB and TB level, maps can be generated by appropriate data processing methods.

cartopy drawing

The basic process of drawing a map with Cartopy is not complicated:

  1. Create a canvas.

  2. Create a GeoAxes object by specifying the projection parameter.

  3. Call the methods of GeoAxes to draw.

For example, to draw a coastline:

import cartopy.crs as ccrs
from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter
import matplotlib.pyplot as plt


def main():
    fig = plt.figure(figsize=(8, 10))

    # Label axes of a Plate Carree projection with a central longitude of 180:
    ax1 = fig.add_subplot(2, 1, 1,
                          projection=ccrs.PlateCarree(central_longitude=180))
    ax1.set_global()
    ax1.coastlines()
    ax1.set_xticks([0, 60, 120, 180, 240, 300, 360], crs=ccrs.PlateCarree())
    ax1.set_yticks([-90, -60, -30, 0, 30, 60, 90], crs=ccrs.PlateCarree())
    lon_formatter = LongitudeFormatter(zero_direction_label=True)
    lat_formatter = LatitudeFormatter()
    ax1.xaxis.set_major_formatter(lon_formatter)
    ax1.yaxis.set_major_formatter(lat_formatter)

    plt.show()


if __name__ == '__main__':
    main()

Draw the map:

import cartopy.crs as ccrs
import cartopy.feature as cfeature
import matplotlib.pyplot as plt


def main():
    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1, projection=ccrs.PlateCarree())
    ax.set_extent([-20, 60, -40, 45], crs=ccrs.PlateCarree())

    ax.add_feature(cfeature.LAND)
    ax.add_feature(cfeature.OCEAN)
    ax.add_feature(cfeature.COASTLINE)
    ax.add_feature(cfeature.BORDERS, linestyle=':')
    ax.add_feature(cfeature.LAKES, alpha=0.5)
    ax.add_feature(cfeature.RIVERS)

    plt.show()


if __name__ == '__main__':
    main()

##geopandas plotting

geopandas is mainly used to process geospatial data, and it can also display maps through the matplotlib interface.

Of course, it also relies on many geospatial libraries such as shapely, fiona, and pyproj for data analysis and processing. The data form is similar to pandas dataframe.

import geopandas as gpd
from matplotlib_scalebar.scalebar import ScaleBar

nybb = gpd.read_file(gpd.datasets.get_path('nybb'))
nybb = nybb.to_crs(32619)  # Convert the dataset to a coordinate
# system which uses meters

ax = nybb.plot()
ax.add_artist(ScaleBar(1))

import geopandas
import contextily as cx

df = geopandas.read_file(geopandas.datasets.get_path('nybb'))
ax = df.plot(figsize=(10, 10), alpha=0.5, edgecolor='k')

df.crs
df_wm = df.to_crs(epsg=3857)
ax = df_wm.plot(figsize=(10, 10), alpha=0.5, edgecolor='k')
cx.add_basemap(ax)

You can also read the map via folium for visualization.

summary

The combination of matplotlib+cartopy+geopandas is very powerful and can solve most of the visualization needs of geospatial.

I think python processing geospatial data is also the current trend, and learning it will help a lot.

Tags: Python Data Analysis matplotlib

Posted by fleabay on Sun, 09 Oct 2022 22:01:44 +0530