2

I am new to geopandas. I am trying to plot the locations of bays and gulfs. I have a csv file containing their names. After geocoding and finding their locations, I tried to plot the points on a world map.

import geopandas as gpd
import pandas as pd
import numpy as np
from geopandas.tools import geocode
data=pd.read_csv("Bays and Gulfs of the World.csv")
def geocoder(row):
    try:
        point = geocode(row, provider='nominatim',user_agent="my_application").geometry.iloc[0]
        return pd.Series({'Latitude': point.y, 'Longitude': point.x, 'geometry': point})
    except:
        return None

data[['Latitude', 'Longitude', 'geometry']] = data.apply(lambda x: geocoder(x['Gulf/Bay']), axis=1)
map_bays_gulf=gpd.GeoDataFrame(data,geometry=data.geometry)
world_map= gpd.read_file("Worldshpfile")
world_map.crs = {'init': 'epsg:4326'}
map_bays_gulf.crs = {'init': 'epsg:4326'}
world_map.plot(ax=world_map)
map_bays_gulf.plot()

When I don't enter the ax parameter's value, everything goes fine. But after giving ax, the following error shows up. Can't figure out the reason.

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-24-b8b5e5eee861> in <module>
      3 world_map.crs = {'init': 'epsg:4326'}
      4 map_bays_gulf.crs = {'init': 'epsg:4326'}
----> 5 world_map.plot(ax=world_map)
      6 map_bays_gulf.plot()


AttributeError: 'GeoDataFrame' object has no attribute 'set_aspect'

1 Answer 1

3

To make a map with more than one layers as is your case (the base worl_map layer and the overlay map_bays_gulf) there are at least two posible options as it's explained in the documentation of GeoPandas.

  • Assign the output of the plot method to a variable and use it as the ax parameter of the second call to plot
  • Create a Axes object calling plt.subplots(), and using it as the ax parameter for both calls to plot

So this will work:

base = world_map.plot(color='white', edgecolor='black')
map_bays_gulf.plot(ax=base, marker='o', color='red', markersize=5)

And this should also work:

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.set_aspect('equal')

world_map.plot(ax=ax, color='white', edgecolor='black')
map_bays_gulf.plot(ax=ax, marker='o', color='red', markersize=5)

plt.show();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.