How can I combine a scatter plot with a density heatmap?

Question:

I have a series of scatterplots (one example below), but I want to modify it so that the colors of the points in the plot become more red (or "hot") when they are clustered more closely with other points, while points that are spread out further are colored more blue (or "cold"). Is it possible to do this?

scatterplot example

Currently, my code is pretty basic in its set up.

import plotly.express as px
    
fig = px.scatter(data, x='A', y='B', trendline='ols')
Asked By: Bob McBobson

||

Answers:

Using scipy.stats.gaussian_kde you can calculate the density and then use this to color the plot:

import numpy as np
import pandas as pd
import plotly.express as px
from scipy import stats

df = pd.DataFrame({
    'x':[0,0,1,1,2,2,2.25,2.5,2.5,3,3,4,2,4,8,2,2.75,3.5,2.5], 
    'y':[0,2,3,2,1,2,2.75,2.5,3,3,4,1,5,4,8,4,2.75,1.5,3.25]
})
values = np.vstack([df.x, df.y])
positions = np.vstack([df.x.ravel(), df.y.ravel()])
kernel = stats.gaussian_kde(values)
df['z'] = np.reshape(kernel(positions).T, df.x.shape)
fig = px.scatter(df, x='x', y='y', color='z', trendline='ols', color_continuous_scale=px.colors.sequential.Bluered)

output:
scatterplot

Answered By: Zach Flanders
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.