'How can i stack a seaborn kdeplot over a real map?

I've generated a seaborn kdeplot using latitude and longitude of a crime dataset of Chicago and I want to stack it over a real map of the city, which python library is the best to use?



Solution 1:[1]

I am not sure whether the question was about a map of the city that needs to be interacted with but since you mentioned seaborn kdeplot, I guessed not. This answer only works for a static image.

Having said that, I would save the kdeplot with the same dimension as your image, remove all borders and save it as transparent. Then use pillow to merge

from PIL import Image
import seaborn as sns

# save kdeplot
my_plot = sns.kdeplot(...)

my_plot.set_xlim(0, map_s["width"])
my_plot.set_ylim(0, map_s["height"])
my_plot.set(yticklabels=[])
my_plot.set(xticklabels=[])
my_plot.set(xlabel=None)
my_plot.set(ylabel=None)

plt.gca().set_position([0, 0, 1, 1])
plt.box(False)
plt.savefig(filename, transparent=True, dpi=300) # higher resolution

# merge
image1 = Image.open("my_image.jpg")
image2 = Image.open(filename)
image1.paste(image2)
image1.save('new_image.jpg', quality=95)

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 David Bensoussan