0

Trying to do a swarmplot with 3 different vectors in seaborn. I'd like to have each vector at a different x-coordinate and with a different colour.

Unfortunately all the tutorials have the data in some format I can't really find an explanation / manual for... This is what I've got so far:

#!/usr/bin/env python3
import numpy as np
import matplotlib.pyplot as plt
import seaborn
import pandas as pd

# Create figure
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_ylabel('Evaluations')

colors = [
    'tab:orange',
    'tab:green',
    'tab:red',
]

labels = [
    'Method 2',
    'Method 3',
    'Method 4',
]

data = [
    [1, 2.1, 3.2, 4.5, 3.6, 2.7, 1.4],
    [2.2, 4.7, 5.1, 4.4, 3.8, 5, 3.4],
    [8.4, 7.2, 6.1, 5.4, 8.1, 7.4, 6.8],
]
data = np.array(data).T

df = pd.DataFrame(data, columns=labels)

seaborn.swarmplot(data=df, y='Method 2', color=colors[0])
seaborn.swarmplot(data=df, y='Method 3', color=colors[1])
seaborn.swarmplot(data=df, y='Method 4', color=colors[2])

plt.show()

This almost works, but plots everything on the same axis:

enter image description here

Also, the label should be on the x-axis not the y-axis. Pretty sure I'm missing something really basic here. Anyone?

1 Answer 1

1

try:

df2 = df.melt()

colors = ["orange", "green", "red"]
sns.swarmplot(data=df2, x = 'variable', y='value', palette =colors)
plt.show()
1
  • Thanks, that gets them on separate axes, but still doesn't give them the colours I need Mar 19, 2019 at 17:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.