Mapping geodata with Geopandas and Matplotlib
For now your are familiar with GIS python libraries.In this chapter we shall cover mapping geospatial data specifically Esri shapefile.I will be using Kenya Counties shapefile. To tackle this test you need to have installed python and you have pip installed geopandas and matplotlib but if you have anaconda environment the better.
in this steps i used Anaconda
Libraries importation
# import the needed libraries
Loading our data
Examine the data
we are able to display only the first five items . if we want to dispaly all are a specified number of item we place n value in the head bracket e.g countys.head(46). also through this code we have displayed the properties of our data
Plotting
# plotting our counties
Clip and intersecting
I have attached the codes and the data feel free to use them
The End
Thank you for your time
For now your are familiar with GIS python libraries.In this chapter we shall cover mapping geospatial data specifically Esri shapefile.I will be using Kenya Counties shapefile. To tackle this test you need to have installed python and you have pip installed geopandas and matplotlib but if you have anaconda environment the better.
in this steps i used Anaconda
Libraries importation
# import the needed libraries
- import geopandas as gpd
from matplotlib import pyplot as plt
%matplotlib inline
Loading our data
- # Loading our county shapefile data
countys=gpd.read_file("county.shp")
# we assign our county.shp data countys variable
# we request geopandas to read our shapefile county
Examine the data
- # we want to examine our shapefile data
countys.head()
we are able to display only the first five items . if we want to dispaly all are a specified number of item we place n value in the head bracket e.g countys.head(46). also through this code we have displayed the properties of our data
- # coordinate reference system
countys.crs
- # display columns
countys.columns
- # display length of columns
len(countys)
Plotting
# plotting our counties
- countys.plot(cmap="rainbow",figsize=(10,10))
# to add more eg countys.plot(cmap="rainbow",figsize=(10,10))
# cmap=rainbow assigns the map different rainbow colors also try a different color
# try jet color
# figsize makes the map to be 10 by 10
- # we want to plot unit perimeter
countys.plot(column="UNIT_PERIM",figsize=(10,10),legend=True)
# if you want to plot the data with specific target add column with the name of the column
- # importing towns and plotting
town=gpd.read_file("kenya_towns.shp")
town.plot(facecolor="black",figsize=(10,20))
# display both towns and counties
fig,ax=plt.subplots(1,figsize=(10,20))
countys.plot(ax=ax,cmap="jet",column="CODE")
town.plot(ax=ax,facecolor="black")
Clip and intersecting
- # dispaly single county from our data
nairobi=countys[countys.COUNTY_NAM=="Nairobi"]
nairobi.plot(figsize=(10,20),facecolor="none",edgecolor='black')
- # we select Kasarani town in Nairobi and plot it
home=town[town.TOWN_NAME=="Kasarani"]
home.plot(facecolor="black")
- # intersecting our Kasarani town with nairobi county
fig,ax=plt.subplots(1,figsize=(10,20))
home.plot(ax=ax,facecolor="black")
nairobi.plot(ax=ax,facecolor="none",edgecolor='black')
I have attached the codes and the data feel free to use them
The End
Thank you for your time