3

Is there any way to use non-uniform spacing on a discrete axis in seaborn's swarmplots?

Here's an example:

import pandas as pd
import numpy as np
import seaborn as sns
data = pd.DataFrame({
    "x":np.concatenate([np.repeat("A",450), np.repeat("B",5), np.repeat("C",5), np.repeat("D",450)]),
    "y":np.random.randn(910)})
sns.swarmplot(x="x",y="y",data=data)

Swarmplot with two over-wide categories and two infrequent categories

In the example the A and D categories overflow the plot. I could reduce the point size, but then the middle categories become hard to see. A better option would be to set the axis spacing such that the frequent categories have more space than the rare categories:

(Mockup) enter image description here

Is there any way to do this with seaborn (or the underlying matplotlib axes)?

Marcus Campbell
  • 2,746
  • 4
  • 22
  • 36
Quantum7
  • 3,165
  • 3
  • 34
  • 45

1 Answers1

0

You could manually create a bigger figure to plot on. E.g.

import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
data = pd.DataFrame({
    "x":np.concatenate([np.repeat("A",450), np.repeat("B",5), np.repeat("C",5), np.repeat("D",450)]),
    "y":np.random.randn(910)})
plt.figure(figsize=(16,8))
sns.swarmplot(x="x",y="y",data=data)
plt.show()

enter image description here

Just play around with the ratios for the figsize until you have your expected result.

  • As I see it, the question asks for a way to "set the axis spacing such that the frequent categories have more space than the rare categories". This is not the case here. – ImportanceOfBeingErnest Jun 12 '18 at 12:53
  • Yes, I just realized myself, that this isn't the answer the author was looking for. But i guess I leave it here, maybe it will be sufficient for him or i can figure out a better one – Marvin Taschenberger Jun 12 '18 at 12:54
  • This is actually what I did to make the mockup. The problem is that I have a couple big categories and like 20 small categories, so my plot is very wide and white with this technique. Thanks though! – Quantum7 Jun 12 '18 at 14:05