'How to mantain aspect ratios after overlay in geopandas?
I'm trying to overlay some cities on top of a map.
import pandas as pd
import geopandas as gdp
import matplotlib.pyplot as plt
from shapely.geometry import box
destinations = ["Oslo", "Bergen", "Trondheim", "Tromsø", "Nordkapp", "Bodø", "Lofoten"]
world = gdp.read_file(gdp.datasets.get_path('naturalearth_lowres'))
norway = world[world.name=="Norway"]
norway =gdp.clip(norway, box(0, 0, 60,75)) # removing Svalbard
cities = world = gdp.read_file(gdp.datasets.get_path('naturalearth_cities'))
df = pd.read_csv("norway_cities.csv", sep=";")
norway_cities = gdp.GeoDataFrame(df, geometry=gdp.points_from_xy(df.lng, df.lat))
ax = norway.plot(color='white', edgecolor='black')
ax = cities[cities.name=="Stockholm"].plot(ax=ax, color='red')
ax = norway_cities[norway_cities.city.isin(destinations)].plot(ax=ax, color='blue')
plt.show()
Proportions are mantained when I plot Stockholm coordinates:
Things get distorted when I plot the rest:
Data are coming from different sources but the scale seems to be the same:
Changing the figure size does not affect the results, how do I keep the same aspect ratio of the first picture?
Solution 1:[1]
When constructing norway_cities
you have not defined the CRS.
norway_cities = gdp.GeoDataFrame(
df, geometry=gdp.points_from_xy(df.lng, df.lat), crs="epsg:4386"
)
full code
- get cities from a web page as I do not have access to your CSV
import pandas as pd
import geopandas as gdp
import matplotlib.pyplot as plt
from shapely.geometry import box
import requests, io
destinations = ["Oslo", "Bergen", "Trondheim", "Tromsø", "Nordkapp", "Bodø", "Lofoten"]
world = gdp.read_file(gdp.datasets.get_path("naturalearth_lowres"))
norway = world[world.name == "Norway"]
norway = gdp.clip(norway, box(0, 0, 60, 75)) # removing Svalbard
cities = world = gdp.read_file(gdp.datasets.get_path("naturalearth_cities"))
# df = pd.read_csv("norway_cities.csv", sep=";")
df = pd.read_csv(
io.StringIO(
requests.get("https://simplemaps.com/static/data/country-cities/no/no.csv").text
)
)
norway_cities = gdp.GeoDataFrame(
df, geometry=gdp.points_from_xy(df.lng, df.lat), crs="epsg:4386"
)
ax = norway.plot(color="white", edgecolor="black")
ax = cities[cities.name == "Stockholm"].plot(ax=ax, color="red")
ax = norway_cities[norway_cities.city.isin(destinations)].plot(ax=ax, color="blue")
plt.show()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Rob Raymond |