Reputation: 81
I am writing a code for a map of europe with python geopandas.
I am currently facing a problem with French Guiana. I don't want it to display in the map, however, I don't find a way to detach it from France.
Here is my code:
import geopandas as gpd
import pandas as pd
import matplotlib.pyplot as plt
europe = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
europe = europe[europe.continent == 'Europe']
#europe = europe[europe.name != 'France']
data = pd.read_csv('HICP_EU_bycountry_12_2022.csv', delimiter=';')
data = data[['Area', 'Rate']]
merged_data = europe.merge(data, left_on='name', right_on='Area', how='left')
fig, ax = plt.subplots(figsize=(10, 6))
merged_data.plot(column='Rate', cmap='Reds', linewidth=0.8, ax=ax, edgecolor='0.8', legend=True)
ax.set_title('Inflation Rates in Europe', fontsize=16)
ax.set_axis_off()
for idx, row in merged_data.iterrows():
rate = row['Rate']
if not pd.isna(rate):
ax.annotate(text=str(rate), xy=row['geometry'].centroid.coords[0], horizontalalignment='center', fontsize=8)
ax.set_facecolor('#f7f7f7')
plt.show()
Upvotes: 1
Views: 1408
Reputation: 1293
I have a simpler solution that also keeps La Corse in France.
France = list(world.loc[43, "geometry"].geoms)
FrGuiana = France[0]
FranceE = MultiPolygon([France[1], France[2]])
world.loc[world['name'] == 'France', 'geometry'] = FranceE
The first part splits the French multipolygon to a list of Polygons. The first one is for French Guiana and the rest for the European parts of France. Then you can reassign the new European France multipolygon to the original database.
Upvotes: 0
Reputation: 21
Alternatively, you can mess around with the MultiPolygon of France including Guiana and Corse. I extract the points from the polygon for France and make a new polygon to replace the multipolygon in the geo panda.
import geopandas as gpd
import matplotlib.pyplot as plt
from shapely.geometry import Polygon, MultiPolygon
# Load Europe map data from the geopandas library
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
europe = world[world.continent == 'Europe']
# Exclude French Guiana from the map (also Corsika though),
tmp = [x.replace(')','') for x in str(europe.loc[43,'geometry']).split('((')[1:]][1]
tmp2 = [x.split(' ') for x in tmp.split(', ')][:-1]
tmp3 = [(float(x[0]),float(x[1])) for x in tmp2]
France_mainland = Polygon(tmp3)
europe.loc[europe['name']=='France','geometry'] = France_mainland
Upvotes: 2
Reputation: 53
As there is no separation between territories in this dataset, we can cut the polygons to your desired size using a bounding box (bbox) as a clip.
This is relatively simple, but we will need a small function and the Polygon
class from Shapely to convert a bbox to a shape. In order to achieve this, we need to complete the following steps:
europe
.From https://stackoverflow.com/a/68741143/18253502, we can convert bbox coordinates to a shapely Polygon. Bbox coordinates can be made at http://bboxfinder.com.
import geopandas as gpd
import pandas as pd
from shapely.geometry import Polygon
import matplotlib.pyplot as plt
# STEP 1 #
# Make polygon from bbox coordinates https://stackoverflow.com/a/68741143/18253502
def make_bbox(long0, lat0, long1, lat1):
return Polygon([[long0, lat0],
[long1,lat0],
[long1,lat1],
[long0, lat1]])
# Coords covering Europe & Russia made with http://bboxfinder.com
bbox = make_bbox(-36.210938,28.304381,197.226563,81.361287)
## Alternatively, can clip to more standard European extent
## with Central/Eastern Russia excluded
# bbox = make_bbox(-36.386719,29.228890,60.292969,74.543330)
# STEP 2 #
# Convert to gdf
bbox_gdf = gpd.GeoDataFrame(index=[0], crs='epsg:4326', geometry = [bbox])
# STEP 3 #
# Load europe
europe = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
europe = europe[europe.continent == 'Europe']
# Use bbox as clipping border for Europe
europe = europe.overlay(bbox_gdf, how="intersection")
Now europe
has been clipped to the bbox extent:
# plot result
fig, ax = plt.subplots(figsize=(15,10))
europe.plot(linewidth=0.8, ax=ax, edgecolor='0.8', legend=True)
ax.set_title('Clipped Extent', fontsize=16)
ax.set_axis_off()
ax.set_facecolor('#f7f7f7')
plt.show()
Note that the Eastern tip of Russia is missing, as it wraps around to the other Longitude (see https://stackoverflow.com/a/71408567/18253502 for more details).
Upvotes: 0