Riccardo
Riccardo

Reputation: 97

Query on errorbar() added to stripplot

I've been struggling to get desired errorbars for my categorical variable (x=Type) with two distinct marker shapes (per categories A and B) and errorbar colors matching the color of the respective marker color (that is palette=dict(A= "k", B= "r"))

Here's my data:

Type         Y              Err
A             3           0.2
A             3.1         0.8
A             4.3         0.6
B             5.9         1.1
B             5.1         0.5

I searched this forum thoroughly to get errorbars and here's what I've got so far.

Subplot1 = sns.stripplot(y=DataFrame["Y"], x=DataFrame["Type"], marker='s',
                         palette=dict(A= "k", B="r")`

# Add errorbars to each data point 
for PointPair in Subplot1.collections:
     for x, y in PointPair.get_offsets():
         x_coords.append(x)
         y_coords.append(y)

Subplot1.errorbar(x_coords, y_coords, yerr=DataFrame['Err'], 
                     fmt=' ', 
                     elinewidth=1, ecolor='k', 
                     capsize=3, markeredgewidth=0.81, 
                      )

So in plain English, I've been attempting to tweak ecolor='k' as well as marker='s' to gain individual errorbar color and markershapes for Categories A and B. But yet the following doesn't seem to be working: ecolor=['k', 'r'] marker=['s', 'o']

If you could please shed some light on this. Many thanks in advance!

Upvotes: 1

Views: 238

Answers (1)

Diziet Asahi
Diziet Asahi

Reputation: 40667

Unfortunately, seaborn is very difficult to work with if you are trying to move away from the plots that are offered.

In your case, I think it would be much easier to forgo seaborn altogether and to generate a plot using standard matplotlib functions:

markers = ['s','o']
colors = ['k', 'r']
grouped = df.groupby('Type')

fig, ax = plt.subplots()
for i,((g,d),m,c) in enumerate(zip(grouped,markers,colors)):
    # generate scattered x values, adjust scale= as needed
    x = np.random.normal(loc=i,scale=0.05,size=(len(d['Y'],))) 
    ax.errorbar(x,d['Y'],yerr=d['Err'],
                fmt=m, color=c, capsize=3)
ax.set_xticks(list(range(len(grouped))))
ax.set_xticklabels([a for a in grouped.groups])

enter image description here

Upvotes: 2

Related Questions