I am currently working on creating a Kernel density estimation (Seaborn's Kdeplot) visualization of point cloud data between two countries. However, I have encountered a problem for which I need some assistance. Initially, I create a Kdeplot and then save the kde levels as polygons, which are subsequently stored in a geopackage file (.gpkg). For some reason, at certain points, some of the Kdes appear elongated either vertically or horizontally, and I require help in resolving this issue. Here is an example showcasing the mobilities between Spain and Portugal, illustrating the problem. The crs is consistently set to 3035 throughout the program.
Below is a picture displaying the Seaborn Kdeplots for Portugal and Spain separately, both of which appear fine and are not elongated.
Portugal to the left & Spain to the right:
However, when I add a contextily basemap with the CRS set to 3035, the x-axis shrinks and appears as follows:
Portugal to the left & Spain to the right:
When I save the KDE levels as polygons in a .gpkg file, the resulting polygons look exactly like those shown above in QGIS – elongated.
Using the same point cloud data utilized in Seaborn's Kdeplot, I have created a KDE/heatmap in QGIS, and the result appears as shown below. Unlike the previously mentioned plots, these ones do not display any elongation.
Portugal to the left & Spain to the right:
Does anyone know what is causing the elongation issue?
Is it occurring during the creation of the Seaborn's Kdeplot or when saving the Kdeplot's levels as polygons?
The problem I'm facing is that some of the Kdeplots I have generated become elongated immediately upon creating the Seaborn Kdeplot. It appears that as soon as the Sns Kdeplot is provided with context, such as a background map, it sometimes results in the shrinking of either the x or y axis. The only correlation I have observed is that when the country's area is narrow or wide, the Kdeplot also becomes elongated.
How fo I resolve this issue and prevent the distortion of the Kdeplots?
This is how the sns kdeplot code looks like where “country” is a geodataframe of the point cloud data.
def kde_plot(self, country, bw):
levels = [0.05, 0.10, 0.15, 0.20, 0.25, 0.30, 0.35, 0.40, 0.45, 0.50, 0.55, 0.60, 0.65, 0.70, 0.75, 0.80, 0.85, 0.90, 0.95, 1]
self.kde = sns.kdeplot(
x = country.geometry.x,
y = country.geometry.y,
cmap = 'viridis',
fill = True,
alpha = 0.5,
bw_adjust = bw,
levels = levels,
)
plt.show()
return self.kde