Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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')
like image 894
Bob McBobson Avatar asked Oct 31 '25 08:10

Bob McBobson


1 Answers

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: scatterplot

like image 103
Zach Flanders Avatar answered Nov 02 '25 23:11

Zach Flanders