Geopandas 无法绘制正确的国家
Geopandas can't plot the right country
我想在法国地图上绘制一次旅行,但我不能只绘制法国地图(见图片),有人可以帮助我吗?
我相信这可能是上下文库中的 crs 的问题,但我没有在网上找到任何信息
这是一个可重现的例子...
import pandas as pd
import geopandas as gpd
from shapely.geometry import Point
import shapely
import matplotlib.pyplot as plt
from matplotlib.colors import to_hex
import seaborn as sns
import contextily as ctx
def main():
gdf = pd.DataFrame()
gdf["longitudes"] = [5.8127891, 5.2250324]
gdf["latitudes"] = [46.1965678, 46.2051192]
gdf["geometry"] = gpd.points_from_xy(gdf["longitudes"], gdf["latitudes"])
gdf = gpd.GeoDataFrame(gdf, crs= {"init": "epsg:4326"})
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
france = world.query('name == "France"')
# Make the plot
fig, ax = plt.subplots(figsize=(4,5))
#ax = gdf.plot(figsize=(10, 5))
# Plot the state outlines
france.boundary.plot(color='black', linewidth=0.5, ax=ax)
# Modify projection to match what contextily uses
gdf = gdf.to_crs(epsg=3857)
part1 = shapely.geometry.LineString(
gdf['geometry'].values)
linegdf = gpd.GeoDataFrame(
{'geometry': [part1]}
)
c = to_hex(sns.light_palette('green')[0])
linegdf.plot(
color=c,
linewidth=3,
ax=ax
)
print(len(gdf))
# Plot points colored by day
gdf.plot(
cmap=sns.light_palette('green', as_cmap=True),
ax=ax,
markersize=50,
edgecolor='black', linewidth=0.5,
zorder=1000 # force the points to be the top layer of the plot
)
# Add basemap
ctx.add_basemap(ax=ax)
# Remove axes
ax.set_axis_off()
plt.show()
main()
非常感谢任何帮助!
两分
- 法国是一个多边形,其中一部分在南极洲。已将此排除在几何
之外
- 需要与 CRS 非常一致。另外最好不要使用遗留的引用 CRS
{"init": "epsg:4326"}
import matplotlib.pyplot as plt
from matplotlib.colors import to_hex
import seaborn as sns
import pandas as pd
import geopandas as gpd
import shapely.geometry
import contextily as ctx
def main():
gdf = pd.DataFrame()
gdf["longitudes"] = [5.8127891, 5.2250324]
gdf["latitudes"] = [46.1965678, 46.2051192]
gdf["geometry"] = gpd.points_from_xy(gdf["longitudes"], gdf["latitudes"])
# lets project CRS early...
gdf = gpd.GeoDataFrame(gdf, crs="epsg:4326").to_crs("EPSG:3857")
world = gpd.read_file(gpd.datasets.get_path("naturalearth_lowres"))
france = world.query('name == "France"')
# just mainland france, not antartica... plus CRS
france = (
france["geometry"]
.apply(
lambda mp: shapely.geometry.MultiPolygon(
[p for p in mp.geoms if p.bounds[1] > 20]
)
)
.to_crs("EPSG:3857")
)
# Make the plot
fig, ax = plt.subplots(figsize=(4, 5))
# Plot the state outlines
ax = france.boundary.plot(color="black", linewidth=0.5, ax=ax)
part1 = shapely.geometry.LineString(gdf["geometry"].values)
linegdf = gpd.GeoDataFrame({"geometry": [part1]})
c = to_hex(sns.light_palette("green")[0])
linegdf.plot(color=c, linewidth=3, ax=ax)
# Plot points colored by day
gdf.plot(
cmap=sns.light_palette("green", as_cmap=True),
ax=ax,
markersize=50,
edgecolor="black",
linewidth=0.5,
zorder=1000, # force the points to be the top layer of the plot
)
# Add basemap
ctx.add_basemap(ax=ax)
# Remove axes
ax.set_axis_off()
plt.show()
main()
我想在法国地图上绘制一次旅行,但我不能只绘制法国地图(见图片),有人可以帮助我吗? 我相信这可能是上下文库中的 crs 的问题,但我没有在网上找到任何信息 这是一个可重现的例子...
import pandas as pd
import geopandas as gpd
from shapely.geometry import Point
import shapely
import matplotlib.pyplot as plt
from matplotlib.colors import to_hex
import seaborn as sns
import contextily as ctx
def main():
gdf = pd.DataFrame()
gdf["longitudes"] = [5.8127891, 5.2250324]
gdf["latitudes"] = [46.1965678, 46.2051192]
gdf["geometry"] = gpd.points_from_xy(gdf["longitudes"], gdf["latitudes"])
gdf = gpd.GeoDataFrame(gdf, crs= {"init": "epsg:4326"})
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
france = world.query('name == "France"')
# Make the plot
fig, ax = plt.subplots(figsize=(4,5))
#ax = gdf.plot(figsize=(10, 5))
# Plot the state outlines
france.boundary.plot(color='black', linewidth=0.5, ax=ax)
# Modify projection to match what contextily uses
gdf = gdf.to_crs(epsg=3857)
part1 = shapely.geometry.LineString(
gdf['geometry'].values)
linegdf = gpd.GeoDataFrame(
{'geometry': [part1]}
)
c = to_hex(sns.light_palette('green')[0])
linegdf.plot(
color=c,
linewidth=3,
ax=ax
)
print(len(gdf))
# Plot points colored by day
gdf.plot(
cmap=sns.light_palette('green', as_cmap=True),
ax=ax,
markersize=50,
edgecolor='black', linewidth=0.5,
zorder=1000 # force the points to be the top layer of the plot
)
# Add basemap
ctx.add_basemap(ax=ax)
# Remove axes
ax.set_axis_off()
plt.show()
main()
非常感谢任何帮助!
两分
- 法国是一个多边形,其中一部分在南极洲。已将此排除在几何 之外
- 需要与 CRS 非常一致。另外最好不要使用遗留的引用 CRS
{"init": "epsg:4326"}
import matplotlib.pyplot as plt
from matplotlib.colors import to_hex
import seaborn as sns
import pandas as pd
import geopandas as gpd
import shapely.geometry
import contextily as ctx
def main():
gdf = pd.DataFrame()
gdf["longitudes"] = [5.8127891, 5.2250324]
gdf["latitudes"] = [46.1965678, 46.2051192]
gdf["geometry"] = gpd.points_from_xy(gdf["longitudes"], gdf["latitudes"])
# lets project CRS early...
gdf = gpd.GeoDataFrame(gdf, crs="epsg:4326").to_crs("EPSG:3857")
world = gpd.read_file(gpd.datasets.get_path("naturalearth_lowres"))
france = world.query('name == "France"')
# just mainland france, not antartica... plus CRS
france = (
france["geometry"]
.apply(
lambda mp: shapely.geometry.MultiPolygon(
[p for p in mp.geoms if p.bounds[1] > 20]
)
)
.to_crs("EPSG:3857")
)
# Make the plot
fig, ax = plt.subplots(figsize=(4, 5))
# Plot the state outlines
ax = france.boundary.plot(color="black", linewidth=0.5, ax=ax)
part1 = shapely.geometry.LineString(gdf["geometry"].values)
linegdf = gpd.GeoDataFrame({"geometry": [part1]})
c = to_hex(sns.light_palette("green")[0])
linegdf.plot(color=c, linewidth=3, ax=ax)
# Plot points colored by day
gdf.plot(
cmap=sns.light_palette("green", as_cmap=True),
ax=ax,
markersize=50,
edgecolor="black",
linewidth=0.5,
zorder=1000, # force the points to be the top layer of the plot
)
# Add basemap
ctx.add_basemap(ax=ax)
# Remove axes
ax.set_axis_off()
plt.show()
main()