geom_pointdensity()¶import numpy as np
import pandas as pd
from lets_plot import *
from lets_plot.geo_data import *
from lets_plot.bistro import *
LetsPlot.setup_html()
sample_size = 1000
df_full = pd.read_csv("https://raw.githubusercontent.com/JetBrains/lets-plot-docs/refs/heads/master/data/diamonds.csv")
print(df_full.shape)
df = df_full.sample(sample_size, random_state=42).reset_index(drop=True)
df.head()
ggplot(df_full, aes("carat", "price")) + geom_pointdensity()
def get_mn_data(cov=[[1, 0], [0, 1]], size=250, seed=42):
np.random.seed(seed)
mean=[0, 0]
x, y = np.random.multivariate_normal(mean, cov, size).T
return {'x': x, 'y': y}
def get_nu_data(size=250, seed=42):
np.random.seed(seed)
return {
'x': np.random.uniform(size=size),
'y': np.random.normal(size=size),
}
def get_nu_data(size=250, seed=42):
np.random.seed(seed)
return {
'x': np.random.uniform(size=size),
'y': np.random.normal(size=size),
}
def get_np_data(scale, size=250, seed=42):
np.random.seed(seed)
return {
'x': np.random.poisson(size=size) + np.random.normal(scale=scale, size=size),
'y': np.random.normal(size=size),
}
def get_pointdensity_plot(data, method):
return ggplot(data, aes("x", "y")) + geom_pointdensity(method=method) + ggtitle("Method: {0}".format(method))
gggrid([
get_pointdensity_plot(get_mn_data(), 'neighbours'),
get_pointdensity_plot(get_mn_data(), 'kde2d'),
get_pointdensity_plot(get_mn_data(cov=[[1, 0.9], [0.9, 1]]), 'neighbours'),
get_pointdensity_plot(get_mn_data(cov=[[1, 0.9], [0.9, 1]]), 'kde2d'),
get_pointdensity_plot(get_mn_data(cov=[[1, -0.75], [-0.75, 1]]), 'neighbours'),
get_pointdensity_plot(get_mn_data(cov=[[1, -0.75], [-0.75, 1]]), 'kde2d'),
get_pointdensity_plot(get_nu_data(), 'neighbours'),
get_pointdensity_plot(get_nu_data(), 'kde2d'),
get_pointdensity_plot(get_np_data(.1), 'neighbours'),
get_pointdensity_plot(get_np_data(.1), 'kde2d'),
], ncol=2)
weight¶weighted_data = {
'x': [0, 0, 1],
'y': [0, 1, 0],
'w': [1, 2, 1],
}
tooltips_with_weights = layer_tooltips().line("@|@..density..")\
.line("'neighbours count'|@..count..")\
.line("weight|@w")
gggrid([
ggplot(weighted_data, aes('x', 'y')) + \
geom_pointdensity(adjust=150, tooltips=tooltips_with_weights) + \
ggtitle("method='neighbors', without weights"),
ggplot(weighted_data, aes('x', 'y')) + \
geom_pointdensity(aes(weight='w'), adjust=150, tooltips=tooltips_with_weights) + \
ggtitle("method='neighbors', with weights"),
ggplot(weighted_data, aes('x', 'y')) + \
geom_pointdensity(method='kde2d', tooltips=tooltips_with_weights) + \
ggtitle("method='kde2d', without weights"),
ggplot(weighted_data, aes('x', 'y')) + \
geom_pointdensity(aes(weight='w'), method='kde2d', tooltips=tooltips_with_weights) + \
ggtitle("method='kde2d', with weights"),
], ncol=2)
ggplot(df, aes("carat", "price")) + \
geom_pointdensity(aes(alpha="..density.."), color="black") + \
ggtitle("Custom color and alpha")
ggplot(df, aes("carat", "price")) + \
geom_pointdensity(aes(fill="..density.."), method='kde2d', color="black", shape=21) + \
ggtitle("Custom shape, color and fill")
ggplot(df, aes("carat", "price")) + \
geom_pointdensity(method='kde2d', shape=17, angle=90) + \
ggtitle("Custom shape and angle")
ggplot(df, aes("carat", "price")) + \
geom_pointdensity(aes(size="depth"), method='kde2d', alpha=.2) + \
scale_size(range=[1, 5]) + \
ggtitle("Custom size and alpha")
ggplot(df, aes("carat", "price")) + \
geom_pointdensity(method='kde2d', shape=1, stroke=2, alpha=.5) + \
ggtitle("Custom shape, stroke and alpha")
gggrid([
ggplot(df, aes("carat", "price")) + geom_pointdensity() + ggtitle("Default method (auto)"),
ggplot(df, aes("carat", "price")) + geom_pointdensity(method='neighbours') + ggtitle("method='neighbours'"),
ggplot(df, aes("carat", "price")) + geom_pointdensity(method='kde2d') + ggtitle("method='kde2d'"),
], ncol=3)
If method=‘auto’ (default), the selection is determined by the size of the dataset.
Note: If grouping is used, the method is selected independently for each group:
ideal_df = df_full.assign(is_ideal_quality=(df_full["cut"] == "Ideal").map({True: "Quality: ideal", False: "Quality: not ideal"}))
ggplot(ideal_df, aes("carat", "price")) + \
geom_pointdensity() + \
facet_grid(x="is_ideal_quality")