Search code examples
pythonmappingoverlaygeopandas

Python Overlay Different Data into Single Map


i am trying to overlay two sets of latitude and longitude plots so that the first set has points of one color and the second set of points has a different color plotted on the same map. I have tried to share the same axis (ax) but it keeps plotting the points in 2 maps instead of 1 single map with both sets or colors of points. My code looks like this:

from sys import exit
from shapely.geometry import Point
import geopandas as gpd
from geopandas import GeoDataFrame as gdf
from shapely.geometry import Point, LineString
import pandas as pd
import matplotlib.pyplot as plt

dfp = pd.read_csv("\\\porfiler03\\gtdshare\\Long_Lats_90p.csv", delimiter=',', skiprows=0, 
low_memory=False)

geometry = [Point(xy) for xy in zip(dfp['Longitude'], dfp['Latitude'])]
gdf = gpd.GeoDataFrame(dfp, geometry=geometry)   

#this is a simple map that goes with geopandas
fig, ax = plt.subplots()
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
#world = world[(world.name=="Spain")]
gdf.plot(ax=world.plot(figsize=(10, 6)), marker='o', color='red', markersize=15);


dfn = pd.read_csv("\\\porfiler03\\gtdshare\\Long_Lats_90n.csv", 
delimiter=',', skiprows=0, 
low_memory=False)
geometry = [Point(xy) for xy in zip(dfn['Longitude'], dfn['Latitude'])]
gdf = gpd.GeoDataFrame(dfn, geometry=geometry)   

#this is a simple map that goes with geopandas
#world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
gdf.plot(ax=world.plot(figsize=(10, 6)), marker='o', color='yellow', 
markersize=15);

My first plot looks like the second plot below but with red points in USA and Spain:

My second plot looks like this:

Yellow Points in Spain

Thank you in helping me overlay these two different sets of points and colors into one map.


Solution

  • In your case, you want to plot 3 geodataframes (world, gdf1, and gdf2) on single axes. Then, after you create fig/axes, you must reuse the same axes (say, ax1) for each plot. Here is the summary of important steps:

    1. Create figure/axes

      fig, ax1 = plt.subplots(figsize=(5, 3.5))

    2. Plot base map

      world.plot(ax=ax1)

    3. Plot a layer

      gdf1.plot(ax=ax1)

    4. Plot more layer

      gdf2.plot(ax=ax1)

    Hope this helps.