Plotting Shapefiles in python using GEOPANDAS¶
In this tutorial, we will be using Pandas, Geopandas, matplotlib python library to create beautiful maps. We will start first by importing this libraries
import geopandas as gpd
import matplotlib.pyplot as plt
Let’s understand what this libraries are:
What is Geopandas?¶
Geopandas is an open-source Python library that extends the capabilities of pandas, a data analysis and manipulation library, to allow for spatial operations. It uses the Geometry data type from the shapely library to represent geospatial shapes and provides tools to work with geospatial data such as points, lines, and polyggon shapes. With Geopandas, you can perform tasks such as reading, writing, and manipulating geospatial data, performing spatial join operations, and plotting and visualizing geographical data on maps.
What is Pandas?¶
Pandas is an open-source Python library for data analysis and manipulation. It provides data structures for efficiently storing large datasets, such as arrays and dataframes, and provides tools for working with them. With pandas, you can perform operations such as reading and writing data from various sources, cleaning and transforming data, aggregating and summarizing data, and performing complex calculations on large datasets. It is widely used in data science, machine learning, and scientific computing. The library is known for its easy-to-use and expressive syntax, making it a popular choice among data scientists and researchers
What is Matplotlib?¶
Matplotlib is a plotting library for the Python programming language. It provides an interface for creating static, animated, and interactive visualizations in a variety of styles, including line plots, bar charts, scatter plots, histograms, and more. Matplotlib is widely used in data analysis and visualization, and is particularly well suited for creating publication-quality figures and visualizations. The library provides extensive customization options, allowing you to control the look and feel of your visualizations, and it integrates seamlessly with other popular libraries such as NumPy and Pandas. With Matplotlib, you can create high-quality visualizations to effectively communicate and explore your data.
Reading a the shape files¶
What is shapefile?¶
A Shapefile is a geospatial data format that is used to store vector data, such as points, lines, and polyggon shapes, and their associated attributes. It was developed by Esri, a leading GIS software provider, as a data format for their ArcView software. Shapefiles are now widely used and supported by many GIS software packages and have become a standard for exchanging geospatial data between different systems. A Shapefile is stored as a set of several files with different file extensions, including .shp, .shx, .dbf, and others. The .shp file stores the geometries, the .shx file provides an index for the geometries, the .dbf file stores the attribute data, and other files may store additional information such as projections and metadata.
coastlines=gpd.read_file("Coastlines/Global_coastlines_low_res.shp") # location to your shapefiles
Ploting the shapefiles¶
fig = plt.figure(figsize=[12,8])
ax = fig.add_axes([0, 0, 1, 1])
coastlines.plot(ax=ax)
plt.title("Global Coastlines")
Text(0.5, 1.0, 'Global Coastlines')
This code creates a Matplotlib figure object with a specified size of [12, 8]
.
fig = plt.figure(figsize=[12,8])
: This line creates a new figure object with a size of[12, 8]
.ax = fig.add_axes([0, 0, 1, 1])
: This line creates a new axis object with dimensions[0, 0, 1, 1]
, which covers the entire figure.coastlines.plot(ax=ax)
: This line plots thecoastlines
dataset on the created axisax
.plt.title("Global Coastlines")
: This line adds a title “Global Coastlines” to the plot.
This code demonstrates how to create a simple plot in Matplotlib, including creating a figure and axis object and plotting data on the axis. The plot created in this code will show the global coastlines.
Changing color and adding Outline¶
Matplotlib provides extensive support for color in visualizations. It allows users to specify colors using a variety of methods, including RGB values, HEX codes, and predefined color names. Additionally, color can be used to convey information in a visualization through the use of colormaps and color gradients.
You can check list of colors here:
fig = plt.figure(figsize=[12,8])
ax = fig.add_axes([0, 0, 1, 1])
coastlines.plot(ax=ax, facecolor="wheat", edgecolor="black",linewidth=0.1)
plt.title("Global Coastlines")
Text(0.5, 1.0, 'Global Coastlines')