2D density plot with plotnine (stat_density_2d)

Viewed 80

I am trying to create a 2D density plot using python's plotnine along the lines of the last example here: https://r-graph-gallery.com/2d-density-plot-with-ggplot2.html#:~:text=A%202d%20density%20plot%20is,of%20points%20in%20this%20fragment.

It should look roughly like the following (constructed using geom_bin2d), but smoother.

enter image description here

I have tired:

import pandas as pd
import numpy as np
from plotnine import *

df = pd.DataFrame({
    'x': np.random.normal(1,1,10000),
    'y': np.random.normal(3,2,10000),
})

p = (ggplot(df, aes('x','y'))
  + theme_light()
  + stat_density_2d(aes(fill='..level..'), geom='raster', contour=False)
  + labs(x=None,y=None)
)
p

But end up with just a yellow blob:

enter image description here

Is it possible to do this in plotnine using the data as is, or do I need to do additional data transformations etc?

1 Answers

You should map the fill to the density. The level is demarcated by lines while the density by regions.

(
...
+ stat_density_2d(aes(fill=after_stat('density')), geom='raster', contour=False)
)

Then set the interpolation method to smoothen it.

Related