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.
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:
Is it possible to do this in plotnine
using the data as is, or do I need to do additional data transformations etc?