1

I would like to overplot a swarmplot and regplot in seaborn, so that I can have a y=x line through my swarmplot.

Here is my code:

import matplotlib.pyplot as plt
import seaborn as sns
    
sns.regplot(y=y, x=x, marker=' ', color='k')
sns.swarmplot(x=x_data, y=y_data)

I don't get any errors when I plot, but the regplot never shows on the plot. How can I fix this?

plot

EDIT: My regplot and swarmplot don't overplot and instead, plot in the same frame but separated by some unspecified y amount. If I flip them so regplot is above the call to swarmplot, regplot doesn't show up at all.

import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd

df = pd.DataFrame({"x":x_data,"y":y_data} )

sns.regplot(y="y", x="x", data= df, color='k', scatter_kws={"alpha" : 0.0})
sns.swarmplot(y="y", x="x", data= df)

updated plot

SECOND EDIT: The double axis solution from below works beautifully!

Marcus Campbell
  • 2,746
  • 4
  • 22
  • 36
morepenguins
  • 1,187
  • 10
  • 21

1 Answers1

1

In principle the approach of plotting a swarmplot and a regplot simulatneously works fine.

The problem here is that you set an empty marker (marker = " "). This destroys the regplot, such that it's not shown. Apparently this is only an issue when plotting several things to the same graph; plotting a single regplot with empty marker works fine.

The solution would be not to specify the marker argument, but instead set the markers invisible by using the scatter_kws argument: scatter_kws={"alpha" : 0.0}.

Here is a complete example:

import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np

## generate some data
n=19; m=9
y_data = []
for i in range(m):
    a = (np.random.poisson(lam=0.99-float(i)/m,size=n)+i*.9+np.random.rand(1)*2)
    a+=(np.random.rand(n)-0.5)*2
    y_data.append(a*m)
y_data = np.array(y_data).flatten()
x_data = np.floor(np.sort(np.random.rand(n*m))*m)
## put them into dataframe
df = pd.DataFrame({"x":x_data,"y":y_data} )

## plotting
sns.regplot(y="y", x="x", data= df, color='k', scatter_kws={"alpha" : 0.0})
sns.swarmplot(x="x", y="y", data= df)

plt.show()

enter image description here


Concerning the edited part of the question:
Since swarmplot is a categorical plot, the axis in the plot still goes from -0.5 to 8.5 and not as the labels suggest from 10 to 18. A possible workaround is to use two axes and twiny.
fig, ax = plt.subplots()
ax2 = ax.twiny()
sns.swarmplot(x="x", y="y", data= df, ax=ax)
sns.regplot(y="y", x="x", data= df, color='k', scatter_kws={"alpha" : 0.0},  ax=ax2)
ax2.grid(False) #remove grid as it overlays the other plot
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • This works with violinplot and regplot, but not with swarmplot and regplot. – morepenguins Jan 30 '17 at 15:41
  • As I said, I couldn't test it with seaborn > 0.7.dev. What happens when using swarmplot instead of violinplot in my example? Same issue as in the question? – ImportanceOfBeingErnest Jan 30 '17 at 15:57
  • I have now tinkered a solution that uses swarmplot. It's actually the same as before using violin plot, so there really is no difference. – ImportanceOfBeingErnest Jan 30 '17 at 16:37
  • Awesome! Thank you! It wasn't my downvote, but I was able to upvote it and cancel out the vote. – morepenguins Jan 30 '17 at 16:59
  • I'm running into an issue when I try this. I copied your code exactly, but replaced x_data and y_data with my actual data. I'm adding my problem as an edit above. – morepenguins Jan 30 '17 at 17:24
  • From what we see in the edit, you used the same code that is working for me, so the problem can only be in the data or in some other part of the code that you don't show. This is why it's so **important to always create a [mcve]**. In the text you say you wouldn't see the output when "regplot is above the call to swarmplot", but isn't that exactly the case in the code you show? So it would really help to have a clear description of the issue. – ImportanceOfBeingErnest Jan 30 '17 at 17:41
  • @morepenguins See edited answer, on your new problem. – ImportanceOfBeingErnest Jan 30 '17 at 21:50
  • Thank you so much! That solved it beautifully. Thank you also for the documentation on how to add the best example when asking questions - it's really helpful! – morepenguins Feb 03 '17 at 03:44