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')
coastlines.plot(ax=ax, facecolor=”wheat”, edgecolor=”black”,linewidth=0.1) plots the coastlines dataset on the axis ax, with the polygon interiors colored “wheat” and the polygon borders colored “black” with a line width of 0.1.
These are parameters in the plot function of the Geopandas library for visualizing geospatial data. They control various aspects of the appearance of the plot:
edgecolor:
Specifies the color of the polygon or line boundaries.
facecolor:
Specifies the color of the polygon interiors.
linewidth:
Specifies the width of the lines in the plot.markersize:
Specifies the size of markers in the plot, when plotting point geometries.
`alpha:’ Specifies the transparency of the plot, where 1.0 is fully opaque and 0.0 is fully transparent.
These parameters can be set to specific values or to sequences of values in order to control the appearance of different elements in the plot. By using these parameters, you can customize the appearance of your plots to effectively communicate your data and insights.
Plotting a Defined Region¶
fig = plt.figure(figsize=[12,8])
ax = fig.add_axes([0, 0, 1, 1])
ax.set_xlim([60,100])
ax.set_ylim([0,50])
coastlines.plot(ax=ax, facecolor="wheat", edgecolor="black",linewidth=0.1)
plt.title("Indian Subcontinent")
Text(0.5, 1.0, 'Indian Subcontinent')
print("Columns in our data are {}".format(coastlines.columns))
Columns in our data are Index(['FID_Global', 'PLATEID1', 'TYPE', 'FROMAGE', 'TOAGE', 'NAME', 'PLATEID2', 'GPGIM_TYPE', 'L_PLATE', 'R_PLATE', 'SPREAD_ASY', 'FEATURE_ID', 'IMPORT_AGE', 'FID_land_m', 'source', 'PLATEID1_1', 'FROMAGE_1', 'TOAGE_1', 'PLATEID2_1', 'GPGIM_TY_1', 'FEATURE__1', 'L_PLATE_1', 'R_PLATE_1', 'SPREAD_A_1', 'DESCR', 'RECON_METH', 'geometry'], dtype='object')
fig = plt.figure(figsize=[12,8])
ax = fig.add_axes([0, 0, 1, 1])
ax.set_xlim([100,150])
ax.set_ylim([-20,30])
coastlines.plot(column="FROMAGE",
ax=ax,
edgecolor="black" ,
vmin=0,
vmax=1000,
linewidth=0.5,
legend=True,
cmap="brg",
legend_kwds={'label': "Age of Formation (in Million years)",'orientation': "vertical"})
plt.title("South-East Asia")
Text(0.5, 1.0, 'South-East Asia')
This code is plotting a coastlines
dataset on an axis ax
with a color map that is determined by the values in the FROMAGE
column of the dataset.
column="FROMAGE"
: Specifies that the values in theFROMAGE
column of thecoastlines
dataset will be used to color the plot.ax=ax
: Specifies the axis on which the plot will be drawn.edgecolor="black"
: Specifies the color of the polygon or line boundaries as black.vmin=0
andvmax=1000
: Specify the minimum and maximum values that the color map should span.linewidth=0.5
: Specifies the width of the lines in the plot as 0.5.legend=True
: Enables a color legend to be displayed on the plot, which shows the mapping of color to values in theFROMAGE
column.cmap="brg"
: Specifies the color map to use. In this case, the “brg” color map is used. You can chose other color map as welllegend_kwds={'label': "Age of Formation (in Million years)",'orientation': "vertical"}
: Specifies additional parameters for the color legend. In this case, thelabel
is “Age of Formation (in Million years)” andlegend orientation
is vertical