pythonmatplotlibdata-visualizationseabornswarmplot

Plot another point on top of swarmplot


I want to plot a "highlighted" point on top of swarmplot like this

enter image description here

The swarmplot don't have the y-axis, so I have no idea how to plot that point.

import seaborn as sns
sns.set(style="whitegrid")
tips = sns.load_dataset("tips")
ax = sns.swarmplot(x=tips["total_bill"])

Solution

  • This approach is predicated on knowing the index of the data point you wish to highlight, but it should work - although if you have multiple swarmplots on a single Axes instance it will become slightly more complex.

    import matplotlib.pyplot as plt
    import matplotlib
    import seaborn as sns
    sns.set(style="whitegrid")
    tips = sns.load_dataset("tips")
    ax = sns.swarmplot(x=tips["total_bill"])
    artists = ax.get_children()
    offsets = []
    for a in artists:
        if type(a) is matplotlib.collections.PathCollection:
            offsets = a.get_offsets()
            break
    plt.scatter(offsets[50,0], offsets[50,1], marker='o', color='orange', zorder=10)
    

    enter image description here