Search code examples
pythonmatplotlibseabornkernel-density

is it possible to use a non gaussian kernel for the two lateral distributions in seaborn jointplot


My data look like:

s1 = sns.jointplot(data.columns[i], 
data.columns[j], 
data=data, 
space=0, color="b", stat_func=None)

enter image description here

if I use kde instead

s1 = sns.jointplot(data.columns[i], 
    data.columns[j], 
    data=data, kind = 'kde',
    space=0, color="b", stat_func=None)

I am quite happy with the two dimensional kde interpolation, less with the lateral one. The problem is both placed so close together actually suggest the maximum of the distribution lying at two different points which might be quite misleading.

enter image description here

So now the actual question: is it possible to specify something different from gaussian as a kernel (blue) for the two lateral distributions? (I know that gaussian is thw only option in 2D). Because for example 'biw' (green) might esthetically look better (I am still not convinced that it is mathematically speaking a good thing to place the interpolations done with the different kernel close together making them seem the same thing...). So my question is whether I can specify the different kernel somewhere in sns.jointplot or is the only way to overwrite the lateral distribution by anotherone calculated in a second moment.

ax1 = sns.distplot(data[data.columns[j]])
sns.kdeplot(data[data.columns[j]], kernel= 'biw', ax = ax1)

enter image description here


Solution

  • You can set a different kernel for the marginal plots:

    s1 = sns.jointplot(data.columns[i], 
                       data.columns[j], 
                       data=data, kind = 'kde',
                       space=0, color="b", stat_func=None,
                       marginal_kws={"kernel":"biw"})  # like this
    

    or, if you want to change just one marginal plot, you can replot on them:

    s1.ax_marg_y.cla() # clear axis
    sns.kdeplot(data.y, ax=s1.ax_marg_y, # choose the ax
                kernel="biw", # choose your kernel
                legend=0, # remove the legend
                vertical=True) # swap axis
    

    vertical=True allows you to switch x and y axis, ie not needed if you change the top-margin plot.