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?

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')
                Using scipy.stats.gaussian_kde you can calculate the density and then use this to color the plot:
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]
})
kernel = stats.gaussian_kde([df.x, df.y])
df['z'] = kernel([df.x, df.y])
fig = px.scatter(df, x='x', y='y', color='z', trendline='ols', color_continuous_scale=px.colors.sequential.Bluered)
output:

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