Search code examples
pythonseabornjointplot

Set edgecolor on seaborn jointplot


I am able to set edgecolors for a seaborn histogram by passing in a hist_kws argument:

sns.distplot(ad_data["Age"], kde = False, bins = 35, hist_kws = {"ec":"black"})

However, I'm unable to similarly set edgecolors for the histograms in a seaborn jointplot. It doesn't accept a hist_kws argument or any other similar argument to set edgecolors. I'm unable to find anything in the document that addresses this. Any help would be appreciated.

For reference, I'm using seaborn 0.9 and matplotlib 3.1.


Solution

  • You need a 'hist_kws' inside the 'marginal_kws':

    import seaborn as sns
    import matplotlib.pyplot as plt
    import numpy as np
    
    x = np.random.normal(np.repeat([2, 8, 7, 10], 1000), 1)
    y = np.random.normal(np.repeat([7, 2, 9, 4], 1000), 1)
    
    g = sns.jointplot(x=x, y=y, color='purple', alpha=0.1,
                      marginal_kws={'color': 'tomato', 'hist_kws': {'edgecolor': 'black'}})
    plt.show()
    

    example plot

    In this case, jointplot sends the marginal_kws to distplot which in its turn sends the hist_kws to matplotlib's hist.

    Similarly, you can also set the parameters of a kde for the distplot:

    g = sns.jointplot(x=x, y=y, kind='hex', color='indigo', 
                      marginal_kws={'color': 'purple', 'kde': True,
                                    'kde_kws': {'color': 'crimson', 'lw': 1},
                                    'hist_kws': {'ec': 'black', 'lw': 2}})