Search code examples
pythonmatplotlibseaborncatplot

How can I display kind="swarm" and kind="point" on the same catplot in Seaborn?


I am trying to display both the means and errors (kind="point") and individual data points (kind="swarm") overlayed on the same catplot in Seaborn.

I have the following code:

sns.catplot(x="Variable_A", y="Dependent_Variable", col="Variable_B", data=LRP_df, kind="swarm", color = "black")

sns.catplot(x="Variable_A", y="Dependent_Variable", col="Variable_B", data=LRP_df, kind="point", color = "red")

sns.despine()

which produces the plots separately:

result of calling catplot

How can I make the two plots sit on the same axes?

Thanks!


Solution

  • Using a face grid, you can overlay each one by specifying each one with map_dataframe(). The examples in this official reference have been modified. The data is based on sample data.

    import seaborn as sns
    
    sns.set_theme(style="ticks")
    exercise = sns.load_dataset("exercise")
    
    g = sns.FacetGrid(exercise, col="kind")
    g.map_dataframe(sns.stripplot, x="time", y="pulse", color="black")
    g.map_dataframe(sns.pointplot, x="time", y="pulse", color="red")
    g.set_axis_labels("time", "pulse")
    

    enter image description here