matplotlibseabornswarmplot

Swarmplot, each point with its own errorbar


Can I have some help with seaborn swarmplot? Each point on the swarmplot represents mean of one group. Separately, I have also calculated standard deviation of each group. Is there a way to plot errorbar of one standard deviation on each data point in the swarmplot?

I tried seaborn swarmplot. But cannot add errorbar. I also tried scatter plot with errorbar, but multiple points with same X-value overlapped too much.

Example:

X = np.arange(0,10) 
Y = np.random.normal(5,0.0003,(10,10)) 
E = np.random.normal(0.0001,0.00002,(10,10)) 
for i in range(Y.shape[0]): 
     plt.errorbar(X,Y[i], yerr = E[i],ls='none',marker = 'o') 
plt.show()

Solution

  • Seaborn only draws error bars if you let it calculate the errors from the original data. You can mimic seaborn's approach by manually adding a delta to simulate seaborn's dodging.

    Here is some example code to show how it could work. I changed the dimensions to be different from each other, to make sure the example code doesn't mix them up.

    import matplotlib.pyplot as plt
    import numpy as np
    
    X = np.arange(0, 11)
    Y = np.random.normal(5, 0.0003, (10, 11))
    E = np.random.normal(0.0001, 0.00002, (10, 11))
    plt.figure(figsize=(12, 5))
    for Yi, Ei, delta in zip(Y, E, np.linspace(-0.4, 0.4, Y.shape[0])):
        plt.errorbar(X + delta, Yi, yerr=Ei, ls='none', marker='o')
    for Xi in X[:-1]:  # draw a vertical line between each group
        plt.axvline(Xi + 0.5, ls=':', lw=0.5, color='grey')
    plt.xticks(X)
    plt.show()
    

    simulating seaborn's dodging, points with error bars