Is there any way that I can plot a heatmap in seaborn based on a scatter plot? For instance, I have the following scatter plot and want to plot the corresponding heatmap in such a way that darker spots in the heatmap shows more crowded areas in the scatter plot.

sns.histplot(x=x_data, y=y_data) would create a 2d histogram of the given data. sns.kdeplot(x=x_data, y=y_data) would average out the values, creating an approximation of a 2D probability density function.
Here is a comparison between the 3 plots, using the iris dataset.
import matplotlib.pyplot as plt
import seaborn as sns
fig, (ax1, ax2, ax3) = plt.subplots(ncols=3, figsize=(15, 4), sharex=True, sharey=True)
iris = sns.load_dataset('iris')
sns.set_style('darkgrid')
sns.scatterplot(x=iris['sepal_length'], y=iris['sepal_width'], ax=ax1)
sns.histplot(x=iris['sepal_length'], y=iris['sepal_width'], ax=ax2)
sns.kdeplot(x=iris['sepal_length'], y=iris['sepal_width'], fill=True, ax=ax3)
ax1.set_title('scatterplot')
ax2.set_title('histplot')
ax3.set_title('kdeplot')
plt.tight_layout()
plt.show()

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With