2

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?

brb
  • 1,123
  • 17
  • 40

1 Answers1

3

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.

has2k1
  • 2,095
  • 18
  • 16
  • Thanks @has2k1, appreciate it and love your work. Could you include in the answer how you would set the interpolation method? I tried 'interpolate=True' as per geom_raster() and got an error that interpolate was not understood. This aspect is not covered in the documentation: https://plotnine.readthedocs.io/en/stable/generated/plotnine.stats.stat_density_2d.html – brb May 30 '22 at 10:37
  • 2
    e.g. `interpolation='gaussian'`, see the documentation for [geom_raster](https://plotnine.readthedocs.io/en/stable/generated/plotnine.geoms.geom_raster.html) – has2k1 May 30 '22 at 11:13