1

Using official NUTS polygon data from Eurostat (Year: NUTS2021, File format: geojson, Geometry type: Polygon (RG), Scale: (20M), Coordinate reference system: EPSG 3857 and geopandas, I create a map of European countries.

I would like to add bar plots to certain locations, mostly at the country centroid, or sometimes even at two or more locations per country. These coordinates are provided either by Eurostat (Year: NUTS2021, File format: geojson, Geometry type: Points (LB), Coordinate reference system: EPSG 3857 or other data sources, ie. here.

The problem is the only way to add bar plots to the map that I am aware of is to create another subplot via fig.add_axes(rect). Per definition, rect is a list [x0, y0, width, height] denoting the lower left point of the new subplot in figure coordinates (x0, y0) and its width and height.

Instead of using figure coordinates (x0, y0), what I would like to do is to use pre-defined coordinates, either using the same Coordinate Reference System (e.g. Germany has the NUTS POINT(1155338.417 6634297.661) or according to other formats (e.g., provided by the csv-file (10.451526, 51.165691)). How can I do that?

What have I tried so far:

I tried to normalize the coordinates according to using lon_eu_min, lon_eu_max, lat_eu_min, lat_eu_max but that did not work because the bar chart is positioned according to the coordinate of the figure, not of the map axes.

Manual placing works of course - see below with example.

import cartopy.crs as ccrs
import geopandas as gpd

# import and reduce country polygon data
nuts_path = '.\NUTS_RG_20M_2021_3857.geojson'
gdf = gpd.read_file(nuts_path)
gdf = gdf[gdf['LEVL_CODE'] == 0]
gdf = gdf[['CNTR_CODE', 'geometry']]
gdf = gdf.rename(columns={'CNTR_CODE': 'region'})
gdf = gpd.GeoDataFrame(gdf, geometry='geometry', crs=3857)

# import and reduce country point data
nuts_points_path = '.\NUTS_LB_2021_3857.geojson'
gdf_points = gpd.read_file(nuts_points_path)
gdf_points = gdf_points.query("LEVL_CODE == 0")
gdf_points = gdf_points[['CNTR_CODE', 'geometry']]
gdf_points = gdf_points.rename(columns={'CNTR_CODE': 'region'})

# plot
fig = plt.figure(figsize=(9, 10))
ax = plt.axes(projection=ccrs.epsg(3857))

# extent
lat_eu_min = 35
lat_eu_max = 71
lon_eu_min = -12
lon_eu_max = 34
ax.set_extent([lon_eu_min, lon_eu_max, lat_eu_min, lat_eu_max])

# plot countries
gdf.plot(
    ax=ax,
    edgecolor='black',
    facecolor='lightgrey',
)

# manual positioning
longitude, latitude = 0.46, 0.33

# add bar plot
bar_ax = fig.add_axes([longitude, latitude, 0.05, 0.05])
bar_ax.bar([1, 2, 3], [1, 2, 3], color=['C1', 'C2', 'C3'])
bar_ax.set_axis_off()

plt.tight_layout()
plt.show()

Desired outcome, but created with manual positioning: desired_outcome

2
  • I think you're going to want to create a matplotlob Rectangle object and use ax.add_patch
    – Paul H
    Mar 26, 2023 at 2:33
  • As you are a new contributor, and you have got an answer. Your next move is to review it, if it is good, you can click to accept it and earn some points.
    – swatchai
    Mar 29, 2023 at 5:47

1 Answer 1

0

Short answer
Lets define

    useproj = ccrs.epsg(3857)
    nonproj = ccrs.PlateCarree()

The transformation of (long,lat) to data coordinates (x,y) for plotting is

    xy = useproj.transform_point(lon1, lat1, nonproj)

Long answer
A demo code and the result is the answer. Read comments in the code for explanation.

from mpl_toolkits.axes_grid1.inset_locator import inset_axes
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.patches as mpatches
import cartopy.crs as ccrs

import os
os.environ['USE_PYGEOS'] = '0'
import geopandas as gpd

# function to create inset axes and plot bar chart on it
# this is good for 3 items bar chart
def build_bar(mapx, mapy, ax, width, xvals=['a','b','c'], yvals=[1,4,2], fcolors=['r','g','b']):
    ax_h = inset_axes(ax, width=width, \
                    height=width, \
                    loc=3, \
                    bbox_to_anchor=(mapx, mapy), \
                    bbox_transform=ax.transData, \
                    borderpad=0, \
                    axes_kwargs={'alpha': 0.35, 'visible': True})
    for x,y,c in zip(xvals, yvals, fcolors):
        ax_h.bar(x, y, label=str(x), fc=c)
    ax_h.axis('off')
    return ax_h

# Use world data for basemap
world_data = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
europe  = world_data[world_data.continent=='Europe']

# extent of plot
lat_eu_min = 35
lat_eu_max = 71
lon_eu_min = -12
lon_eu_max = 34

# some settings for demo data
mid_lon = (lon_eu_min+lon_eu_max)/2.0
hr_lon = (lon_eu_max-lon_eu_min)/2.0
mid_lat = (lat_eu_min+lat_eu_max)/2.0
hr_lat = (lat_eu_max-lat_eu_min)/2.0

# Create figure and axes for plotting
# Projections required are set uo here
fig = plt.figure(figsize=(6, 7.5))
nonproj = ccrs.PlateCarree()
useproj = ccrs.epsg(3857)
ax = plt.axes(projection=useproj)

# Set extent of our plot
ax.set_extent([lon_eu_min, lon_eu_max, lat_eu_min, lat_eu_max])
# Begin our plot with basemap
europe.plot(ax=ax, transform=ccrs.PlateCarree(), color='lightgray', ec="k", lw=0.3)

# Generate some demo data
# 1. locations
n = 15
lon1s = mid_lon + hr_lon*(np.random.random_sample(n)-0.5)
lat1s = mid_lat + hr_lat*(np.random.random_sample(n)-0.5)

# 2. list of 3-values data for the locations above
bar_data = np.random.randint(1,5,[n,3])  # list of 3 items lists

# 3. create a barchart at each location in (lon1s,lat1s)
# Some parameter settings
bar_width = 0.1  # inch
colors = ['r','g','b']
xlabels = ['a','b','c']

# 4. Set of (lon1s, lat1s, bar_data) are ready to plot as symbols (barchart)
# (lon1s, lat1s): in geo degrees, need transformation to data coordinates
for ix, lon1, lat1, bdata in zip(list(range(n)), lon1s, lat1s, bar_data):
    # Coordinates longitude, latitude in degrees here
    print(f"{lon1:8.3f},{lat1:8.3f}")
    #xyz = useproj.transform_points(nonproj, np.array([lon1,]),np.array([lat1,]))
    xyz = useproj.transform_point(lon1, lat1, nonproj)
    #bax = build_bar(xyz[0][0], xyz[0][1], \
    bax = build_bar(xyz[0], xyz[1], \
            ax, 0.2, \
            xvals = xlabels, \
            yvals = bdata, \
            fcolors = colors)

# create legend (of the 3 classes)
patch0 = mpatches.Patch(color=colors[0], label='Category A')
patch1 = mpatches.Patch(color=colors[1], label='Category B')
patch2 = mpatches.Patch(color=colors[2], label='Category C')
ax.legend(handles=[patch0,patch1,patch2], loc=2)

plt.show()

uk_plot1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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