pythonseabornviolin-plotstripplot

Shifting strip plot next to half violin plot in seaborn


I'm trying to shift the points to the left of the violin plot. How can I achieve this?

Here is an example code:

import seaborn as sns
ax = sns.violinplot(np.random.random(100), inner=None,
                    bw_method=.1,fill=True,)
sns.stripplot(np.random.random(100),
              edgecolor='white', linewidth=0.5, palette=['lightblue'] * 3, ax=ax)

enter image description here


Solution

  • You can provide an array of repeated x-values to position both.

    import matplotlib.pyplot as plt
    import seaborn as sns
    import numpy as np
    
    data = np.random.randn(200).cumsum()
    ax = sns.violinplot(y=data, x=np.full_like(data, 1.5), inner=None, orient='v',
                        bw_method=.1, fill=True)
    sns.stripplot(y=data, x=np.full_like(data, 1), jitter=True, orient='v',
                  edgecolor='white', linewidth=0.5, color='lightblue', ax=ax)
    ax.set_xticks([])
    plt.show()
    

    stripplot next to violinplot

    PS: The jitter= parameter of sns.stripplot can also be a value to change the width, e.g jitter=0.3.