Search code examples
pythondata-analysisgeopandas

Use UK map instead of South America (as in website tutorial)


I have this code from the GeoPandas website:

# load data which contains long/lat of locations in England.
df = pd.read_csv(xxx.csv)

gdf = geopandas.GeoDataFrame(
    df, geometry=geopandas.points_from_xy(df.Longitude, df.Latitude))

print(gdf.head)

# plotting coordinates over a country level map
world = geopandas.read_file(geopandas.datasets.get_path("naturalearth_lowres"))

# then restrict this to the United Kingdom
ax = world[world.continent == 'South America'].plot(
    color='white', edgecolor='black')

# then plot the geodataframe on this
gdf.plot(ax=ax, color='red')
plt.show()

I want to change this to England or UK instead of South America, But I can't seem to find out how to replace continent with country or something similar etc.


Solution

  • If you view the dataframe called "world" you will notice it has a column called "continent" and a column called "name" - "name" holds the Country. Filtering the "continent" column to "Europe" you will see all the available Countries you can select from in the "name" column. Only "United Kingdom" is available, not "England".

    Try replacing your line of code here:

    # then restrict this to the United Kingdom
    ax = world[world.continent == 'South America'].plot(
        color='white', edgecolor='black')
    

    with this:

    # then restrict this to the United Kingdom
    ax = world[(world.name == "United Kingdom")].plot(
    color='white', edgecolor='black')